@Test public void testUpdateExistingModel() { String idToChange = "000000000001"; String idToChange2 = "000000000003"; String idToNotChange = "000000000002"; String model = "DCT6412"; MockParkService parkService = new MockParkService(); MockHouseService houseService = new MockHouseService(); MetaStb stbShouldChange = createStb(idToChange, "00:00:00:00:00:01", model, "MAC_G", new String[]{"HD", "DVR", "VOD"}, new String[]{}); houseService.upsertStb(stbShouldChange); MetaStb stbShouldChange2 = createStb(idToChange2, "00:00:00:00:00:03", model, new String[]{}); houseService.upsertStb(stbShouldChange2); MetaStb stbShouldNotChange = createStb(idToNotChange, "00:00:00:00:00:02", "DCT6416", new String[]{model}); houseService.upsertStb(stbShouldNotChange); DawgModel existing = createModel(model, "MAC_G", "HD", "DVR"); DawgModel updated = createModel(model, "MAC_S", "HD", "DUAL_TUNER"); parkService.upsertModel(existing); StbModelController controller = new StbModelController(); controller.houseService = houseService; controller.service = parkService; controller.addModel(updated, model); Assert.assertNotNull(parkService.getModelById(model)); MetaStb updatedStb = new MetaStb(houseService.getById(idToChange)); Assert.assertEquals(updatedStb.getFamily(), Family.valueOf("MAC_S")); Assert.assertFalse(updatedStb.getCapabilities().contains(Capability.DVR)); Assert.assertTrue(updatedStb.getCapabilities().contains(Capability.HD)); Assert.assertTrue(updatedStb.getCapabilities().contains(Capability.valueOf("DUAL_TUNER"))); Assert.assertTrue(updatedStb.getCapabilities().contains(Capability.VOD)); MetaStb updatedStb2 = new MetaStb(houseService.getById(idToChange2)); Assert.assertEquals(updatedStb2.getFamily(), Family.valueOf("MAC_S")); Assert.assertEquals(updatedStb2.getCapabilities().size(), 2); Assert.assertTrue(updatedStb2.getCapabilities().contains(Capability.HD)); Assert.assertTrue(updatedStb2.getCapabilities().contains(Capability.valueOf("DUAL_TUNER"))); MetaStb unupdatedStb = new MetaStb(houseService.getById(idToNotChange)); Assert.assertNull(unupdatedStb.getFamily()); Assert.assertTrue(unupdatedStb.getCapabilities().isEmpty()); }
@RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody public boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id) { DawgModel existing = service.getModelById(id); service.upsertModel(newModel); if (existing != null) { List<String> addedCaps = new ArrayList<String>(); List<String> removedCaps = new ArrayList<String>(); List<String> existingCaps = existing.getCapabilities(); List<String> newCaps = newModel.getCapabilities(); for (String cap : newCaps) { if (!existingCaps.contains(cap)) { addedCaps.add(cap); } } for (String cap : existingCaps) { if (!newCaps.contains(cap)) { removedCaps.add(cap); } } Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); for (Map<String, Object> data : datas) { MetaStb stb = new MetaStb(data); if (stb.getModel().name().equals(newModel.getName())) { if (!stb.getData().containsKey(MetaStb.CAPABILITIES)) { stb.setCapabilities(enumerate(newCaps)); } else { Collection<Capability> caps = stb.getCapabilities(); caps.addAll(enumerate(addedCaps)); caps.removeAll(enumerate(removedCaps)); stb.setCapabilities(caps); } stb.setFamily(Family.valueOf(newModel.getFamily())); houseService.upsertStb(stb); } } } else { Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); applyModels(datas, Arrays.asList(newModel)); } return true; }
StbModelController { @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody public boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id) { DawgModel existing = service.getModelById(id); service.upsertModel(newModel); if (existing != null) { List<String> addedCaps = new ArrayList<String>(); List<String> removedCaps = new ArrayList<String>(); List<String> existingCaps = existing.getCapabilities(); List<String> newCaps = newModel.getCapabilities(); for (String cap : newCaps) { if (!existingCaps.contains(cap)) { addedCaps.add(cap); } } for (String cap : existingCaps) { if (!newCaps.contains(cap)) { removedCaps.add(cap); } } Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); for (Map<String, Object> data : datas) { MetaStb stb = new MetaStb(data); if (stb.getModel().name().equals(newModel.getName())) { if (!stb.getData().containsKey(MetaStb.CAPABILITIES)) { stb.setCapabilities(enumerate(newCaps)); } else { Collection<Capability> caps = stb.getCapabilities(); caps.addAll(enumerate(addedCaps)); caps.removeAll(enumerate(removedCaps)); stb.setCapabilities(caps); } stb.setFamily(Family.valueOf(newModel.getFamily())); houseService.upsertStb(stb); } } } else { Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); applyModels(datas, Arrays.asList(newModel)); } return true; } }
StbModelController { @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody public boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id) { DawgModel existing = service.getModelById(id); service.upsertModel(newModel); if (existing != null) { List<String> addedCaps = new ArrayList<String>(); List<String> removedCaps = new ArrayList<String>(); List<String> existingCaps = existing.getCapabilities(); List<String> newCaps = newModel.getCapabilities(); for (String cap : newCaps) { if (!existingCaps.contains(cap)) { addedCaps.add(cap); } } for (String cap : existingCaps) { if (!newCaps.contains(cap)) { removedCaps.add(cap); } } Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); for (Map<String, Object> data : datas) { MetaStb stb = new MetaStb(data); if (stb.getModel().name().equals(newModel.getName())) { if (!stb.getData().containsKey(MetaStb.CAPABILITIES)) { stb.setCapabilities(enumerate(newCaps)); } else { Collection<Capability> caps = stb.getCapabilities(); caps.addAll(enumerate(addedCaps)); caps.removeAll(enumerate(removedCaps)); stb.setCapabilities(caps); } stb.setFamily(Family.valueOf(newModel.getFamily())); houseService.upsertStb(stb); } } } else { Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); applyModels(datas, Arrays.asList(newModel)); } return true; }  }
StbModelController { @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody public boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id) { DawgModel existing = service.getModelById(id); service.upsertModel(newModel); if (existing != null) { List<String> addedCaps = new ArrayList<String>(); List<String> removedCaps = new ArrayList<String>(); List<String> existingCaps = existing.getCapabilities(); List<String> newCaps = newModel.getCapabilities(); for (String cap : newCaps) { if (!existingCaps.contains(cap)) { addedCaps.add(cap); } } for (String cap : existingCaps) { if (!newCaps.contains(cap)) { removedCaps.add(cap); } } Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); for (Map<String, Object> data : datas) { MetaStb stb = new MetaStb(data); if (stb.getModel().name().equals(newModel.getName())) { if (!stb.getData().containsKey(MetaStb.CAPABILITIES)) { stb.setCapabilities(enumerate(newCaps)); } else { Collection<Capability> caps = stb.getCapabilities(); caps.addAll(enumerate(addedCaps)); caps.removeAll(enumerate(removedCaps)); stb.setCapabilities(caps); } stb.setFamily(Family.valueOf(newModel.getFamily())); houseService.upsertStb(stb); } } } else { Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); applyModels(datas, Arrays.asList(newModel)); } return true; }  @RequestMapping(value="models", method=RequestMethod.GET) @ResponseBody List<DawgModel> getModels(@RequestParam String[] id); @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id); @RequestMapping(value="models/{id}", method=RequestMethod.DELETE) @ResponseBody boolean deleteModel(@PathVariable String id); @RequestMapping("/modelsConfig") ModelAndView models(); @RequestMapping("/assignmodels") ModelAndView assignModels(); void applyModels(Map<String, Object>[] stbs, List<DawgModel> models); }
StbModelController { @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody public boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id) { DawgModel existing = service.getModelById(id); service.upsertModel(newModel); if (existing != null) { List<String> addedCaps = new ArrayList<String>(); List<String> removedCaps = new ArrayList<String>(); List<String> existingCaps = existing.getCapabilities(); List<String> newCaps = newModel.getCapabilities(); for (String cap : newCaps) { if (!existingCaps.contains(cap)) { addedCaps.add(cap); } } for (String cap : existingCaps) { if (!newCaps.contains(cap)) { removedCaps.add(cap); } } Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); for (Map<String, Object> data : datas) { MetaStb stb = new MetaStb(data); if (stb.getModel().name().equals(newModel.getName())) { if (!stb.getData().containsKey(MetaStb.CAPABILITIES)) { stb.setCapabilities(enumerate(newCaps)); } else { Collection<Capability> caps = stb.getCapabilities(); caps.addAll(enumerate(addedCaps)); caps.removeAll(enumerate(removedCaps)); stb.setCapabilities(caps); } stb.setFamily(Family.valueOf(newModel.getFamily())); houseService.upsertStb(stb); } } } else { Map<String, Object>[] datas = houseService.getStbsByKey(newModel.getName()); applyModels(datas, Arrays.asList(newModel)); } return true; }  @RequestMapping(value="models", method=RequestMethod.GET) @ResponseBody List<DawgModel> getModels(@RequestParam String[] id); @RequestMapping(value="models/{id}", method=RequestMethod.POST) @ResponseBody boolean addModel(@RequestBody DawgModel newModel, @PathVariable String id); @RequestMapping(value="models/{id}", method=RequestMethod.DELETE) @ResponseBody boolean deleteModel(@PathVariable String id); @RequestMapping("/modelsConfig") ModelAndView models(); @RequestMapping("/assignmodels") ModelAndView assignModels(); void applyModels(Map<String, Object>[] stbs, List<DawgModel> models);  }