Search in sources :

Example 1 with DatabaseConfigService

use of com.sequenceiq.redbeams.service.dbconfig.DatabaseConfigService in project cloudbreak by hortonworks.

the class DatabaseServerConfigServiceTest method testCreateDatabaseOnServer.

@Test
public void testCreateDatabaseOnServer() {
    when(repository.findByResourceCrn(SERVER_CRN)).thenReturn(Optional.of(server));
    when(databaseConfigService.register(any(DatabaseConfig.class), eq(false))).thenAnswer((Answer<DatabaseConfig>) invocation -> {
        return invocation.getArgument(0, DatabaseConfig.class);
    });
    when(userGeneratorService.generateUserName()).thenReturn(USERNAME);
    when(passwordGeneratorService.generatePassword(any())).thenReturn(PASSWORD);
    server.setDatabaseVendor(DatabaseVendor.POSTGRES);
    server.setHost("myhost");
    server.setPort(5432);
    server.setConnectionUserName("root");
    server.setConnectionPassword("rootpassword");
    String databaseName = "mydb";
    String databaseType = "hive";
    Optional<String> databaseDescription = Optional.of("mine not yours");
    String result = underTest.createDatabaseOnServer(SERVER_CRN.toString(), databaseName, databaseType, databaseDescription);
    assertEquals("created", result);
    verify(driverFunctions).execWithDatabaseDriver(eq(server), any());
    ArgumentCaptor<DatabaseConfig> captor = ArgumentCaptor.forClass(DatabaseConfig.class);
    verify(databaseConfigService).register(captor.capture(), eq(false));
    DatabaseConfig db = captor.getValue();
    assertEquals(databaseName, db.getName());
    assertEquals(databaseType, db.getType());
    assertEquals(databaseDescription.get(), db.getDescription());
    String databaseUserName = db.getConnectionUserName().getRaw();
    assertEquals(USERNAME, databaseUserName);
    assertNotEquals(server.getConnectionUserName(), databaseUserName);
    assertEquals(PASSWORD, db.getConnectionPassword().getRaw());
}
Also used : UserGeneratorService(com.sequenceiq.redbeams.service.UserGeneratorService) AbstractArchivistService(com.sequenceiq.cloudbreak.common.archive.AbstractArchivistService) TestData(com.sequenceiq.redbeams.TestData) ThreadBasedUserCrnProvider(com.sequenceiq.cloudbreak.auth.ThreadBasedUserCrnProvider) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) DatabaseServerConfig(com.sequenceiq.redbeams.domain.DatabaseServerConfig) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Is.is(org.hamcrest.core.Is.is) DriverFunctions(com.sequenceiq.redbeams.service.drivers.DriverFunctions) DatabaseVendor(com.sequenceiq.cloudbreak.api.endpoint.v4.common.DatabaseVendor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DatabaseServerConnectionValidator(com.sequenceiq.redbeams.service.validation.DatabaseServerConnectionValidator) Set(java.util.Set) DatabaseConfigService(com.sequenceiq.redbeams.service.dbconfig.DatabaseConfigService) Mockito.doNothing(org.mockito.Mockito.doNothing) UUID(java.util.UUID) Crn(com.sequenceiq.cloudbreak.auth.crn.Crn) DatabaseServerConfigRepository(com.sequenceiq.redbeams.repository.DatabaseServerConfigRepository) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) PasswordGeneratorService(com.sequenceiq.redbeams.service.PasswordGeneratorService) Mockito.any(org.mockito.Mockito.any) DatabaseCommon(com.sequenceiq.cloudbreak.common.database.DatabaseCommon) Mockito.eq(org.mockito.Mockito.eq) DBStack(com.sequenceiq.redbeams.domain.stack.DBStack) Clock(com.sequenceiq.cloudbreak.common.service.Clock) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Errors(org.springframework.validation.Errors) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) OwnerAssignmentService(com.sequenceiq.authorization.service.OwnerAssignmentService) DBStackService(com.sequenceiq.redbeams.service.stack.DBStackService) CrnTestUtil(com.sequenceiq.cloudbreak.auth.crn.CrnTestUtil) Mockito.lenient(org.mockito.Mockito.lenient) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) DatabaseConfig(com.sequenceiq.redbeams.domain.DatabaseConfig) Assert.assertTrue(org.junit.Assert.assertTrue) CrnService(com.sequenceiq.redbeams.service.crn.CrnService) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) Matchers.hasItem(org.hamcrest.Matchers.hasItem) NotFoundException(com.sequenceiq.redbeams.exception.NotFoundException) Collections(java.util.Collections) ResourceStatus(com.sequenceiq.redbeams.api.endpoint.v4.ResourceStatus) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DatabaseConfig(com.sequenceiq.redbeams.domain.DatabaseConfig) Test(org.junit.Test)

Aggregations

OwnerAssignmentService (com.sequenceiq.authorization.service.OwnerAssignmentService)1 DatabaseVendor (com.sequenceiq.cloudbreak.api.endpoint.v4.common.DatabaseVendor)1 ThreadBasedUserCrnProvider (com.sequenceiq.cloudbreak.auth.ThreadBasedUserCrnProvider)1 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)1 CrnTestUtil (com.sequenceiq.cloudbreak.auth.crn.CrnTestUtil)1 AbstractArchivistService (com.sequenceiq.cloudbreak.common.archive.AbstractArchivistService)1 DatabaseCommon (com.sequenceiq.cloudbreak.common.database.DatabaseCommon)1 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)1 Clock (com.sequenceiq.cloudbreak.common.service.Clock)1 TransactionService (com.sequenceiq.cloudbreak.common.service.TransactionService)1 TestData (com.sequenceiq.redbeams.TestData)1 ResourceStatus (com.sequenceiq.redbeams.api.endpoint.v4.ResourceStatus)1 DatabaseConfig (com.sequenceiq.redbeams.domain.DatabaseConfig)1 DatabaseServerConfig (com.sequenceiq.redbeams.domain.DatabaseServerConfig)1 DBStack (com.sequenceiq.redbeams.domain.stack.DBStack)1 NotFoundException (com.sequenceiq.redbeams.exception.NotFoundException)1 DatabaseServerConfigRepository (com.sequenceiq.redbeams.repository.DatabaseServerConfigRepository)1 PasswordGeneratorService (com.sequenceiq.redbeams.service.PasswordGeneratorService)1 UserGeneratorService (com.sequenceiq.redbeams.service.UserGeneratorService)1 CrnService (com.sequenceiq.redbeams.service.crn.CrnService)1