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.Network; import org.ovirt.engine.core.common.action.NetworkAttachmentParameters; import org.ovirt.engine.core.common.action.RemoveNetworkAttachmentParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; import org.ovirt.engine.core.common.queries.IdQueryParameters; import org.ovirt.engine.core.common.queries.VdcQueryReturnValue; import org.ovirt.engine.core.common.queries.VdcQueryType; import org.ovirt.engine.core.compat.Guid; public abstract class AbstractBackendNetworkAttachmentResourceTest<C extends AbstractBackendNetworkAttachmentsResource, R extends AbstractBackendNetworkAttachmentResource<C>> extends AbstractBackendSubResourceTest<org.ovirt.engine.api.model.NetworkAttachment, NetworkAttachment, R> { protected static final Guid hostId = Guid.newGuid(); public AbstractBackendNetworkAttachmentResourceTest(R resource) { super(resource); } @Test public void testBadGuid() throws Exception { try { createReourceWithBadGuid(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } protected abstract void createReourceWithBadGuid(); @Test public void testGetNotFound() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(1, 0, true); try { resource.get(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testGet() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(1, 0, false); verifyModel(resource.get(), 0); } @Test public void testUpdateNotFound() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(1, 0, true); try { resource.update(getModel(0)); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testUpdate() throws Exception { setUpEntityQueryExpectations(2, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.UpdateNetworkAttachment, NetworkAttachmentParameters.class, new String[] {}, new Object[] {}, 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); } @Test public void testConflictedUpdate() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(1, 0, false); org.ovirt.engine.api.model.NetworkAttachment 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 { setUpEntityQueryExpectations(1, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.RemoveNetworkAttachment, RemoveNetworkAttachmentParameters.class, new String[] {}, new Object[] {}, true, true)); verifyRemove(resource.remove()); } @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(1, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.RemoveNetworkAttachment, RemoveNetworkAttachmentParameters.class, new String[] {}, new Object[] {}, valid, success)); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } @Test public void testRemoveNotFound() throws Exception { setUpEntityQueryExpectations(1, 0, true); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testRemoveNonExistant() throws Exception { VdcQueryReturnValue vdcQueryReturnValue = new VdcQueryReturnValue(); vdcQueryReturnValue.setSucceeded(false); setUpEntityQueryExpectations(VdcQueryType.GetNetworkAttachmentById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { new Guid(resource.id) }, vdcQueryReturnValue); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { assertNotNull(wae.getResponse()); assertEquals(404, wae.getResponse().getStatus()); } } @Override protected NetworkAttachment getEntity(int index) { return setUpEntityExpectations(mock(NetworkAttachment.class), index); } @Override protected final void verifyModel(org.ovirt.engine.api.model.NetworkAttachment model, int index) { assertEquals(GUIDS[index].toString(), model.getId()); assertEquals(GUIDS[index].toString(), model.getNetwork().getId()); verifyModel(model); verifyLinks(model); } protected void verifyModel(org.ovirt.engine.api.model.NetworkAttachment model) { } protected void setUpEntityQueryExpectations(int times, int index, boolean notFound) throws Exception { while (times-- > 0) { setUpEntityQueryExpectations(VdcQueryType.GetNetworkAttachmentById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[index] }, notFound ? null : getEntity(index)); } } protected final NetworkAttachment setUpEntityExpectations(NetworkAttachment entity, int index) { when(entity.getId()).thenReturn(GUIDS[index]); when(entity.getNetworkId()).thenReturn(GUIDS[index]); setUpEntityExpectations(entity); return entity; } protected void setUpEntityExpectations(NetworkAttachment entity) { } protected org.ovirt.engine.api.model.NetworkAttachment getModel(int index) { org.ovirt.engine.api.model.NetworkAttachment model = new org.ovirt.engine.api.model.NetworkAttachment(); model.setId(GUIDS[index].toString()); model.setNetwork(new Network()); model.getNetwork().setId(GUIDS[index].toString()); return model; } private void doTestBadUpdate(boolean valid, boolean success, String detail) throws Exception { setUpEntityQueryExpectations(1, 0, false); setUriInfo(setUpActionExpectations(VdcActionType.UpdateNetworkAttachment, NetworkAttachmentParameters.class, new String[] {}, new Object[] {}, valid, success)); try { resource.update(getModel(0)); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } }