SensorParserGroupServiceImpl implements SensorParserGroupService { @Override public SensorParserGroup save(SensorParserGroup sensorParserGroup) throws RestException { ParserConfigurations parserConfigurations = cache.get( ParserConfigurations.class); Map<String, SensorParserGroup> groups = new HashMap<>(parserConfigurations.getSensorParserGroups()); groups.remove(sensorParserGroup.getName()); if (sensorParserGroup.getSensors().size() == 0) { throw new RestException("A parser group must contain sensors"); } for(String sensor: sensorParserGroup.getSensors()) { if (sensorParserConfigService.findOne(sensor) == null) { throw new RestException(String.format("Could not find config for sensor %s", sensor)); } for (SensorParserGroup group : groups.values()) { Set<String> groupSensors = group.getSensors(); if (groupSensors.contains(sensor)) { throw new RestException(String.format("Sensor %s is already in group %s", sensor, group.getName())); } } } groups.put(sensorParserGroup.getName(), sensorParserGroup); saveGroups(parserConfigurations, new HashSet<>(groups.values())); return sensorParserGroup; } @Autowired  SensorParserGroupServiceImpl(ConfigurationsCache cache, GlobalConfigService globalConfigService, SensorParserConfigService sensorParserConfigService); @Override SensorParserGroup save(SensorParserGroup sensorParserGroup); @Override SensorParserGroup findOne(String name); @Override Map<String, SensorParserGroup> getAll(); @Override boolean delete(String name);  }
@Test public void saveShouldThrowExceptionOnMissingSensor() { when(cache.get(ParserConfigurations.class)).thenReturn(new ParserConfigurations()); RestException e = assertThrows(RestException.class, () -> sensorParserGroupService.save(new SensorParserGroup())); assertEquals("A parser group must contain sensors", e.getMessage()); }
@Test public void saveShouldThrowExceptionOnMissingConfig() { when(cache.get(ParserConfigurations.class)).thenReturn(new ParserConfigurations()); SensorParserGroup sensorParserGroup = new SensorParserGroup(); sensorParserGroup.setSensors(Collections.singleton("bro")); RestException e = assertThrows(RestException.class, () -> sensorParserGroupService.save(sensorParserGroup)); assertEquals("Could not find config for sensor bro", e.getMessage()); }