/*
* Copyright (c) 2014 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ovirt.engine.api.restapi.resource.openstack;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import javax.ws.rs.WebApplicationException;
import org.junit.Test;
import org.ovirt.engine.api.model.OpenStackNetworkProvider;
import org.ovirt.engine.api.model.OpenStackNetworkProviderType;
import org.ovirt.engine.api.restapi.resource.AbstractBackendSubResourceTest;
import org.ovirt.engine.core.common.action.ProviderParameters;
import org.ovirt.engine.core.common.action.VdcActionType;
import org.ovirt.engine.core.common.businessentities.Provider;
import org.ovirt.engine.core.common.businessentities.ProviderType;
import org.ovirt.engine.core.common.queries.IdQueryParameters;
import org.ovirt.engine.core.common.queries.VdcQueryType;
public class BackendOpenStackNetworkProviderResourceTest
extends AbstractBackendSubResourceTest<OpenStackNetworkProvider, Provider, BackendOpenStackNetworkProviderResource> {
public BackendOpenStackNetworkProviderResourceTest() {
super(new BackendOpenStackNetworkProviderResource(GUIDS[0].toString()));
}
@Test
public void testBadGuid() throws Exception {
try {
new BackendOpenStackNetworkProviderResource("foo");
fail("expected WebApplicationException");
}
catch (WebApplicationException wae) {
verifyNotFoundException(wae);
}
}
@Test
public void testGetNotFound() throws Exception {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(1, true);
try {
resource.get();
fail("expected WebApplicationException");
} catch (WebApplicationException wae) {
verifyNotFoundException(wae);
}
}
@Test
public void testGet() throws Exception {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(1);
verifyModel(resource.get(), 0);
}
@Test
public void testUpdateNotFound() throws Exception {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(1, true);
try {
resource.update(getModel(0));
fail("expected WebApplicationException");
} catch (WebApplicationException wae) {
verifyNotFoundException(wae);
}
}
@Test
public void testUpdate() throws Exception {
setUpGetEntityExpectations(2);
setUriInfo(
setUpActionExpectations(
VdcActionType.UpdateProvider,
ProviderParameters.class,
new String[] { "Provider.Id" },
new Object[] { GUIDS[0] },
true,
true
)
);
verifyModel(resource.update(getModel(0)), 0);
}
@Test
public void testUpdateCantDo() throws Exception {
doTestBadUpdate(false, true, CANT_DO);
}
@Test
public void testUpdateFailed() throws Exception {
doTestBadUpdate(true, false, FAILURE);
}
private void doTestBadUpdate(boolean valid, boolean success, String detail) throws Exception {
setUpGetEntityExpectations(1);
setUriInfo(
setUpActionExpectations(
VdcActionType.UpdateProvider,
ProviderParameters.class,
new String[] { "Provider.Id" },
new Object[] { GUIDS[0] },
valid,
success
)
);
try {
resource.update(getModel(0));
fail("expected WebApplicationException");
}
catch (WebApplicationException wae) {
verifyFault(wae, detail);
}
}
@Test
public void testConflictedUpdate() throws Exception {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(1);
OpenStackNetworkProvider model = getModel(1);
model.setId(GUIDS[1].toString());
try {
resource.update(model);
fail("expected WebApplicationException");
}
catch (WebApplicationException wae) {
verifyImmutabilityConstraint(wae);
}
}
@Test
public void testRemove() throws Exception {
setUriInfo(setUpBasicUriExpectations());
setUpGetEntityExpectations(1);
setUpActionExpectations(VdcActionType.RemoveProvider,
ProviderParameters.class,
new String[] { "Provider.Id" },
new Object[] { GUIDS[0] },
true,
true);
resource.remove();
}
private OpenStackNetworkProvider getModel(int index) {
OpenStackNetworkProvider model = new OpenStackNetworkProvider();
model.setName(NAMES[index]);
model.setType(OpenStackNetworkProviderType.NEUTRON);
model.setDescription(DESCRIPTIONS[index]);
return model;
}
@Override
protected Provider getEntity(int index) {
Provider provider = mock(Provider.class);
when(provider.getId()).thenReturn(GUIDS[index]);
when(provider.getName()).thenReturn(NAMES[index]);
when(provider.getDescription()).thenReturn(DESCRIPTIONS[index]);
when(provider.getType()).thenReturn(ProviderType.OPENSTACK_NETWORK);
return provider;
}
protected void setUpGetEntityExpectations(int times) throws Exception {
setUpGetEntityExpectations(times, false);
}
protected void setUpGetEntityExpectations(int times, boolean notFound) throws Exception {
while (times-- > 0) {
setUpGetEntityExpectations(
VdcQueryType.GetProviderById,
IdQueryParameters.class,
new String[] { "Id" },
new Object[] { GUIDS[0] },
notFound? null: getEntity(0)
);
}
}
}