package org.ovirt.engine.api.restapi.resource.aaa; import static org.ovirt.engine.api.restapi.resource.aaa.BackendUsersResourceTest.GROUPS; import static org.ovirt.engine.api.restapi.resource.aaa.BackendUsersResourceTest.PARSED_GROUPS; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; import java.util.stream.Collectors; import javax.ws.rs.WebApplicationException; import org.junit.Test; import org.ovirt.engine.api.model.Group; import org.ovirt.engine.api.model.User; 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.DbUser; import org.ovirt.engine.core.common.queries.IdQueryParameters; import org.ovirt.engine.core.common.queries.VdcQueryType; public class BackendUserResourceTest extends AbstractBackendSubResourceTest<User, DbUser, BackendUserResource> { public BackendUserResourceTest() { super(new BackendUserResource(GUIDS[0].toString(), new BackendUsersResource())); } protected void init() { super.init(); initResource(resource.getParent()); } @Test public void testBadGuid() throws Exception { try { new BackendUserResource("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.RemoveUser, IdParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, true, true)); verifyRemove(resource.remove()); } @Test public void testRemoveNonExistant() throws Exception { setUpGetEntityExpectations( VdcQueryType.GetDbUserByUserId, 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); } private void doTestBadRemove(boolean valid, boolean success, String detail) throws Exception { setUpGetEntityExpectations(); setUriInfo(setUpActionExpectations( VdcActionType.RemoveUser, IdParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, valid, success)); try { resource.remove(); fail("expected WebApplicationException"); } catch (WebApplicationException wae) { verifyFault(wae, detail); } } protected void setUpGetEntityExpectations() throws Exception { setUpGetEntityExpectations(false); } protected void setUpGetEntityExpectations(boolean notFound) throws Exception { setUpGetEntityExpectations(VdcQueryType.GetDbUserByUserId, IdQueryParameters.class, new String[] { "Id" }, new Object[] { GUIDS[0] }, notFound ? null : getEntity(0)); } @Override protected DbUser getEntity(int index) { DbUser entity = new DbUser(); entity.setId(GUIDS[index]); entity.setExternalId(EXTERNAL_IDS[index]); entity.setFirstName(NAMES[index]); entity.setGroupNames(new LinkedList<>(Arrays.asList(GROUPS.split(",")))); entity.setDomain(DOMAIN); return entity; } protected void verifyModel(User model, int index) { assertEquals(GUIDS[index].toString(), model.getId()); assertEquals(NAMES[index], model.getName()); assertNotNull(model.getDomain()); assertTrue(model.isSetGroups()); assertEquals(PARSED_GROUPS.length, model.getGroups().getGroups().size()); Set<String> groupNames = model.getGroups().getGroups().stream().map(Group::getName).collect(Collectors.toSet()); assertEquals(new HashSet<>(Arrays.asList(PARSED_GROUPS)), groupNames); verifyLinks(model); } }