/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#asRepresentation(T, * org.openmrs.module.webservices.rest.web.representation.Representation) */ @Override public SimpleObject asRepresentation(User instance, Representation rep) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8(instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); return userResource.asRepresentation(userPass, rep); }
/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#getProperty(T, * java.lang.String) */ @Override public Object getProperty(User instance, String propertyName) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8(instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); return userResource.getProperty(userPass, propertyName); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation) */ @Override public DelegatingResourceDescription getRepresentationDescription(Representation rep) { DelegatingResourceDescription description = super.getRepresentationDescription(rep); if (description != null) { description.removeProperty("secretQuestion"); } return description; }
final String requestedRolesParameter = context.getParameter(PARAMETER_ROLES); if (requestedRolesParameter != null) { foundRoles = getRequestedRoles(requestedRolesParameter); if (isNoRequestedRoleFound(foundRoles)) {
/** * Assert that a search with the given parameters returns an expected number of results. * * @param userName The user name to search for. * @param roles The roles to search for. * @param expectedResultCount The expected result count for the given search parameters. * @throws ResponseException */ @SuppressWarnings("unchecked") private void assertSearch(final String userName, final Collection<String> roles, final int expectedResultCount) throws ResponseException { // input final MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("q", userName); if (roles != null) { final String rolesAsCommaSeparatedString = StringUtils.join(roles, ","); request.addParameter(UserResource1_8.PARAMETER_ROLES, rolesAsCommaSeparatedString); } final RequestContext context = RestUtil.getRequestContext(request, new MockHttpServletResponse()); // search final SimpleObject simple = getResource().search(context); final List<SimpleObject> results = (List<SimpleObject>) simple.get("results"); // verify final String errorMessage = "Number of results does not match for: userName=" + userName + ", roles=" + roles + ", Results=" + results; Assert.assertEquals(errorMessage, expectedResultCount, results.size()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#setProperty(java.lang.Object, * java.lang.String, java.lang.Object) */ @Override public void setProperty(Object instance, String propertyName, Object value) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8((User) instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); userResource.setProperty(userPass, propertyName, value); } }
@Test public void shouldUpdateUser() throws Exception { SimpleObject userSimpleObject = new SimpleObject(); userSimpleObject.putAll(new ObjectMapper().readValue( getClass().getClassLoader().getResourceAsStream("update_user.json"), HashMap.class)); SimpleObject updated = (SimpleObject) resource.update("c98a1558-e131-11de-babe-001e378eb67e", userSimpleObject, new RequestContext()); Map<String, String> userProperties = (Map<String, String>) updated.get("userProperties"); Assert.assertEquals(2, userProperties.size()); Assert.assertNotNull(userProperties.get("favouriteObsTemplates")); Assert.assertEquals("Gynaecology", userProperties.get("favouriteObsTemplates")); userSimpleObject.putAll(new ObjectMapper().readValue( getClass().getClassLoader().getResourceAsStream("update_user.json"), HashMap.class)); SimpleObject updatedAgain = (SimpleObject) resource.update("c98a1558-e131-11de-babe-001e378eb67e", userSimpleObject, new RequestContext()); }
@RepHandler(RefRepresentation.class) public SimpleObject asRef(UserAndPassword1_8 delegate) throws ConversionException { DelegatingResourceDescription description = new DelegatingResourceDescription(); description.addProperty("uuid"); description.addProperty("display"); if (delegate.isRetired()) { description.addProperty("retired"); } description.addSelfLink(); return convertDelegateToRepresentation(delegate, description); }
/** * @see {@link https://issues.openmrs.org/browse/RESTWS-490} * @throws Exception */ @SuppressWarnings("unchecked") @Test public void testCorrectResourceForUser() throws Exception { // prepare final MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("q", ""); // query for all final RequestContext context = RestUtil.getRequestContext(request, new MockHttpServletResponse()); // search final SimpleObject simple = getResource().search(context); final List<SimpleObject> results = (List<SimpleObject>) simple.get("results"); // verify Assert.assertFalse("A non-empty list is expected.", results.isEmpty()); for (SimpleObject result : results) { final String selfLink = findSelfLink(result); Assert.assertTrue("Resource should be user, but is " + selfLink, selfLink.contains("/user/")); } }