package org.ovirt.engine.api.restapi.resource; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.ovirt.engine.api.restapi.resource.BackendClustersResourceTest.getModel; import static org.ovirt.engine.api.restapi.resource.BackendClustersResourceTest.setUpEntityExpectations; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.UriInfo; import org.junit.Before; import org.junit.Test; import org.ovirt.engine.core.common.action.ClusterOperationParameters; import org.ovirt.engine.core.common.action.ClusterParametersBase; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.businessentities.Cluster; import org.ovirt.engine.core.common.businessentities.network.Network; import org.ovirt.engine.core.common.queries.IdQueryParameters; import org.ovirt.engine.core.common.queries.VdcQueryType; import org.ovirt.engine.core.compat.Guid; public class BackendDataCenterClusterResourceTest extends AbstractBackendSubResourceTest<org.ovirt.engine.api.model.Cluster, Cluster, BackendClusterResource<BackendDataCenterClustersResource>> { private static final Guid MANAGEMENT_NETWORK_ID = Guid.newGuid(); private static final Guid clusterId = GUIDS[0]; private static final Guid dataCenterId = GUIDS[1]; private boolean isPopulateSet = false; public BackendDataCenterClusterResourceTest() { super(new BackendDataCenterClusterResource( new BackendDataCenterClustersResource(dataCenterId.toString()), clusterId.toString())); } @Before public void initParent() { initResource(resource.parent); } @Override protected void setUriInfo(UriInfo uriInfo) { resource.setUriInfo(uriInfo); ((BackendDataCenterClusterResource)resource).getParent().setUriInfo(uriInfo); } @Override protected void init() { initResource(resource); initResource(((BackendDataCenterClusterResource)resource).getParent()); } @Test public void testBadGuid() throws Exception { try { new BackendDataCenterClusterResource( new BackendDataCenterClustersResource(dataCenterId.toString()), "foo"); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testGetNotFound() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, new ArrayList<Cluster>(), null); try { resource.get(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testGet() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpEntityQueryExpectations(VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, setUpClusters(), null); 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); setUpManagementNetworkExpectation(); setUriInfo(setUpActionExpectations(VdcActionType.UpdateCluster, ClusterOperationParameters.class, new String[] {}, new Object[] {}, true, true)); final org.ovirt.engine.api.model.Cluster updatedCluster = resource.update(getModel(0)); verifyModel(updatedCluster, 0); verifyManagementNetwork(updatedCluster); } private void verifyManagementNetwork(org.ovirt.engine.api.model.Cluster updatedCluster) { assertEquals(String.format("%s/%s/%s/%s/%s", BASE_PATH, "clusters", GUIDS[0], "networks", MANAGEMENT_NETWORK_ID), updatedCluster.getManagementNetwork().getHref()); } private void setUpManagementNetworkExpectation() throws Exception { setUpPopulateExpectation(); final Network mockNetwork = mock(Network.class); when(mockNetwork.getId()).thenReturn(MANAGEMENT_NETWORK_ID); setUpGetEntityExpectations(VdcQueryType.GetManagementNetwork, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, mockNetwork); } private void setUpPopulateExpectation() { if (!isPopulateSet) { when(httpHeaders.getRequestHeader(BackendResource.POPULATE)).thenReturn(Collections.singletonList("true")); isPopulateSet = true; } } @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.UpdateCluster, ClusterOperationParameters.class, new String[] {}, new Object[] {}, valid, success)); try { resource.update(getModel(0)); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } @Test public void testConflictedUpdate() throws Exception { setUpGetEntityExpectations(1); org.ovirt.engine.api.model.Cluster 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( VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, setUpClusters(), null ); setUriInfo( setUpActionExpectations( VdcActionType.RemoveCluster, ClusterParametersBase.class, new String[] { "ClusterId" }, new Object[] { GUIDS[0] }, true, true ) ); verifyRemove(resource.remove()); } @Test public void testRemoveNonExistant() throws Exception { setUpEntityQueryExpectations( VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, new ArrayList<Cluster>(), null ); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { assertNotNull(wae.getResponse()); assertEquals(404, wae.getResponse().getStatus()); } } @Test public void testRemoveCantDo() throws Exception { setUpEntityQueryExpectations( VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, setUpClusters(), null ); doTestBadRemove(false, true, CANT_DO); } @Test public void testRemoveFailed() throws Exception { setUpEntityQueryExpectations( VdcQueryType.GetClustersByStoragePoolId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { dataCenterId }, setUpClusters(), null ); doTestBadRemove(true, false, FAILURE); } protected void doTestBadRemove(boolean valid, boolean success, String detail) throws Exception { setUriInfo( setUpActionExpectations( VdcActionType.RemoveCluster, ClusterParametersBase.class, new String[] { "ClusterId" }, new Object[] { GUIDS[0] }, valid, success ) ); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } protected void setUpGetEntityExpectations(int times) throws Exception { setUpGetEntityExpectations(times, false); } protected void setUpGetEntityExpectations(int times, boolean notFound) throws Exception { while (times-- > 0) { setUpGetEntityExpectations(VdcQueryType.GetClusterById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, notFound ? null : getEntity(0)); } } @Override protected Cluster getEntity(int index) { return setUpEntityExpectations(mock(Cluster.class), index); } protected List<Cluster> setUpClusters() { List<Cluster> entities = new ArrayList<>(); for (int i = 0; i < NAMES.length; i++) { entities.add(getEntity(i)); } return entities; } }