TrendController { @RequestMapping(value = "/vpr/trend/**", method = RequestMethod.GET, params = "format=json") public ModelAndView renderJson(HttpServletRequest request) throws Exception { String uid = getUidFromUrl(request.getRequestURI()); String range = request.getParameter("range"); Class clazz = UidUtils.getDomainClassByUid(uid); IPatientObject item = (IPatientObject) genericPatientObjectDao.findByUID(clazz, uid); String pid = item.getPid(); Map<String, Object> itemData = item.getData(); String qnm = (String) item.getData().get("qualifiedName"); if (StringUtils.hasText(qnm) && !qnm.startsWith("\"")) { qnm = "\"" + qnm + "\""; } QueryDef qryDef = new QueryDef().usingIndex(getIndex(item), qnm); if (StringUtils.hasText(range)) { qryDef.where("observed").between(new PointInTime(range), PointInTime.now()); } if (StringUtils.hasText((String) itemData.get("units"))) { qryDef.where("units").is(itemData.get("units")); } List< IPatientObject > items = (List<IPatientObject>) genericPatientObjectDao.findAllByQuery(clazz, qryDef, Collections.singletonMap("pid", (Object) pid)); String units = ""; for(IPatientObject obj: items) { String unt = (obj.getData().get("units")!=null?obj.getData().get("units").toString():""); if(units.equals("")) { units = unt; } else { if(!units.equals(unt)) { throw new Exception("Units of measure not compatible in query"); } } } List data = createTrendData(items); JsonCCollection cr = JsonCCollection.create(data); cr.put("name", itemData.get("qualifiedName")); cr.put("type", "line"); cr.put("units", units); final Link link = createLink(request); cr.setSelfLink((link == null ? null : link.getHref())); return ModelAndViewFactory.contentNegotiatingModelAndView(cr); }  @Autowired void setLinkService(ILinkService linkService); @Autowired void setGenericPatientObjectDao(IGenericPatientObjectDAO genericPatientObjectDao); @RequestMapping(value = "/vpr/trend/**", method = RequestMethod.GET, params = "format=xml") String renderXml(HttpServletRequest request); @RequestMapping(value = "/vpr/trend/**", method = RequestMethod.GET, params = "format=json") ModelAndView renderJson(HttpServletRequest request); @RequestMapping(value = "/vpr/trend/**", method = RequestMethod.GET, params = "format=extjs") @ResponseBody String renderExtJs(@RequestParam("format") String format, HttpServletRequest request); static final String LAB_INDEX; static final String VITAL_INDEX; }
@Test public void testRenderJson() throws Exception { mockRequest.setRequestURI("/vpr/trend/urn:va:lab:F484:8:CH;6879469.819787;690"); Result result = new Result(); result.setData("pid", "23"); result.setData("typeName", "GLUCOSE"); when(genericPatientObjectDaoMock.findByUID(Result.class, "urn:va:lab:F484:8:CH;6879469.819787;690")).thenReturn(result); Link link = new Link(); link.setRel(LinkRelation.TREND.toString()); when(linkServiceMock.getLinks(result)).thenReturn(Arrays.asList(link)); Result result1 = new Result(); result1.setData("pid", "23"); result1.setData("observed", 20120920); result1.setData("result", "12"); when(genericPatientObjectDaoMock.findAllByQuery(any(Class.class), any(QueryDef.class), anyMap())).thenReturn(Arrays.asList(result1)); ModelAndView mv = tc.renderJson(mockRequest); assertNotNull(mv); JsonCCollection<Map<String,Object>> resp = (JsonCCollection<Map<String,Object>>) mv.getModel().get(ModelAndViewFactory.DEFAULT_MODEL_KEY); assertNotNull(resp); assertEquals("GLUCOSE", resp.getAdditionalData().get("name")); assertEquals("line", resp.getAdditionalData().get("type")); DateTime t = new DateTime(2012, 9, 20, 12, 0, 0, 0); Map<String, Object> map2 = new LinkedHashMap<>(2); map2.put("x", t.getMillis()); map2.put("y", 12.0f); assertThat(resp.getItems().get(0), equalTo(map2)); }