@Test(dataProvider = "populateTestDataProvider") public void testPopulate(Boolean isToOverrideMetaData) { List<MetaStb> list = new ArrayList<MetaStb>(); MetaStb stb1 = new MetaStb(); stb1.setId("id1"); MetaStb stb2 = new MetaStb(); stb2.setId("id2"); list.add(stb1); list.add(stb2); String token = "4cb2e04d-5fed-4f52-83e7-c05f29acb432"; HouseRestController controller = new HouseRestController(); ReflectionTestUtils.setField(controller, "serverUtils", utils); PopulatingClient client = EasyMock.createMock(PopulatingClient.class); try { EasyMock.expect(client.populate((String) EasyMock.anyObject())).andReturn(list).anyTimes(); } catch (Exception e) { Assert.fail(e.getMessage()); } MockHouseService mockService = new MockHouseService(); EasyMock.replay(client); ReflectionTestUtils.setField(controller, "service", mockService); ReflectionTestUtils.setField(controller, "client", client); Assert.assertEquals(controller.populate(token, isToOverrideMetaData), 2); Assert.assertNotNull(mockService.getById("id1")); Assert.assertNotNull(mockService.getById("id2")); Assert.assertNull(mockService.getById("id3")); }
@RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody public int populate(@PathVariable String token, @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData) { int counter = 0; try { List<MetaStb> stbs; if(null != client) stbs = client.populate(token); else stbs = new LinkedList<MetaStb>(); String[] ids = new String[stbs.size()]; for(int i = 0; i < stbs.size(); i++) { ids[i] = stbs.get(i).getId(); } Map<String, Object>[] existingStbs = service.getStbsById(ids); Map<String, MetaStb> existingStbMap = new HashMap<String, MetaStb>(); for (Map<String, Object> data : existingStbs) { MetaStb stb = new MetaStb(data); existingStbMap.put(stb.getId(), stb); } for(MetaStb stb : stbs) { MetaStb existing = existingStbMap.get(stb.getId()); MetaStb stbToUpsert; if (existing == null) { stbToUpsert = stb; } else { Map<String, Object> newData = stb.getData(); if (!isToOverrideMetaData) { Set<String> modifiedProps = existing.getModifiedProps(); for (String key : newData.keySet()) { if ((modifiedProps == null) || !modifiedProps.contains(key)) { existing.getData().put(key, newData.get(key)); } } } else { for (String key : newData.keySet()) { existing.getData().put(key, newData.get(key)); } existing.setModifiedProps(null); } stbToUpsert = existing; } try { service.upsertStb(stbToUpsert); counter++; } catch (Exception e) { e.printStackTrace(); logger.warn("Could not load into MetaStb=" + stb.toString() + ", Token="+token); } } } catch (Exception e) { e.printStackTrace(); logger.error("Could not load anything from the PopulatingClient, Token=" + token); } return counter; }
HouseRestController { @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody public int populate(@PathVariable String token, @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData) { int counter = 0; try { List<MetaStb> stbs; if(null != client) stbs = client.populate(token); else stbs = new LinkedList<MetaStb>(); String[] ids = new String[stbs.size()]; for(int i = 0; i < stbs.size(); i++) { ids[i] = stbs.get(i).getId(); } Map<String, Object>[] existingStbs = service.getStbsById(ids); Map<String, MetaStb> existingStbMap = new HashMap<String, MetaStb>(); for (Map<String, Object> data : existingStbs) { MetaStb stb = new MetaStb(data); existingStbMap.put(stb.getId(), stb); } for(MetaStb stb : stbs) { MetaStb existing = existingStbMap.get(stb.getId()); MetaStb stbToUpsert; if (existing == null) { stbToUpsert = stb; } else { Map<String, Object> newData = stb.getData(); if (!isToOverrideMetaData) { Set<String> modifiedProps = existing.getModifiedProps(); for (String key : newData.keySet()) { if ((modifiedProps == null) || !modifiedProps.contains(key)) { existing.getData().put(key, newData.get(key)); } } } else { for (String key : newData.keySet()) { existing.getData().put(key, newData.get(key)); } existing.setModifiedProps(null); } stbToUpsert = existing; } try { service.upsertStb(stbToUpsert); counter++; } catch (Exception e) { e.printStackTrace(); logger.warn("Could not load into MetaStb=" + stb.toString() + ", Token="+token); } } } catch (Exception e) { e.printStackTrace(); logger.error("Could not load anything from the PopulatingClient, Token=" + token); } return counter; } }
HouseRestController { @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody public int populate(@PathVariable String token, @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData) { int counter = 0; try { List<MetaStb> stbs; if(null != client) stbs = client.populate(token); else stbs = new LinkedList<MetaStb>(); String[] ids = new String[stbs.size()]; for(int i = 0; i < stbs.size(); i++) { ids[i] = stbs.get(i).getId(); } Map<String, Object>[] existingStbs = service.getStbsById(ids); Map<String, MetaStb> existingStbMap = new HashMap<String, MetaStb>(); for (Map<String, Object> data : existingStbs) { MetaStb stb = new MetaStb(data); existingStbMap.put(stb.getId(), stb); } for(MetaStb stb : stbs) { MetaStb existing = existingStbMap.get(stb.getId()); MetaStb stbToUpsert; if (existing == null) { stbToUpsert = stb; } else { Map<String, Object> newData = stb.getData(); if (!isToOverrideMetaData) { Set<String> modifiedProps = existing.getModifiedProps(); for (String key : newData.keySet()) { if ((modifiedProps == null) || !modifiedProps.contains(key)) { existing.getData().put(key, newData.get(key)); } } } else { for (String key : newData.keySet()) { existing.getData().put(key, newData.get(key)); } existing.setModifiedProps(null); } stbToUpsert = existing; } try { service.upsertStb(stbToUpsert); counter++; } catch (Exception e) { e.printStackTrace(); logger.warn("Could not load into MetaStb=" + stb.toString() + ", Token="+token); } } } catch (Exception e) { e.printStackTrace(); logger.error("Could not load anything from the PopulatingClient, Token=" + token); } return counter; }  }
HouseRestController { @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody public int populate(@PathVariable String token, @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData) { int counter = 0; try { List<MetaStb> stbs; if(null != client) stbs = client.populate(token); else stbs = new LinkedList<MetaStb>(); String[] ids = new String[stbs.size()]; for(int i = 0; i < stbs.size(); i++) { ids[i] = stbs.get(i).getId(); } Map<String, Object>[] existingStbs = service.getStbsById(ids); Map<String, MetaStb> existingStbMap = new HashMap<String, MetaStb>(); for (Map<String, Object> data : existingStbs) { MetaStb stb = new MetaStb(data); existingStbMap.put(stb.getId(), stb); } for(MetaStb stb : stbs) { MetaStb existing = existingStbMap.get(stb.getId()); MetaStb stbToUpsert; if (existing == null) { stbToUpsert = stb; } else { Map<String, Object> newData = stb.getData(); if (!isToOverrideMetaData) { Set<String> modifiedProps = existing.getModifiedProps(); for (String key : newData.keySet()) { if ((modifiedProps == null) || !modifiedProps.contains(key)) { existing.getData().put(key, newData.get(key)); } } } else { for (String key : newData.keySet()) { existing.getData().put(key, newData.get(key)); } existing.setModifiedProps(null); } stbToUpsert = existing; } try { service.upsertStb(stbToUpsert); counter++; } catch (Exception e) { e.printStackTrace(); logger.warn("Could not load into MetaStb=" + stb.toString() + ", Token="+token); } } } catch (Exception e) { e.printStackTrace(); logger.error("Could not load anything from the PopulatingClient, Token=" + token); } return counter; }  @RequestMapping(value="list*", method = RequestMethod.GET) @ResponseBody Map<String,Object>[] listAll(); @RequestMapping(value="id/{id}", method = {RequestMethod.GET}) @ResponseBody Map<String,Object> getStbsById(@PathVariable String id); @RequestMapping(value="edit/{id}", method = {RequestMethod.GET}) @ResponseBody Map<String,Object> editStb(@PathVariable String id); @RequestMapping(value="id/{id}", method={RequestMethod.DELETE}) @ResponseBody void deleteSingle(@PathVariable String id); @RequestMapping(value="id/{id}", method = {RequestMethod.PUT}) @ResponseBody void add(@PathVariable String id, @RequestBody Map<String,Object> data); @RequestMapping(value="id", method = {RequestMethod.POST}) @ResponseBody Map<String,Object>[] getStbsByIds(@RequestParam String[] id); @RequestMapping(value="update", method = {RequestMethod.POST}) @ResponseBody int update(@RequestParam String[] id, @RequestBody Map<String,Object> data,
            @RequestParam(defaultValue="false") boolean replace); @RequestMapping(value="remotetypes", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8") @ResponseBody List<String> getRemoteTypes(); @SuppressWarnings({"unchecked", "rawtypes" }) @RequestMapping(value="update/tags/{op}", method = {RequestMethod.POST}) @ResponseBody void updateTags(@RequestBody Map<String,Object> payload, @PathVariable("op") String op); @RequestMapping(value="query", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody Map<String,Object>[] getStbsByQuery(@RequestParam String q); @RequestMapping(value = "tag/{tag}", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody Map<String, Object>[] getStbsByTag(@PathVariable String tag, @RequestParam(required = false) String q); @RequestMapping(value="delete", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody void deleteStb(@RequestParam String[] id); @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody int populate(@PathVariable String token,
            @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData); }
HouseRestController { @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody public int populate(@PathVariable String token, @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData) { int counter = 0; try { List<MetaStb> stbs; if(null != client) stbs = client.populate(token); else stbs = new LinkedList<MetaStb>(); String[] ids = new String[stbs.size()]; for(int i = 0; i < stbs.size(); i++) { ids[i] = stbs.get(i).getId(); } Map<String, Object>[] existingStbs = service.getStbsById(ids); Map<String, MetaStb> existingStbMap = new HashMap<String, MetaStb>(); for (Map<String, Object> data : existingStbs) { MetaStb stb = new MetaStb(data); existingStbMap.put(stb.getId(), stb); } for(MetaStb stb : stbs) { MetaStb existing = existingStbMap.get(stb.getId()); MetaStb stbToUpsert; if (existing == null) { stbToUpsert = stb; } else { Map<String, Object> newData = stb.getData(); if (!isToOverrideMetaData) { Set<String> modifiedProps = existing.getModifiedProps(); for (String key : newData.keySet()) { if ((modifiedProps == null) || !modifiedProps.contains(key)) { existing.getData().put(key, newData.get(key)); } } } else { for (String key : newData.keySet()) { existing.getData().put(key, newData.get(key)); } existing.setModifiedProps(null); } stbToUpsert = existing; } try { service.upsertStb(stbToUpsert); counter++; } catch (Exception e) { e.printStackTrace(); logger.warn("Could not load into MetaStb=" + stb.toString() + ", Token="+token); } } } catch (Exception e) { e.printStackTrace(); logger.error("Could not load anything from the PopulatingClient, Token=" + token); } return counter; }  @RequestMapping(value="list*", method = RequestMethod.GET) @ResponseBody Map<String,Object>[] listAll(); @RequestMapping(value="id/{id}", method = {RequestMethod.GET}) @ResponseBody Map<String,Object> getStbsById(@PathVariable String id); @RequestMapping(value="edit/{id}", method = {RequestMethod.GET}) @ResponseBody Map<String,Object> editStb(@PathVariable String id); @RequestMapping(value="id/{id}", method={RequestMethod.DELETE}) @ResponseBody void deleteSingle(@PathVariable String id); @RequestMapping(value="id/{id}", method = {RequestMethod.PUT}) @ResponseBody void add(@PathVariable String id, @RequestBody Map<String,Object> data); @RequestMapping(value="id", method = {RequestMethod.POST}) @ResponseBody Map<String,Object>[] getStbsByIds(@RequestParam String[] id); @RequestMapping(value="update", method = {RequestMethod.POST}) @ResponseBody int update(@RequestParam String[] id, @RequestBody Map<String,Object> data,
            @RequestParam(defaultValue="false") boolean replace); @RequestMapping(value="remotetypes", method = {RequestMethod.GET}, produces = "application/json; charset=utf-8") @ResponseBody List<String> getRemoteTypes(); @SuppressWarnings({"unchecked", "rawtypes" }) @RequestMapping(value="update/tags/{op}", method = {RequestMethod.POST}) @ResponseBody void updateTags(@RequestBody Map<String,Object> payload, @PathVariable("op") String op); @RequestMapping(value="query", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody Map<String,Object>[] getStbsByQuery(@RequestParam String q); @RequestMapping(value = "tag/{tag}", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody Map<String, Object>[] getStbsByTag(@PathVariable String tag, @RequestParam(required = false) String q); @RequestMapping(value="delete", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody void deleteStb(@RequestParam String[] id); @RequestMapping(value="populate/{token}", method=RequestMethod.POST) @ResponseBody int populate(@PathVariable String token,
            @RequestParam(required = false, defaultValue = "false", value = "isToOverride") Boolean isToOverrideMetaData); static final String[] ignoreProps; }