@Test public void testRegisterConnectionFailure() { thrown.expect(IllegalArgumentException.class); DatabaseConfig configToRegister = new DatabaseConfig(); configToRegister.setConnectionDriver("org.postgresql.MyCustomDriver"); doAnswer((Answer) invocation -> { MapBindingResult errors = invocation.getArgument(1, MapBindingResult.class); errors.addError(new ObjectError("failed", ERROR_MESSAGE)); return null; }).when(connectionValidator).validate(any(), any()); underTest.register(configToRegister, true); }
public DatabaseConfig register(DatabaseConfig configToSave, boolean test) { if (repository.findByName(configToSave.getName()).isPresent()) { throw new BadRequestException(String.format("database config already exists with name '%s'", configToSave.getName())); } if (configToSave.getConnectionDriver() == null) { configToSave.setConnectionDriver(configToSave.getDatabaseVendor().connectionDriver()); LOGGER.info("Database configuration lacked a connection driver; defaulting to {}", configToSave.getConnectionDriver()); } if (test) { String testResults = testConnection(configToSave); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(configToSave); configToSave.setCreationDate(clock.getCurrentTimeMillis()); Crn crn = crnService.createCrn(configToSave); configToSave.setResourceCrn(crn); configToSave.setAccountId(crn.getAccountId()); return transactionService.required(() -> { DatabaseConfig saved = repository.save(configToSave); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database config registration: ", e); throw new InternalServerErrorException(e); } }
DatabaseConfigService extends AbstractArchivistService<DatabaseConfig> implements ResourceIdProvider, ResourceBasedCrnProvider { public DatabaseConfig register(DatabaseConfig configToSave, boolean test) { if (repository.findByName(configToSave.getName()).isPresent()) { throw new BadRequestException(String.format("database config already exists with name '%s'", configToSave.getName())); } if (configToSave.getConnectionDriver() == null) { configToSave.setConnectionDriver(configToSave.getDatabaseVendor().connectionDriver()); LOGGER.info("Database configuration lacked a connection driver; defaulting to {}", configToSave.getConnectionDriver()); } if (test) { String testResults = testConnection(configToSave); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(configToSave); configToSave.setCreationDate(clock.getCurrentTimeMillis()); Crn crn = crnService.createCrn(configToSave); configToSave.setResourceCrn(crn); configToSave.setAccountId(crn.getAccountId()); return transactionService.required(() -> { DatabaseConfig saved = repository.save(configToSave); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database config registration: ", e); throw new InternalServerErrorException(e); } } }
DatabaseConfigService extends AbstractArchivistService<DatabaseConfig> implements ResourceIdProvider, ResourceBasedCrnProvider { public DatabaseConfig register(DatabaseConfig configToSave, boolean test) { if (repository.findByName(configToSave.getName()).isPresent()) { throw new BadRequestException(String.format("database config already exists with name '%s'", configToSave.getName())); } if (configToSave.getConnectionDriver() == null) { configToSave.setConnectionDriver(configToSave.getDatabaseVendor().connectionDriver()); LOGGER.info("Database configuration lacked a connection driver; defaulting to {}", configToSave.getConnectionDriver()); } if (test) { String testResults = testConnection(configToSave); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(configToSave); configToSave.setCreationDate(clock.getCurrentTimeMillis()); Crn crn = crnService.createCrn(configToSave); configToSave.setResourceCrn(crn); configToSave.setAccountId(crn.getAccountId()); return transactionService.required(() -> { DatabaseConfig saved = repository.save(configToSave); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database config registration: ", e); throw new InternalServerErrorException(e); } }  }
DatabaseConfigService extends AbstractArchivistService<DatabaseConfig> implements ResourceIdProvider, ResourceBasedCrnProvider { public DatabaseConfig register(DatabaseConfig configToSave, boolean test) { if (repository.findByName(configToSave.getName()).isPresent()) { throw new BadRequestException(String.format("database config already exists with name '%s'", configToSave.getName())); } if (configToSave.getConnectionDriver() == null) { configToSave.setConnectionDriver(configToSave.getDatabaseVendor().connectionDriver()); LOGGER.info("Database configuration lacked a connection driver; defaulting to {}", configToSave.getConnectionDriver()); } if (test) { String testResults = testConnection(configToSave); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(configToSave); configToSave.setCreationDate(clock.getCurrentTimeMillis()); Crn crn = crnService.createCrn(configToSave); configToSave.setResourceCrn(crn); configToSave.setAccountId(crn.getAccountId()); return transactionService.required(() -> { DatabaseConfig saved = repository.save(configToSave); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database config registration: ", e); throw new InternalServerErrorException(e); } }  Set<DatabaseConfig> findAll(String environmentCrn); DatabaseConfig register(DatabaseConfig configToSave, boolean test); DatabaseConfig getByCrn(String resourceCrn); DatabaseConfig getByName(String name, String environmentCrn); DatabaseConfig getByName(String name); Set<DatabaseConfig> deleteMultipleByCrn(Set<String> resourceCrns); DatabaseConfig deleteByCrn(String resourceCrn); DatabaseConfig deleteByName(String name, String environmentCrn); String testConnection(String databaseConfigName, String environmentCrn); String testConnection(DatabaseConfig config); @Override DatabaseConfig delete(DatabaseConfig databaseConfig); DatabaseConfig delete(DatabaseConfig databaseConfig, boolean force, boolean skipDeletionOnServer); @Override JpaRepository repository(); @Override Long getResourceIdByResourceCrn(String resourceCrn); @Override Long getResourceIdByResourceName(String resourceName); @Override AuthorizationResourceType getResourceType(); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount(); }
DatabaseConfigService extends AbstractArchivistService<DatabaseConfig> implements ResourceIdProvider, ResourceBasedCrnProvider { public DatabaseConfig register(DatabaseConfig configToSave, boolean test) { if (repository.findByName(configToSave.getName()).isPresent()) { throw new BadRequestException(String.format("database config already exists with name '%s'", configToSave.getName())); } if (configToSave.getConnectionDriver() == null) { configToSave.setConnectionDriver(configToSave.getDatabaseVendor().connectionDriver()); LOGGER.info("Database configuration lacked a connection driver; defaulting to {}", configToSave.getConnectionDriver()); } if (test) { String testResults = testConnection(configToSave); if (!testResults.equals(DATABASE_TEST_RESULT_SUCCESS)) { throw new IllegalArgumentException(testResults); } } try { MDCBuilder.buildMdcContext(configToSave); configToSave.setCreationDate(clock.getCurrentTimeMillis()); Crn crn = crnService.createCrn(configToSave); configToSave.setResourceCrn(crn); configToSave.setAccountId(crn.getAccountId()); return transactionService.required(() -> { DatabaseConfig saved = repository.save(configToSave); grpcUmsClient.assignResourceOwnerRoleIfEntitled(ThreadBasedUserCrnProvider.getUserCrn(), saved.getResourceCrn().toString(), ThreadBasedUserCrnProvider.getAccountId()); return saved; }); } catch (TransactionService.TransactionExecutionException e) { LOGGER.error("Error happened during database config registration: ", e); throw new InternalServerErrorException(e); } }  Set<DatabaseConfig> findAll(String environmentCrn); DatabaseConfig register(DatabaseConfig configToSave, boolean test); DatabaseConfig getByCrn(String resourceCrn); DatabaseConfig getByName(String name, String environmentCrn); DatabaseConfig getByName(String name); Set<DatabaseConfig> deleteMultipleByCrn(Set<String> resourceCrns); DatabaseConfig deleteByCrn(String resourceCrn); DatabaseConfig deleteByName(String name, String environmentCrn); String testConnection(String databaseConfigName, String environmentCrn); String testConnection(DatabaseConfig config); @Override DatabaseConfig delete(DatabaseConfig databaseConfig); DatabaseConfig delete(DatabaseConfig databaseConfig, boolean force, boolean skipDeletionOnServer); @Override JpaRepository repository(); @Override Long getResourceIdByResourceCrn(String resourceCrn); @Override Long getResourceIdByResourceName(String resourceName); @Override AuthorizationResourceType getResourceType(); @Override String getResourceCrnByResourceName(String resourceName); @Override List<String> getResourceCrnListByResourceNameList(List<String> resourceNames); @Override List<String> getResourceCrnsInAccount();  }