/** * Test the registration of the platform sensor type. */ @Test public void registerPlatformSensorType() { final long platformSensorId = 20; long platformId = 1; final String fqcName = "class"; when(platformSensorTypeIdentDao.findIdByClassNameAndPlatformId(fqcName, platformId)).thenReturn(Collections.<Long> emptyList()); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { PlatformSensorTypeIdent platformSensorTypeIdent = (PlatformSensorTypeIdent) invocation.getArguments()[0]; platformSensorTypeIdent.setId(Long.valueOf(platformSensorId)); platformSensorTypeIdent.setFullyQualifiedClassName(fqcName); return null; } }).when(platformSensorTypeIdentDao).saveOrUpdate((PlatformSensorTypeIdent) anyObject()); long registeredId = registrationService.registerPlatformSensorTypeIdent(platformId, fqcName); assertThat(registeredId, is(equalTo(platformSensorId))); verify(platformIdentCache, times(1)).markDirty(platformId); ArgumentCaptor<PlatformSensorTypeIdent> platformSensorArgument = ArgumentCaptor.forClass(PlatformSensorTypeIdent.class); verify(platformSensorTypeIdentDao, times(1)).saveOrUpdate(platformSensorArgument.capture()); assertThat(platformSensorArgument.getValue().getFullyQualifiedClassName(), is(equalTo(fqcName))); assertThat(platformSensorArgument.getValue().getPlatformIdent().getId(), is(equalTo(platformId))); verify(platformIdentCache, times(1)).markDirty(platformId); } }
/** * {@inheritDoc} */ @Override @MethodLog public long registerPlatformSensorTypeIdent(long platformId, String fullyQualifiedClassName) { try { List<Long> platformSensorTypeIdentIds = platformSensorTypeIdentDao.findIdByClassNameAndPlatformId(fullyQualifiedClassName, platformId); if (1 == platformSensorTypeIdentIds.size()) { return platformSensorTypeIdentIds.get(0).longValue(); } else { // only if it s not registered we need updating PlatformIdent platformIdent = new PlatformIdent(); platformIdent.setId(platformId); PlatformSensorTypeIdent platformSensorTypeIdent = new PlatformSensorTypeIdent(); platformSensorTypeIdent.setPlatformIdent(platformIdent); platformSensorTypeIdent.setFullyQualifiedClassName(fullyQualifiedClassName); platformSensorTypeIdentDao.saveOrUpdate(platformSensorTypeIdent); return platformSensorTypeIdent.getId(); } } finally { platformIdentCache.markDirty(platformId); } }