package org.ovirt.engine.api.restapi.resource; import static org.mockito.Mockito.any; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import org.junit.Ignore; import org.junit.Test; import org.ovirt.engine.api.model.Fault; import org.ovirt.engine.api.model.Network; import org.ovirt.engine.api.model.VnicProfile; import org.ovirt.engine.core.common.action.AddVnicProfileParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.queries.IdQueryParameters; import org.ovirt.engine.core.common.queries.VdcQueryParametersBase; 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 AbstractBackendVnicProfilesResourceTest<C extends AbstractBackendVnicProfilesResource> extends AbstractBackendCollectionResourceTest<VnicProfile, org.ovirt.engine.core.common.businessentities.network.VnicProfile, C> { protected static final Guid NETWORK_ID = GUIDS[1]; private VdcQueryType listQueryType; private Class<? extends VdcQueryParametersBase> listQueryParamsClass; public AbstractBackendVnicProfilesResourceTest(C collection, VdcQueryType listQueryType, Class<? extends VdcQueryParametersBase> queryParamsClass) { super(collection, null, ""); this.listQueryType = listQueryType; this.listQueryParamsClass = queryParamsClass; } @Test public void testAddVnicProfile() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpNetworkQueryExpectations(); setUpCreationExpectations(VdcActionType.AddVnicProfile, AddVnicProfileParameters.class, new String[] {}, new Object[] {}, true, true, GUIDS[0], VdcQueryType.GetVnicProfileById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { Guid.Empty }, getEntity(0)); VnicProfile model = getModel(0); model.setNetwork(new Network()); model.getNetwork().setId(NETWORK_ID.toString()); Response response = collection.add(model); assertEquals(201, response.getStatus()); assertTrue(response.getEntity() instanceof VnicProfile); verifyModel((VnicProfile) response.getEntity(), 0); } @Test public void testAddVnicProfileCantDo() throws Exception { setUpNetworkQueryExpectations(); doTestBadAddVnicProfile(false, true, CANT_DO); } @Test public void testAddVnicProfileFailure() throws Exception { setUpNetworkQueryExpectations(); doTestBadAddVnicProfile(true, false, FAILURE); } private void doTestBadAddVnicProfile(boolean valid, boolean success, String detail) throws Exception { setUriInfo(setUpActionExpectations(VdcActionType.AddVnicProfile, AddVnicProfileParameters.class, new String[] {}, new Object[] {}, valid, success)); VnicProfile model = getModel(0); model.setNetwork(new Network()); model.getNetwork().setId(NETWORK_ID.toString()); try { collection.add(model); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } @Test public void testAddIncompleteParameters() throws Exception { VnicProfile model = createIncompleteVnicProfile(); setUriInfo(setUpBasicUriExpectations()); try { collection.add(model); fail("expected WebApplicationException on incomplete parameters"); } catch (WebApplicationException wae) { verifyIncompleteException(wae, "VnicProfile", "validateParameters", getIncompleteFields()); } } protected String[] getIncompleteFields() { return new String[] { "name" }; } protected VnicProfile createIncompleteVnicProfile() { return new VnicProfile(); } @Test @Ignore @Override public void testQuery() throws Exception { } @Override @Test public void testList() throws Exception { UriInfo uriInfo = setUpUriExpectations(null); setUpVnicProfilesQueryExpectations(null); collection.setUriInfo(uriInfo); verifyCollection(getCollection()); } @Override @Test public void testListFailure() throws Exception { setUpVnicProfilesQueryExpectations(FAILURE); UriInfo uriInfo = setUpUriExpectations(null); collection.setUriInfo(uriInfo); try { getCollection(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { assertTrue(wae.getResponse().getEntity() instanceof Fault); assertEquals(mockl10n(FAILURE), ((Fault) wae.getResponse().getEntity()).getDetail()); } } @Override @Test public void testListCrash() throws Exception { Throwable t = new RuntimeException(FAILURE); setUpVnicProfilesQueryExpectations(t); UriInfo uriInfo = setUpUriExpectations(null); collection.setUriInfo(uriInfo); try { getCollection(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, BACKEND_FAILED_SERVER_LOCALE, t); } } @Override @Test public void testListCrashClientLocale() throws Exception { UriInfo uriInfo = setUpUriExpectations(null); locales.add(CLIENT_LOCALE); Throwable t = new RuntimeException(FAILURE); setUpVnicProfilesQueryExpectations(t); collection.setUriInfo(uriInfo); try { getCollection(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, BACKEND_FAILED_CLIENT_LOCALE, t); } finally { locales.clear(); } } private void setUpVnicProfilesQueryExpectations(Object failure) { VdcQueryReturnValue queryResult = mock(VdcQueryReturnValue.class); when(queryResult.getSucceeded()).thenReturn(failure == null); List<org.ovirt.engine.core.common.businessentities.network.VnicProfile> entities = new ArrayList<>(); if (failure == null) { for (int i = 0; i < NAMES.length; i++) { entities.add(getEntity(i)); } when(queryResult.getReturnValue()).thenReturn(entities); } else { if (failure instanceof String) { when(queryResult.getExceptionString()).thenReturn((String) failure); setUpL10nExpectations((String) failure); } else if (failure instanceof Exception) { when(backend.runQuery(eq(listQueryType), any(listQueryParamsClass))).thenThrow((Exception) failure); return; } } when(backend.runQuery(eq(listQueryType), any(listQueryParamsClass))).thenReturn( queryResult); } static VnicProfile getModel(int index) { VnicProfile model = new VnicProfile(); model.setId(GUIDS[index].toString()); model.setName(NAMES[index]); model.setDescription(DESCRIPTIONS[index]); return model; } @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); } protected void setUpNetworkQueryExpectations() { } 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; } }