/** * @should set using name * @should set using uuid */ @Override public void setAsText(String text) throws IllegalArgumentException { UserService es = Context.getUserService(); if (StringUtils.hasText(text)) { try { Privilege p = es.getPrivilege(text); setValue(p); //when a privilege is not found, no exception is generated. throw one to execute the catch block if (p == null) { throw new Exception(); } } catch (Exception ex) { Privilege p = es.getPrivilegeByUuid(text); setValue(p); if (p == null) { log.error("Error setting text: " + text, ex); throw new IllegalArgumentException("Privilege not found: " + ex.getMessage()); } } } else { setValue(null); } }
/** * @see UserService#getPrivilegeByUuid(String) */ @Test public void getPrivilegeByUuid_shouldReturnNullIfNoObjectFoundWithGivenUuid() { Assert.assertNull(userService.getPrivilegeByUuid("some invalid uuid")); }
/** * @see UserService#getPrivilegeByUuid(String) */ @Test public void getPrivilegeByUuid_shouldFindObjectGivenValidUuid() { executeDataSet(XML_FILENAME); String uuid = "d979d066-15e6-467c-9d4b-cb575ef97f0f"; Privilege privilege = userService.getPrivilegeByUuid(uuid); Assert.assertEquals("Some Privilege", privilege.getPrivilege()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getByUniqueId(java.lang.String) */ @Override public Privilege getByUniqueId(String uniqueId) { Privilege privilege = Context.getUserService().getPrivilegeByUuid(uniqueId); if (privilege == null) privilege = Context.getUserService().getPrivilege(uniqueId); return privilege; }
@Test public void shouldEditingAPrivilege() throws Exception { final String newDescription = "updated descr"; SimpleObject privilege = new SimpleObject(); assertEquals(false, newDescription.equals(service.getPrivilegeByUuid(getUuid()).getName())); privilege.add("description", newDescription); String json = new ObjectMapper().writeValueAsString(privilege); MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); req.setContent(json.getBytes()); handle(req); assertEquals(newDescription, service.getPrivilegeByUuid(getUuid()).getDescription()); }
@Test public void shouldPurgeAPrivilege() throws Exception { assertNotNull(service.getPrivilegeByUuid(getUuid())); MockHttpServletRequest req = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); req.addParameter("purge", "true"); handle(req); assertNull(service.getPrivilegeByUuid(getUuid())); }
@Test public void shouldGetAPrivilegeByUuid() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); SimpleObject result = deserialize(handle(req)); Privilege privilege = service.getPrivilegeByUuid(getUuid()); assertEquals(privilege.getUuid(), PropertyUtils.getProperty(result, "uuid")); assertEquals(privilege.getPrivilege(), PropertyUtils.getProperty(result, "name")); assertEquals(privilege.getDescription(), PropertyUtils.getProperty(result, "description")); }