package org.ovirt.engine.api.restapi.resource.aaa; import javax.ws.rs.WebApplicationException; import org.junit.Test; import org.ovirt.engine.api.model.Group; import org.ovirt.engine.api.restapi.resource.AbstractBackendSubResourceTest; import org.ovirt.engine.core.common.action.IdParameters; import org.ovirt.engine.core.common.action.VdcActionType; import org.ovirt.engine.core.common.businessentities.aaa.DbGroup; 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 BackendGroupResourceTest extends AbstractBackendSubResourceTest<Group, DbGroup, BackendGroupResource> { public BackendGroupResourceTest() { super(new BackendGroupResource(GUIDS[0].toString(), new BackendGroupsResource())); } protected void init() { super.init(); initResource(resource.getParent()); } @Test public void testBadGuid() throws Exception { try { new BackendGroupResource("foo", null); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testGetNotFound() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpGetEntityExpectations(true); try { resource.get(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyNotFoundException(wae); } } @Test public void testGet() throws Exception { setUriInfo(setUpBasicUriExpectations()); setUpGetEntityExpectations(); verifyModel(resource.get(), 0); } @Test public void testRemove() throws Exception { setUpGetEntityExpectations(); setUriInfo(setUpActionExpectations( VdcActionType.RemoveGroup, IdParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, true, true)); verifyRemove(resource.remove()); } @Test public void testRemoveNonExistant() throws Exception { setUpGetEntityExpectations(GUIDS[0], true); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { assertNotNull(wae.getResponse()); assertEquals(404, wae.getResponse().getStatus()); } } private void setUpGetEntityExpectations(Guid entityId, boolean returnNull) throws Exception { setUpGetEntityExpectations( VdcQueryType.GetDbGroupById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { entityId }, returnNull ? null : getEntity(0)); } @Test public void testRemoveCantDo() throws Exception { doTestBadRemove(false, true, CANT_DO); } @Test public void testRemoveFailed() throws Exception { doTestBadRemove(true, false, FAILURE); } private void doTestBadRemove(boolean valid, boolean success, String detail) throws Exception { setUpGetEntityExpectations(); setUriInfo(setUpActionExpectations( VdcActionType.RemoveGroup, IdParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, valid, success)); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } private void setUpGetEntityExpectations() throws Exception { setUpGetEntityExpectations(false); } private void setUpGetEntityExpectations(boolean notFound) throws Exception { setUpGetEntityExpectations( VdcQueryType.GetDbGroupById, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, notFound ? null : getEntity(0) ); } @Override protected DbGroup getEntity(int index) { DbGroup entity = new DbGroup(); entity.setId(GUIDS[index]); entity.setExternalId(EXTERNAL_IDS[index]); entity.setName(NAMES[index]); entity.setDomain(DOMAIN); return entity; } protected void verifyModel(Group model, int index) { assertEquals(GUIDS[index].toString(), model.getId()); assertEquals(NAMES[index], model.getName()); assertNotNull(model.getDomain()); verifyLinks(model); } }