@Test public void testRemoveByEndpointKeyHashAndConfigurationVersion() throws Exception { Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 3); endpointSpecificConfigurationDao.removeByEndpointKeyHashAndConfigurationVersion(KEY, 0); Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 2); Assert.assertTrue(endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 0) == null); }
@After public void tearDown() throws Exception { endpointSpecificConfigurationDao.removeAll(); } }
@Test public void testShouldDeleteActiveConfigurationByEndpointKeyHash() { when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(configurationDto); when(configurationDto.getConfigurationSchemaVersion()).thenReturn(CONF_VERSION); Assert.assertTrue(SERVICE.deleteActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(daoMock).removeByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); }
@Test(expected = KaaOptimisticLockingFailureException.class) public void testLocking() throws Throwable { List<Future<?>> tasks = new ArrayList<>(); for (int i = 0; i < 100; i++) { tasks.add(executorService.submit((Runnable) () -> { endpointSpecificConfigurationDao.save(saved1); })); } for (Future future : tasks) { try { future.get(); } catch (ExecutionException ex) { throw ex.getCause(); } } }
@Override public Optional<EndpointSpecificConfigurationDto> findActiveConfigurationByEndpointKeyHash(byte[] endpointKeyHash) { LOG.debug("Looking for active endpoint specific configuration by EP key hash {}", endpointKeyHash); EndpointProfileDto profileDto = getEndpointProfileDto(endpointKeyHash); if (profileDto == null) { return Optional.empty(); } EndpointSpecificConfiguration configuration = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(endpointKeyHash, profileDto.getConfigurationVersion()); return Optional.ofNullable(configuration).map(ToDto::toDto); }
@Test public void testFindByEndpointKeyHashAndConfigurationVersion() throws Exception { Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 3); EndpointSpecificConfigurationDto found1 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 0).toDto(); EndpointSpecificConfigurationDto found2 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 1).toDto(); EndpointSpecificConfigurationDto found3 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY_2, 0).toDto(); EndpointSpecificConfiguration found4 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY_2, 4); Assert.assertEquals(saved1, found1); Assert.assertEquals(saved2, found2); Assert.assertEquals(saved3, found3); Assert.assertNull(found4); }
@Override public Optional<EndpointSpecificConfigurationDto> deleteByEndpointKeyHashAndConfSchemaVersion(byte[] endpointKeyHash, Integer confSchemaVersion) { Optional<EndpointSpecificConfigurationDto> configuration = findByEndpointKeyHashAndConfSchemaVersion(endpointKeyHash, confSchemaVersion); if (configuration.isPresent()) { endpointSpecificConfigurationDao.removeByEndpointKeyHashAndConfigurationVersion(endpointKeyHash, confSchemaVersion); } return configuration; }
@Test public void testShouldDeleteByEndpointKeyHashAndConfSchemaVersion() { EndpointProfileDto profile = generateProfile(); profile.setConfigurationVersion(0); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(profile); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(configurationDto); when(configurationServiceMock.findConfSchemaByAppIdAndVersion(APP_ID, CONF_VERSION)).thenReturn(new ConfigurationSchemaDto()); when(configurationDto.getConfigurationSchemaVersion()).thenReturn(CONF_VERSION); Assert.assertTrue(SERVICE.deleteByEndpointKeyHashAndConfSchemaVersion(KEY, CONF_VERSION).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(daoMock).removeByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); verify(configurationServiceMock).findConfSchemaByAppIdAndVersion(APP_ID, CONF_VERSION); }
@Test(expected = KaaOptimisticLockingFailureException.class) public void testLocking() throws Throwable { List<Future<?>> tasks = new ArrayList<>(); for (int i = 0; i < 100; i++) { tasks.add(executorService.submit((Runnable) () -> { endpointSpecificConfigurationDao.save(saved1); })); } for (Future future : tasks) { try { future.get(); } catch (ExecutionException ex) { throw ex.getCause(); } } }
@Override public Optional<EndpointSpecificConfigurationDto> findActiveConfigurationByEndpointProfile(EndpointProfileDto endpointProfileDto) { byte[] endpointKeyHash = endpointProfileDto.getEndpointKeyHash(); int configurationVersion = endpointProfileDto.getConfigurationVersion(); EndpointSpecificConfiguration configuration = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(endpointKeyHash, configurationVersion); return Optional.ofNullable(configuration).map(ToDto::toDto); }
@Test public void testFindByEndpointKeyHashAndConfigurationVersion() throws Exception { Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 3); EndpointSpecificConfigurationDto found1 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 0).toDto(); EndpointSpecificConfigurationDto found2 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 1).toDto(); EndpointSpecificConfigurationDto found3 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY_2, 0).toDto(); EndpointSpecificConfiguration found4 = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY_2, 4); Assert.assertEquals(saved1, found1); Assert.assertEquals(saved2, found2); Assert.assertEquals(saved3, found3); Assert.assertNull(found4); }
@Override public Optional<EndpointSpecificConfigurationDto> deleteActiveConfigurationByEndpointKeyHash(byte[] endpointKeyHash) { Optional<EndpointSpecificConfigurationDto> configuration = findActiveConfigurationByEndpointKeyHash(endpointKeyHash); if (configuration.isPresent()) { int confSchemaVersion = configuration.get().getConfigurationSchemaVersion(); endpointSpecificConfigurationDao.removeByEndpointKeyHashAndConfigurationVersion(endpointKeyHash, confSchemaVersion); } return configuration; }
@Test public void testRemoveByEndpointKeyHashAndConfigurationVersion() throws Exception { Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 3); endpointSpecificConfigurationDao.removeByEndpointKeyHashAndConfigurationVersion(KEY, 0); Assert.assertTrue(endpointSpecificConfigurationDao.find().size() == 2); Assert.assertTrue(endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(KEY, 0) == null); }
protected EndpointSpecificConfigurationDto generateEpsConfigurationDto(byte[] endpointKeyHash, Integer configurationVersion, String configuration, Long version) { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(endpointKeyHash, configurationVersion, configuration, version); return endpointSpecificConfigurationDao.save(dto).toDto(); } }
@Override public Optional<EndpointSpecificConfigurationDto> findByEndpointKeyHashAndConfSchemaVersion(byte[] endpointKeyHash, Integer confSchemaVersion) { LOG.debug("Looking for endpoint specific configuration by EP key hash {} and confSchemaVersion", endpointKeyHash, confSchemaVersion); validateNotNull(confSchemaVersion, "Configuration schema version is required"); EndpointProfileDto profileDto = getEndpointProfileDto(endpointKeyHash); if (profileDto == null) { return Optional.empty(); } validateConfSchemaVersion(profileDto.getApplicationId(), confSchemaVersion); EndpointSpecificConfiguration configuration = endpointSpecificConfigurationDao.findByEndpointKeyHashAndConfigurationVersion(endpointKeyHash, confSchemaVersion); return Optional.ofNullable(configuration).map(ToDto::toDto); }
@After public void tearDown() throws Exception { endpointSpecificConfigurationDao.removeAll(); } }
/** * Constructs an endpoint specific configuration with the information provided and * saves it to the database. * * @param endpointKeyHash The endpoint key hash * @param configurationVersion The endpoint configuration version * @param configuration The configuration body * @param version The endpoint specific configuration version * @return saved endpoint specific configuration */ protected EndpointSpecificConfigurationDto generateEndpointSpecificConfigurationDto(byte[] endpointKeyHash, Integer configurationVersion, String configuration, Long version) { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(endpointKeyHash, configurationVersion, configuration, version); return endpointSpecificConfigurationDao.save(dto).toDto(); } }
@Test public void testShouldActiveConfigurationByEndpointProfile() { when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto()); Assert.assertTrue(SERVICE.findActiveConfigurationByEndpointProfile(generateProfile()).isPresent()); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); }
@Test public void testShouldSaveWithActiveConfigSchemaVersion() { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(); dto.setConfigurationSchemaVersion(null); dto.setEndpointKeyHash(KEY); dto.setConfiguration(CONFIG_BODY); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(configurationServiceMock.normalizeAccordingToOverrideConfigurationSchema(APP_ID, CONF_VERSION, CONFIG_BODY)).thenReturn("valid body"); when(daoMock.save(dto)).thenReturn(configuration); when(configuration.toDto()).thenReturn(new EndpointSpecificConfigurationDto()); Assert.assertTrue(SERVICE.save(dto) != null); verify(configurationServiceMock).normalizeAccordingToOverrideConfigurationSchema(APP_ID, CONF_VERSION, CONFIG_BODY); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).save(dto); }
@Test public void testShouldFindActiveConfigurationByEndpointKeyHash() { EndpointSpecificConfigurationDto dto = new EndpointSpecificConfigurationDto(); dto.setConfigurationSchemaVersion(CONF_VERSION); when(endpointServiceMock.findEndpointProfileByKeyHash(KEY)).thenReturn(generateProfile()); when(daoMock.findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION)).thenReturn(configuration); when(configuration.toDto()).thenReturn(dto); Assert.assertTrue(SERVICE.findActiveConfigurationByEndpointKeyHash(KEY).isPresent()); verify(endpointServiceMock).findEndpointProfileByKeyHash(KEY); verify(daoMock).findByEndpointKeyHashAndConfigurationVersion(KEY, CONF_VERSION); }