@Test public void testEquivalent() throws ResourceNotFoundException { Sensor newSensor = Mockito.mock(Sensor.class); Map<String, Object> props = new HashMap<>(); props.put("fuelType", "diesel"); props.put("constructionYear", 2015); props.put("engineDisplacement", 1234); props.put("model", "Dito"); props.put("manufacturer", "Vito"); Mockito.when(newSensor.getProperties()).thenReturn(props); SensorDao dao = createDao(); CarSimilarityServiceImpl service = new CarSimilarityServiceImpl(dao); CarSimilarityServiceImpl serviceMock = Mockito.spy(service); serviceMock.setSimilarityDefinition("/car-similarity-test.json"); Sensor equi = serviceMock.resolveEquivalent(newSensor); Assert.assertThat(equi, Matchers.is(oldSensor)); props.put("manufacturer", "vito mobile "); Sensor similar = serviceMock.resolveEquivalent(newSensor); Assert.assertThat(similar, Matchers.is(oldSensor)); Mockito.verify(serviceMock, Mockito.times(1)) .isManufacturerSimilar(Mockito.any(String.class), Mockito.any(String.class)); }
@Test(expected = ResourceNotFoundException.class) public void testNoEquivalent() throws ResourceNotFoundException { Sensor newSensor = Mockito.mock(Sensor.class); Map<String, Object> props = new HashMap<>(); props.put("fuelType", "diesel"); props.put("constructionYear", 2015); props.put("engineDisplacement", 1234); props.put("model", "Wagon"); props.put("manufacturer", "Odd"); Mockito.when(newSensor.getProperties()).thenReturn(props); SensorDao dao = createDao(); CarSimilarityServiceImpl service = new CarSimilarityServiceImpl(dao); CarSimilarityServiceImpl serviceMock = Mockito.spy(service); serviceMock.setSimilarityDefinition("/car-similarity-test.json"); serviceMock.resolveEquivalent(newSensor); }
private SensorDao createDao() { this.oldSensor = Mockito.mock(Sensor.class); Map<String, Object> props = new HashMap<>(); props.put("fuelType", "diesel"); props.put("constructionYear", 2015); props.put("engineDisplacement", 1234); props.put("model", "DiTO "); props.put("manufacturer", "vITO "); String id = "51ffab4fe4b058cd3d654006"; Mockito.when(oldSensor.getIdentifier()).thenReturn(id); Mockito.when(oldSensor.getProperties()).thenReturn(props); SensorDao dao = Mockito.mock(SensorDao.class); Mockito.when(dao.get(Mockito.any(SensorFilter.class))) .thenReturn(Sensors.from(Arrays.asList(new Sensor[] {oldSensor})).build()); Mockito.when(dao.getByIdentifier(id)).thenReturn(oldSensor); return dao; }