ParkController { @SuppressWarnings("unchecked") @RequestMapping("/{token}*") public String userFront(@PathVariable String token, @RequestParam(required = false) String tag, @RequestParam(required = false) String q, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size, @RequestParam(required = false) Boolean asc, @RequestParam(required = false) String[] sort, Model model, HttpSession session) throws CerealException{ StringBuilder builder = new StringBuilder(); builder.append("Request=").append("token=").append(token).append(",q=") .append(q).append(",tag=").append(tag).append(",page=") .append(page).append(",size=").append(size).append(",asc=") .append(asc).append(",sort=").append(sort); logger.info(builder.toString()); if (isValid(token)) { token = clean(token); Map<String, Object>[] stbs = null; Map<String, Object>[] allDevices = null; Pageable pageable = null; if (null != sort) { if (0 == size) { size = 100; } Direction direction = asc ? Direction.ASC : Direction.DESC; Sort sorting = new Sort(direction, sort); pageable = new PageRequest(page, size, sorting); } if (null != tag) { if (null != q) { model.addAttribute("search", q); } stbs = service.findByCriteria(new TagCondition(tag, q).toCriteria(), pageable); } else if ((null != q) && (!q.trim().isEmpty())) { String[] keys = getKeys(q); stbs = service.findByKeys(keys, pageable); model.addAttribute("search", q); }else { Condition condition = (Condition) session .getAttribute("condition"); if (condition != null) { stbs = service.findByCriteria(condition.toCriteria(), pageable); session.removeAttribute("condition"); } else { stbs = service.findAll(pageable); model.addAttribute("search", ""); allDevices = stbs; } } Map<String, Object> requestParams = new HashMap<String, Object>(); requestParams.put("tag",tag); requestParams.put("q", q); requestParams.put("page", page); requestParams.put("size", size); requestParams.put("asc", asc); requestParams.put("sort", sort); String requestParamsJson = engine.writeToString(requestParams, Map.class); model.addAttribute("requestParams", requestParamsJson); Map<String, Object> devices = new HashMap<String, Object>(); Map<String, List<String>> deviceTags = new HashMap<String, List<String>>(); Object tagData = null; boolean anyReserved = false; for (Map<String, Object> stb : stbs) { if (stb.containsKey(MetaStb.MACADDRESS)) { String id = (String) stb.get(MetaStb.ID); devices.put(id, stb); tagData = stb.get(MetaStb.TAGS); if (tagData != null && (tagData instanceof List)) { deviceTags.put(id, (List<String>) tagData); } else { deviceTags.put(id, null); } String reserver = (String) stb .get(PersistableDevice.RESERVER); if ((token != null) && token.equals(reserver)) { anyReserved = true; } } } String deviceData = engine.writeToString(devices, Map.class); allDevices = allDevices == null ? service.findAll() : allDevices; Collection<String> otherTags = new HashSet<String>(); for (Map<String, Object> device : allDevices) { String id = (String) device.get(MetaStb.ID); if (!deviceTags.containsKey(id)) { tagData = device.get(MetaStb.TAGS); if(tagData != null && (tagData instanceof List)) { otherTags.addAll((List<String>) tagData); } } } model.addAttribute(LATEST_SEARCH_CONDITION_MODEL_ATTRIBUTE_NAME, returnAndRemoveLatestSearchCondition(session)); model.addAttribute(SEARCH_CONDITIONS_MODEL_ATTRIBUTE_NAME, getSearchConditions(session)); model.addAttribute("token", token); model.addAttribute("deviceData", deviceData); model.addAttribute("deviceTags", engine.writeToString(deviceTags)); model.addAttribute("otherTags", engine.writeToString(new ArrayList<String>(otherTags))); model.addAttribute("data", devices.values()); model.addAttribute("dawgShowUrl", config.getDawgShowUrl()); model.addAttribute("dawgPoundUrl", config.getDawgPoundUrl()); model.addAttribute("anyReserved", anyReserved); model.addAttribute("filterFields", SearchableField.values()); model.addAttribute("operators", Operator.values()); return "index"; } else { return "login"; } }  @PostConstruct void init(); @RequestMapping({ "/", "index*" }) String index(Model model); @RequestMapping(value = "/login", method = RequestMethod.GET) ModelAndView login(
    		@RequestParam(value = "error", required = false) String error,
    		@RequestParam(value = "logout", required = false) String logout,
    		@RequestParam(value = "unauthorized", required = false) String unauthorized); @SuppressWarnings("unchecked") @RequestMapping("/{token}*") String userFront(@PathVariable String token,
            @RequestParam(required = false) String tag,
            @RequestParam(required = false) String q,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size,
            @RequestParam(required = false) Boolean asc,
            @RequestParam(required = false) String[] sort, Model model,
            HttpSession session); @RequestMapping(value = "/filter/store", method = RequestMethod.POST) @ResponseBody void storeFilter(@RequestBody String condition, HttpSession session); @RequestMapping(value = "/filter/remove", method = RequestMethod.POST) @ResponseBody void removeFilter(@RequestBody String condition, HttpSession session);  }
@SuppressWarnings("unchecked") @Test(dataProvider="testUserFrontWithValidTokenData") public void testUserFrontWithValidToken(String token, String tag,String q, String[] sort, String[] otherTagsExp) { ParkController controller = new ParkController(); Map<String, Object>[] filteredStbs = new HashMap[1]; Map<String, Object>[] allStbs = new HashMap[2]; Map<String, Object> stb1 = new HashMap<String, Object>(); stb1.put(MetaStb.ID, "sample"); stb1.put(MetaStb.TAGS, Arrays.asList("tag1")); stb1.put(MetaStb.MACADDRESS, "00:00:00:00:00"); Map<String, Object> stb2 = new HashMap<String, Object>(); stb2.put(MetaStb.ID, "otherDevice"); stb2.put(MetaStb.TAGS, Arrays.asList("tag2", "tag3")); stb2.put(MetaStb.MACADDRESS, "00:00:00:00:01"); filteredStbs[0] = stb1; allStbs[0] = stb1; allStbs[1] = stb2; ReflectionTestUtils.setField(controller, "serverUtils", utils); ParkService mockService = EasyMock.createMock(ParkService.class); if (q == null) { EasyMock.expect(mockService.findAll((Pageable) EasyMock.anyObject())).andReturn(allStbs); } else { EasyMock.expect(mockService.findByKeys((String[]) EasyMock.anyObject(), (Pageable) EasyMock.anyObject())).andReturn( filteredStbs); EasyMock.expect(mockService.findAll()).andReturn(allStbs); } if (tag != null) { EasyMock.expect( mockService.findByCriteria( (Criteria) EasyMock.anyObject(), (Pageable) EasyMock.anyObject())).andReturn( filteredStbs); } EasyMock.replay(mockService); ReflectionTestUtils.setField(controller, "service", mockService); ReflectionTestUtils.setField(controller, "config", createConfig()); Integer page = Integer.valueOf(1); Integer size = Integer.valueOf(1); Boolean asc = Boolean.TRUE; try { Model model = new BindingAwareModelMap(); MockHttpSession session = new MockHttpSession(); Assert.assertEquals("index", controller.userFront(token, tag,q, page, size, asc, sort, model, session)); Assert.assertEquals(model.asMap().get("search"), q == null ? "" : q); String otherTagsJson = (String) model.asMap().get("otherTags"); String deviceTagsJson = (String) model.asMap().get("deviceTags"); JsonCerealEngine engine = new JsonCerealEngine(); List<String> otherTags = engine.readFromString(otherTagsJson, List.class); Map<String, List<String>> deviceTags = engine.readFromString(deviceTagsJson, Map.class); Assert.assertEquals(otherTags.size(), otherTagsExp.length); for (String tags : otherTagsExp) { Assert.assertTrue(otherTags.contains(tags)); } Assert.assertEquals(deviceTags.size(), q != null ? 1 : 2); Assert.assertTrue(deviceTags.containsKey("sample")); Assert.assertTrue(deviceTags.get("sample").contains("tag1")); } catch (CerealException e) { Assert.fail(e.getMessage()); } }
@SuppressWarnings("unchecked") @Test(dataProvider="testUserFrontWithInvalidTokenData") public void testUserFrontWithInvalidToken(String token,String tags, String q, String[] sort, String[] otherTagsExp) { ParkController controller = new ParkController(); Integer page = Integer.valueOf(1); Integer size = Integer.valueOf(1); Boolean asc = Boolean.TRUE; try { Model model = new BindingAwareModelMap(); MockHttpSession session = new MockHttpSession(); ReflectionTestUtils.setField(controller, "serverUtils", utils); Assert.assertEquals("login", controller.userFront(token, q,tags, page, size, asc, sort, model, session)); }catch (CerealException e) { Assert.fail(e.getMessage()); } }