package org.ovirt.engine.api.restapi.resource; 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.VnicProfile; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.action.VnicProfileParameters; import org.ovirt.engine.core.common.queries.IdQueryParameters; import org.ovirt.engine.core.common.queries.VdcQueryType; public abstract class AbstractBackendVnicProfileResourceTest<C extends AbstractBackendVnicProfileResource> extends AbstractBackendSubResourceTest<VnicProfile, org.ovirt.engine.core.common.businessentities.network.VnicProfile, AbstractBackendVnicProfileResource> { protected AbstractBackendVnicProfileResourceTest(C resource) { super(resource); } @Test public void testRemoveNotFound() throws Exception { setUpEntityQueryExpectations(1, 0, true); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testRemove() throws Exception { setUpEntityQueryExpectations(2, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.RemoveVnicProfile, VnicProfileParameters.class, new String[] {}, new Object[] {}, true, true)); verifyRemove(resource.remove()); } @Test public void testRemoveNonExistant() throws Exception { setUpEntityQueryExpectations(VdcQueryType.GetVnicProfileById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, null); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { assertNotNull(wae.getResponse()); assertEquals(404, wae.getResponse().getStatus()); } } @Test public void testRemoveCantDo() throws Exception { doTestBadRemove(false, true, CANT_DO); } @Test public void testRemoveFailed() throws Exception { doTestBadRemove(true, false, FAILURE); } protected void doTestBadRemove(boolean valid, boolean success, String detail) throws Exception { setUpEntityQueryExpectations(2, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.RemoveVnicProfile, VnicProfileParameters.class, new String[] {}, new Object[] {}, valid, success)); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } protected void setUpEntityQueryExpectations(int times, int index, boolean notFound) throws Exception { while (times-- > 0) { setUpEntityQueryExpectations(VdcQueryType.GetVnicProfileById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[index] }, notFound ? null : getEntity(index)); } } @Override protected org.ovirt.engine.core.common.businessentities.network.VnicProfile getEntity(int index) { return setUpEntityExpectations(mock(org.ovirt.engine.core.common.businessentities.network.VnicProfile.class), index); } static org.ovirt.engine.core.common.businessentities.network.VnicProfile setUpEntityExpectations(org.ovirt.engine.core.common.businessentities.network.VnicProfile entity, int index) { when(entity.getId()).thenReturn(GUIDS[index]); when(entity.getName()).thenReturn(NAMES[index]); when(entity.getDescription()).thenReturn(DESCRIPTIONS[index]); when(entity.getNetworkId()).thenReturn(GUIDS[index]); return entity; } }