public class OrderTypeSerializer extends JsonSerializer<OrderType> { @Override public void serialize(OrderType value, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { generator.writeStartObject(); generator.writeFieldName("id"); generator.writeNumber(value.getId()); generator.writeFieldName("name"); generator.writeString(value.getName()); generator.writeEndObject(); } }
} else { OrderType orderType = (OrderType) obj; String name = orderType.getName(); if (!StringUtils.hasText(name)) { errors.rejectValue("name", "error.name"); errors.rejectValue("parent", "OrderType.parent.amongDescendants", new Object[] { orderType.getName() }, "Parent of " + orderType.getName() + " is among its descendants"); if (cc != null && orderType.getConceptClasses().contains(cc)) { errors.rejectValue("conceptClasses[" + index + "]", "OrderType.duplicate", new Object[] { cc.getName(), orderType.getName() }, cc.getName() + " is already associated to another order type:" + orderType.getName());
/** * @see OrderService#getOrderTypeByUuid(String) */ @Test public void getOrderTypeByUuid_shouldFindOrderTypeObjectGivenValidUuid() { OrderType orderType = orderService.getOrderTypeByUuid("131168f4-15f5-102d-96e4-000c29c2a5d7"); assertEquals("Drug order", orderType.getName()); }
/** * @see OrderService#getOrderType(Integer) */ @Test public void getOrderType_shouldFindOrderTypeObjectGivenValidId() { assertEquals("Drug order", orderService.getOrderType(1).getName()); }
/** * @see OrderTypeValidator#validate(Object,Errors) */ @Test public void validate_shouldFailIfNameIsADuplicate() { OrderType orderType = new OrderType(); orderType.setName(orderService.getOrderType(1).getName()); Errors errors = new BindException(orderType, "orderType"); new OrderTypeValidator().validate(orderType, errors); Assert.assertEquals(true, errors.hasFieldErrors("name")); }
/** * @see OrderService#saveOrderType(org.openmrs.OrderType) */ @Test public void saveOrderType_shouldAddANewOrderTypeToTheDatabase() { int orderTypeCount = orderService.getOrderTypes(true).size(); OrderType orderType = new OrderType(); orderType.setName("New Order"); orderType.setJavaClassName("org.openmrs.NewTestOrder"); orderType.setDescription("New order type for testing"); orderType.setRetired(false); orderType = orderService.saveOrderType(orderType); assertNotNull(orderType); assertEquals("New Order", orderType.getName()); assertNotNull(orderType.getId()); assertEquals((orderTypeCount + 1), orderService.getOrderTypes(true).size()); }
/** * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext) */ @Override protected NeedsPaging<OrderType> doSearch(RequestContext context) { List<OrderType> orderTypes = Context.getOrderService().getOrderTypes(context.getIncludeAll()); for (Iterator<OrderType> iterator = orderTypes.iterator(); iterator.hasNext();) { OrderType ot = iterator.next(); if (!Pattern.compile(Pattern.quote(context.getParameter("q")), Pattern.CASE_INSENSITIVE).matcher(ot.getName()) .find()) { iterator.remove(); } } return new NeedsPaging<OrderType>(orderTypes, context); }
@Test public void shouldEditAnOrderType() throws Exception { final String newName = "Updated name"; SimpleObject conceptMapTypeType = new SimpleObject(); conceptMapTypeType.add("name", newName); String json = new ObjectMapper().writeValueAsString(conceptMapTypeType); MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); req.setContent(json.getBytes()); handle(req); assertEquals(newName, service.getOrderTypeByUuid(getUuid()).getName()); }
@Test public void shouldGetAnOrderTypeByUuid() throws Exception { SimpleObject result = deserialize(handle(newGetRequest(getURI() + "/" + getUuid()))); OrderType expectedOrderType = service.getOrderTypeByUuid(getUuid()); assertEquals(expectedOrderType.getUuid(), PropertyUtils.getProperty(result, "uuid")); assertEquals(expectedOrderType.getName(), PropertyUtils.getProperty(result, "name")); assertEquals(expectedOrderType.getJavaClassName(), PropertyUtils.getProperty(result, "javaClassName")); assertEquals(expectedOrderType.getDescription(), PropertyUtils.getProperty(result, "description")); assertEquals(expectedOrderType.isRetired(), PropertyUtils.getProperty(result, "retired")); assertNull(PropertyUtils.getProperty(result, "auditInfo")); }
@Test public void shouldGetAnOrderTypeByName() throws Exception { final String name = "Test order"; SimpleObject result = deserialize(handle(newGetRequest(getURI() + "/" + name))); OrderType expectedOrderType = service.getOrderTypeByName(name); assertEquals(expectedOrderType.getUuid(), PropertyUtils.getProperty(result, "uuid")); assertEquals(expectedOrderType.getName(), PropertyUtils.getProperty(result, "name")); }
@Override public void validateFullRepresentation() throws Exception { super.validateFullRepresentation(); assertPropEquals("name", getObject().getName()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("javaClassName", getObject().getJavaClassName()); assertPropEquals("retired", getObject().isRetired()); assertPropPresent("parent"); assertPropPresent("conceptClasses"); assertPropPresent("auditInfo"); }
@Override public void validateDefaultRepresentation() throws Exception { super.validateDefaultRepresentation(); assertPropEquals("name", getObject().getName()); assertPropEquals("description", getObject().getDescription()); assertPropEquals("javaClassName", getObject().getJavaClassName()); assertPropEquals("retired", getObject().isRetired()); assertPropPresent("parent"); assertPropPresent("conceptClasses"); assertPropNotPresent("auditInfo"); }