diff --git a/.gitignore b/.gitignore
index 12a7227..eab9f06 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,8 +5,8 @@
.mtj.tmp/
# Package Files #
-*.jar
*.war
+maven-wrapper.jar
*.ear
# Config file
diff --git a/cascades-client/src/main/java/pl/gov/coi/cascades/client/CascadesImpl.java b/cascades-client/src/main/java/pl/gov/coi/cascades/client/CascadesImpl.java
index 106963c..2dbd6d5 100644
--- a/cascades-client/src/main/java/pl/gov/coi/cascades/client/CascadesImpl.java
+++ b/cascades-client/src/main/java/pl/gov/coi/cascades/client/CascadesImpl.java
@@ -70,10 +70,6 @@ public RemoteDatabaseSpec getSpec() {
private RemoteDatabaseRequest createRequest() {
Driver driver = configuration.getDriver();
return new RemoteDatabaseRequest(
- checkNotNull(
- checkNotNull(driver, "20170330:093926").getType(),
- "20170330:093953"
- ).getName(),
driver.getTemplateId().orNull(),
configuration.getInstanceName().orNull()
);
diff --git a/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/domain/Template.java b/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/domain/Template.java
index c61a0c3..889fa1b 100644
--- a/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/domain/Template.java
+++ b/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/domain/Template.java
@@ -15,7 +15,10 @@ public class Template implements Serializable {
private static final long serialVersionUID = 42L;
@Getter
- private final String id;
+ private final Long id;
+
+ @Getter
+ private final String generatedId;
@Getter
private final String name;
diff --git a/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequest.java b/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequest.java
index 2d518ff..196721f 100644
--- a/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequest.java
+++ b/cascades-contract/src/main/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequest.java
@@ -1,7 +1,6 @@
package pl.gov.coi.cascades.contract.service;
import com.google.common.base.Optional;
-import lombok.Getter;
import pl.gov.coi.cascades.contract.domain.Template;
import javax.annotation.Nullable;
@@ -14,8 +13,6 @@ public class RemoteDatabaseRequest implements Serializable {
private static final long serialVersionUID = 42L;
- @Getter
- private final String type;
@Nullable
private final Template template;
@Nullable
@@ -24,14 +21,11 @@ public class RemoteDatabaseRequest implements Serializable {
/**
* Required argument constructor.
*
- * @param type Given type of database (a name or FQCN).
* @param template Given id of template (Optional).
* @param instanceName Given name of instance (Optional).
*/
- public RemoteDatabaseRequest(String type,
- @Nullable Template template,
+ public RemoteDatabaseRequest(@Nullable Template template,
@Nullable String instanceName) {
- this.type = type;
this.template = template;
this.instanceName = instanceName;
}
diff --git a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/configuration/DriverTest.java b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/configuration/DriverTest.java
index 4e4e182..61a10c6 100644
--- a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/configuration/DriverTest.java
+++ b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/configuration/DriverTest.java
@@ -10,7 +10,6 @@
import org.mockito.junit.MockitoRule;
import pl.gov.coi.cascades.contract.domain.DatabaseType;
import pl.gov.coi.cascades.contract.domain.Template;
-import pl.gov.coi.cascades.contract.domain.Template;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -38,7 +37,7 @@ public class DriverTest {
@Before
public void setUp() {
driver = new Driver(
- databaseType,
+ databaseType,
template
);
}
@@ -47,7 +46,7 @@ public void setUp() {
public void testDefaultConstructor() throws Exception {
// when
Driver actual = new Driver(
- databaseType,
+ databaseType,
template
);
diff --git a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/domain/TemplateTest.java b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/domain/TemplateTest.java
index dba803a..6d9e7d8 100644
--- a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/domain/TemplateTest.java
+++ b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/domain/TemplateTest.java
@@ -11,7 +11,8 @@
*/
public class TemplateTest {
- private String id;
+ private Long id;
+ private String generatedId;
private String name;
private String serverId;
private String version;
@@ -20,10 +21,11 @@ public class TemplateTest {
@Before
public void setUp() {
- id = "oracle_template";
+ id = 123L;
name = "hbet6f73";
serverId = "1234";
version = "0.0.1";
+ generatedId = "gw13dqD";
status = TemplateIdStatus.CREATED;
isDefault = true;
}
@@ -33,6 +35,7 @@ public void testToString() {
// given
Template templateBuilder = Template.builder()
.id(id)
+ .generatedId(generatedId)
.name(name)
.isDefault(isDefault)
.serverId(serverId)
@@ -50,7 +53,8 @@ public void testToString() {
serverId,
status.toString(),
version,
- id,
+ id.toString(),
+ generatedId,
name
);
}
@@ -86,6 +90,7 @@ public void testDefaultConstructor() throws Exception {
// when
Template actual = new Template(
id,
+ generatedId,
name,
TemplateIdStatus.CREATED,
true,
@@ -96,6 +101,7 @@ public void testDefaultConstructor() throws Exception {
// then
assertThat(actual).isNotNull();
assertThat(actual.getId()).isEqualTo(id);
+ assertThat(actual.getGeneratedId()).isEqualTo(generatedId);
assertThat(actual.getStatus()).isEqualTo(TemplateIdStatus.CREATED);
assertThat(actual.getServerId()).isEqualTo(serverId);
assertThat(actual.getName()).isEqualTo(name);
diff --git a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequestTest.java b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequestTest.java
index 44cb3bf..c5fd111 100644
--- a/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequestTest.java
+++ b/cascades-contract/src/test/java/pl/gov/coi/cascades/contract/service/RemoteDatabaseRequestTest.java
@@ -22,7 +22,6 @@ public class RemoteDatabaseRequestTest {
private RemoteDatabaseRequest remoteDatabaseRequest;
private String instanceName;
- private String typeClassName;
@Mock
private Template template;
@@ -36,9 +35,7 @@ public class RemoteDatabaseRequestTest {
@Before
public void setUp() {
instanceName = "PESEL";
- typeClassName = "typeClassName";
- remoteDatabaseRequest = new RemoteDatabaseRequest(
- typeClassName,
+ remoteDatabaseRequest = new RemoteDatabaseRequest(
template,
instanceName
);
@@ -48,7 +45,6 @@ public void setUp() {
public void testDefaultConstructor() throws Exception {
// when
RemoteDatabaseRequest actual = new RemoteDatabaseRequest(
- typeClassName,
template,
instanceName
);
diff --git a/cascades-junit4/src/test/java/pl/gov/coi/cascades/junit4/CascadesRuleTest.java b/cascades-junit4/src/test/java/pl/gov/coi/cascades/junit4/CascadesRuleTest.java
index 87911d2..3150991 100644
--- a/cascades-junit4/src/test/java/pl/gov/coi/cascades/junit4/CascadesRuleTest.java
+++ b/cascades-junit4/src/test/java/pl/gov/coi/cascades/junit4/CascadesRuleTest.java
@@ -15,14 +15,16 @@
@Ignore
public class CascadesRuleTest {
- private String id;
+ private long id;
+ private String generatedId;
private String serverId;
private String version;
private String name;
@Before
public void setUp() {
- id = "gw45223";
+ id = 123L;
+ generatedId = "gw45223";
serverId = "898693";
version = "0.0.1";
name = "oracle_template";
@@ -69,6 +71,7 @@ public void testBefore() {
CascadesRule rule = CascadesRule.builder()
.driver(new Driver(DATABASE_TYPE_STUB, new Template(
id,
+ generatedId,
name,
TemplateIdStatus.CREATED,
false,
diff --git a/cascades-server/README.md b/cascades-server/README.md
new file mode 100644
index 0000000..ab5b024
--- /dev/null
+++ b/cascades-server/README.md
@@ -0,0 +1,9 @@
+# Example usage
+
+* Run PostgreSQL server in docker
+`docker run --name cascades-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres:10.4`
+
+* Build project with profile `-Ppostgresql`
+It adds PostgreSQL driver.
+
+* Run application with profile `mvn spring-boot:run --Dspring.profiles.activeproduction,hibernate,postgresqldocker`
diff --git a/cascades-server/app/pom.xml b/cascades-server/app/pom.xml
index 407c666..8e55118 100644
--- a/cascades-server/app/pom.xml
+++ b/cascades-server/app/pom.xml
@@ -12,6 +12,23 @@
Cascades :: Server :: Spring Application
+
+ org.apache.commons
+ commons-io
+ 1.3.2
+
+
+ postgresql
+ postgresql
+ 9.0-801.jdbc4
+
+
+ com.oracle
+ ojdbc
+ 12.1.0.2
+ system
+ ${basedir}/src/main/resources/ojdbc7.jar
+
org.slf4j
slf4j-api
@@ -98,4 +115,17 @@
+
+
+ postgresql
+
+
+ postgresql
+ postgresql
+ 9.0-801.jdbc4
+
+
+
+
+
diff --git a/cascades-server/app/src/main/java/pl/gov/coi/cascades/server/CustomizationBean.java b/cascades-server/app/src/main/java/pl/gov/coi/cascades/server/CustomizationBean.java
new file mode 100644
index 0000000..6e6d507
--- /dev/null
+++ b/cascades-server/app/src/main/java/pl/gov/coi/cascades/server/CustomizationBean.java
@@ -0,0 +1,15 @@
+package pl.gov.coi.cascades.server;
+
+import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
+import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CustomizationBean implements EmbeddedServletContainerCustomizer {
+
+ @Override
+ public void customize(ConfigurableEmbeddedServletContainer container) {
+ container.setPort(8085);
+ // Use port of your choice that is available
+ }
+}
diff --git a/cascades-server/app/src/main/resources/application-development.properties b/cascades-server/app/src/main/resources/application-development.properties
index 51b6055..965a0bd 100644
--- a/cascades-server/app/src/main/resources/application-development.properties
+++ b/cascades-server/app/src/main/resources/application-development.properties
@@ -1 +1,18 @@
spring.datasource.url=jdbc:h2:mem:CASCADES;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
+
+cascades.managed-servers[0].serverId=rgey65getg
+cascades.managed-servers[0].type=pqsql
+cascades.managed-servers[0].dbname=postgres
+cascades.managed-servers[0].user=root
+cascades.managed-servers[0].password=passwrd
+cascades.managed-servers[0].host=172.17.0.3
+cascades.managed-servers[0].port=5432
+
+cascades.managed-servers[1].serverId=dgrt45gtyt
+cascades.managed-servers[1].type=stub
+cascades.managed-servers[1].dbname=postgres
+cascades.managed-servers[1].user=root
+cascades.managed-servers[1].password=passwrd
+cascades.managed-servers[1].host=cascades.example.org
+cascades.managed-servers[1].port=443
+
diff --git a/cascades-server/app/src/main/resources/application-production.properties b/cascades-server/app/src/main/resources/application-production.properties
new file mode 100644
index 0000000..a599e56
--- /dev/null
+++ b/cascades-server/app/src/main/resources/application-production.properties
@@ -0,0 +1,36 @@
+spring.datasource.url=jdbc:h2:mem:CASCADES;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
+
+cascades.managed-servers[0].serverId=rgey65getg
+cascades.managed-servers[0].type=pgsql
+cascades.managed-servers[0].dbname=postgres
+cascades.managed-servers[0].user=root
+cascades.managed-servers[0].password=passwrd
+cascades.managed-servers[0].host=cascades.example.org
+cascades.managed-servers[0].port=553
+
+
+cascades.managed-servers[2].serverId=hdx234rd
+cascades.managed-servers[2].type=pgsql
+cascades.managed-servers[2].dbname=postgres
+cascades.managed-servers[2].user=root
+cascades.managed-servers[2].password=passwrd
+cascades.managed-servers[2].host=172.17.0.3
+cascades.managed-servers[2].port=5432
+
+
+cascades.managed-servers[1].serverId=dgrt45gtyt
+cascades.managed-servers[1].type=pgsql
+cascades.managed-servers[1].dbname=postgres
+cascades.managed-servers[1].user=root
+cascades.managed-servers[1].password=passwrd
+cascades.managed-servers[1].host=cascades.example.org
+cascades.managed-servers[1].port=443
+
+
+cascades.managed-servers[3].serverId=asdq3
+cascades.managed-servers[3].type=ora12c
+cascades.managed-servers[3].dbname=orcl12c
+cascades.managed-servers[3].user=sys as sysdba
+cascades.managed-servers[3].password=oracle
+cascades.managed-servers[3].host=127.0.0.1
+cascades.managed-servers[3].port=1521
diff --git a/cascades-server/app/src/main/resources/ojdbc7.jar b/cascades-server/app/src/main/resources/ojdbc7.jar
new file mode 100644
index 0000000..fd38a6c
Binary files /dev/null and b/cascades-server/app/src/main/resources/ojdbc7.jar differ
diff --git a/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGatewayTestIT.java b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGatewayTestIT.java
new file mode 100644
index 0000000..f41ecf7
--- /dev/null
+++ b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGatewayTestIT.java
@@ -0,0 +1,155 @@
+package pl.gov.coi.cascades.server;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import pl.gov.coi.cascades.contract.domain.DatabaseId;
+import pl.gov.coi.cascades.contract.domain.Template;
+import pl.gov.coi.cascades.contract.domain.TemplateIdStatus;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+import pl.gov.coi.cascades.server.domain.DatabaseStatus;
+import pl.gov.coi.cascades.server.domain.DatabaseTypeImpl;
+import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsImpl;
+
+import javax.inject.Inject;
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+
+/**
+ * @author Łukasz Małek
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest
+@ProductionHibernateTest
+public class GeneralDatabaseOperationGatewayTestIT {
+
+ private static final String TEMPLATE_NAME = "examplee";
+ private static final String SERVER_ID_ORACLE = "asdq3";
+ private static final String SERVER_ID_POSTGRES = "hdx234rd";
+ private static final String ID_DATABASE = "a123xqw2";
+ private static final String DATABASE_NAME = "databasenamee";
+ private static final String MY_DATABASE = "my_database";
+ private static final String FILE_NAME = "tempFile.txt";
+ private static final String USERNAME = "exampleUsernamee";
+ private static final String PASSWORD = "examplePassword";
+ private static final String ORACLE = "ora12c";
+ private static final String POSTGRES = "pgsql";
+
+ private Template template;
+ private ServerConfigurationService serverConfigurationService;
+ private DatabaseManager databaseManager;
+ private GeneralTemplateGateway generalTemplateGateway;
+ private GeneralDatabaseOperationGateway generalDatabaseOperationGateway;
+ private GeneralUserGateway generalUserGateway;
+ private DatabaseInstance databaseInstance;
+
+
+ @Inject
+ public void setServerConfigurationService(ServerConfigurationService serverConfigurationService) {
+ this.serverConfigurationService = serverConfigurationService;
+ }
+
+ @Inject
+ public void setDatabaseManager(DatabaseManager databaseManager) {
+ this.databaseManager = databaseManager;
+ }
+
+ @Rule
+ public TemporaryFolder tempFolder = new TemporaryFolder();
+
+ @Before
+ public void init() {
+ generalUserGateway = new GeneralUserGateway(databaseManager);
+ generalTemplateGateway = new GeneralTemplateGateway(databaseManager);
+ generalDatabaseOperationGateway = new GeneralDatabaseOperationGateway(serverConfigurationService, databaseManager);
+ }
+
+ @After
+ public void afterTest() {
+ generalTemplateGateway.deleteTemplate(template);
+ // TODO: write implementation - remove database
+ generalUserGateway.deleteUser(databaseInstance);
+ }
+
+ //Ignored test because Travis does not have an integrated database. Only for local test.
+ @Ignore
+ @Test
+ public void shouldCreateOracleDatabase() throws IOException {
+ //given
+ File tempFile = tempFolder.newFile(FILE_NAME);
+ FileUtils.writeStringToFile(tempFile, "CREATE TABLE employees (\n" +
+ " emp_id number(38) unique not null,\n" +
+ " name varchar2(32),\n" +
+ " department number not null,\n" +
+ " hire_date date not null);\n" +
+ "\n");
+
+ template = createTemplate(SERVER_ID_ORACLE);
+ databaseInstance = createDatabaseInstance(template, ORACLE);
+
+ //when
+ generalTemplateGateway.createTemplate(template, tempFile.toPath());
+ generalDatabaseOperationGateway.createDatabase(databaseInstance);
+
+ //then
+ generalUserGateway.createUser(databaseInstance);
+ }
+
+ //Ignored test because Travis does not have an integrated database. Only for local test.
+ @Ignore
+ @Test
+ public void shouldCreatePostgresDatabase() throws IOException {
+ //given
+ File tempFile = tempFolder.newFile(FILE_NAME);
+ FileUtils.writeStringToFile(tempFile, "CREATE TABLE COMPANY(\n" +
+ " ID INT PRIMARY KEY NOT NULL,\n" +
+ " NAME TEXT NOT NULL,\n" +
+ " AGE INT NOT NULL,\n" +
+ " ADDRESS CHAR(50),\n" +
+ " SALARY REAL\n" +
+ ");");
+
+ template = createTemplate(SERVER_ID_POSTGRES);
+ databaseInstance = createDatabaseInstance(template, POSTGRES);
+
+ //when
+ generalTemplateGateway.createTemplate(template, tempFile.toPath());
+ generalDatabaseOperationGateway.createDatabase(databaseInstance);
+
+ //then
+ generalUserGateway.createUser(databaseInstance);
+ }
+
+
+ private DatabaseInstance createDatabaseInstance(Template template, String databaseType) {
+ return DatabaseInstance.builder()
+ .databaseId(new DatabaseId(ID_DATABASE))
+ .status(DatabaseStatus.LAUNCHED)
+ .created(new Date())
+ .credentials(new UsernameAndPasswordCredentialsImpl(USERNAME, PASSWORD.toCharArray()))
+ .databaseName(DATABASE_NAME)
+ .databaseType(new DatabaseTypeImpl(databaseType))
+ .instanceName(MY_DATABASE)
+ .networkBind(null)
+ .reuseTimes(1)
+ .template(template)
+ .build();
+ }
+
+ private Template createTemplate(String serverId) {
+ return Template.builder()
+ .name(TEMPLATE_NAME)
+ .serverId(serverId)
+ .isDefault(false)
+ .status(TemplateIdStatus.CREATED)
+ .build();
+ }
+}
diff --git a/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/TemplateGatewayImplFunctionalIT.java b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/TemplateGatewayImplFunctionalIT.java
index 66affb9..87c105b 100644
--- a/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/TemplateGatewayImplFunctionalIT.java
+++ b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/TemplateGatewayImplFunctionalIT.java
@@ -43,23 +43,23 @@ public void shouldExecutePersistNewTemplate() {
//when
templateIdGateway.addTemplate(template);
- Optional result = templateIdGateway.find(template.getId());
+ Optional result = templateIdGateway.find(template.getGeneratedId());
//then
assertNotNull(result);
pl.gov.coi.cascades.contract.domain.Template resultTemplate = result.orElse(null);
assertNotNull(resultTemplate);
- assertEquals(template.getId(), resultTemplate.getId());
- assertEquals(template.getName(), resultTemplate.getName());
- assertEquals(template.getServerId(), resultTemplate.getServerId());
- assertEquals(template.getVersion(), resultTemplate.getVersion());
- assertEquals(template.getStatus(), resultTemplate.getStatus());
+ assertEquals("1", resultTemplate.getGeneratedId());
+ assertEquals("newDatabase", resultTemplate.getName());
+ assertEquals("1234", resultTemplate.getServerId());
+ assertEquals("0.0.1", resultTemplate.getVersion());
+ assertEquals(TemplateIdStatus.CREATED, resultTemplate.getStatus());
}
private pl.gov.coi.cascades.contract.domain.Template createTemplate() {
return pl.gov.coi.cascades.contract.domain.Template.builder()
- .id(ID)
+ .generatedId(ID)
.name(NAME)
.isDefault(IS_DEFAULT)
.serverId(SERVER_ID)
diff --git a/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/FunctionalIT.java b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/FunctionalIT.java
index 9b08b9a..f56d8b3 100644
--- a/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/FunctionalIT.java
+++ b/cascades-server/app/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/FunctionalIT.java
@@ -23,7 +23,6 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import pl.gov.coi.cascades.server.StubDevelopmentTest;
-import pl.gov.coi.cascades.server.persistance.stub.DatabaseTypeStub;
import pl.gov.coi.cascades.server.persistance.stub.TemplateIdGatewayStub;
import javax.inject.Inject;
@@ -64,7 +63,7 @@ public void setup() {
@After
public void after() {
TemplateIdGatewayStub.getAllTemplates().put(
- TemplateIdGatewayStub.TEMPLATE_ID3.getId(),
+ TemplateIdGatewayStub.TEMPLATE_ID3.getGeneratedId(),
TemplateIdGatewayStub.TEMPLATE_ID3
);
}
@@ -135,16 +134,13 @@ private Description buildDescription(MockHttpServletResponse response)
}
private String requestWithNoTemplateId() throws JSONException {
- DatabaseTypeStub stub = new DatabaseTypeStub();
return new JSONObject()
- .put("type", stub.getName())
+ .put("instanceName", "dfasd")
.toString();
}
private String requestWithTemplateId(String templateId) throws JSONException {
- DatabaseTypeStub stub = new DatabaseTypeStub();
return new JSONObject()
- .put("type", stub.getName())
.put("templateId", templateId)
.toString();
}
diff --git a/cascades-server/configuration/pom.xml b/cascades-server/configuration/pom.xml
index 98e47c4..e35bbf1 100644
--- a/cascades-server/configuration/pom.xml
+++ b/cascades-server/configuration/pom.xml
@@ -13,6 +13,11 @@
Cascades :: Server :: Configuration
+
+ org.apache.commons
+ commons-lang3
+ 3.0
+
org.slf4j
slf4j-api
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfiguration.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfiguration.java
index 7cd08bc..5dab936 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfiguration.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfiguration.java
@@ -12,6 +12,6 @@
@AllArgsConstructor
@RequiredArgsConstructor
class ConnectionConfiguration {
- private String driver;
- private String url;
+ private String driverClass;
+ private String jdbcUrlTemplate;
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfigurator.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfigurator.java
index 605662d..4deea07 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfigurator.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ConnectionConfigurator.java
@@ -8,27 +8,27 @@
*/
public class ConnectionConfigurator {
- ConnectionConfiguration getConnectionConfiguration(ServerDef serverDef) {
- String driver;
- String url;
- switch (serverDef.getType()) {
+ ConnectionConfiguration getConnectionConfiguration(String databaseType) {
+ String driverClass;
+ String jdbdUrlTemplate;
+ switch (databaseType) {
case "ora12c":
- driver = "oracle.jdbc.driver.OracleDriver";
- url = "jdbc:oracle:thin:@//%s:%d/%s";
+ driverClass = "oracle.jdbc.OracleDriver";
+ jdbdUrlTemplate = "jdbc:oracle:thin:@//%s:%d/%s";
break;
case "pgsql":
- driver = "org.postgresql.Driver";
- url = "jdbc:postgresql://%s:%d/%s";
+ driverClass = "org.postgresql.Driver";
+ jdbdUrlTemplate = "jdbc:postgresql://%s:%d/%s";
break;
default:
throw new EidIllegalArgumentException(
"20170728:150904",
- "Given driver hasn't been recognised."
+ String.format("Given database type '%s' hasn't been recognised.", databaseType)
);
}
return new ConnectionConfiguration(
- driver,
- url
+ driverClass,
+ jdbdUrlTemplate
);
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointConfiguration.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointConfiguration.java
index 63dc852..9c9addd 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointConfiguration.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointConfiguration.java
@@ -4,9 +4,10 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTemplateGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
-import java.util.HashMap;
import java.util.Map;
/**
@@ -18,29 +19,30 @@
public class DatabaseEndpointConfiguration {
@Bean
- Map produceDriverManagerDataSource(ServerConfigurationService service,
- ConnectionConfigurator connectionConfigurator) {
- Map managers = new HashMap<>();
- for (ServerDef serverDef : service.getManagedServers()) {
- ConnectionConfiguration configuration = connectionConfigurator.getConnectionConfiguration(serverDef);
- DriverManagerDataSource manager = new DriverManagerDataSource();
- manager.setDriverClassName(configuration.getDriver());
- manager.setUrl(String.format(
- configuration.getUrl(),
- serverDef.getHost(),
- serverDef.getPort(),
- serverDef.getDbname())
- );
- manager.setPassword(serverDef.getPassword());
- manager.setUsername(serverDef.getUser());
- managers.put(serverDef.getServerId(), manager);
- }
- return managers;
+ Map produceDriverManagerDataSource(DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
+ return driverManagerDataSourceHelper.getManagersMap();
}
@Bean
- DatabaseManager produceDatabaseManager(Map driver) {
- return new DatabaseEndpointManager(driver);
+ DriverManagerDataSourceProvider produceDriverManagerDataSourceProvider() {
+ return new DriverManagerDataSourceProviderImpl();
+ }
+
+ @Bean
+ DriverManagerDataSourceHelper produceDriverManagerDataSourceHelper(ConnectionConfigurator connectionConfigurator,
+ ServerConfigurationService serverConfigurationService,
+ DriverManagerDataSourceProvider driverManagerDataSourceProvider) {
+ return new DriverManagerDataSourceHelper(
+ connectionConfigurator,
+ serverConfigurationService,
+ driverManagerDataSourceProvider
+ );
+ }
+
+ @Bean
+ DatabaseManager produceDatabaseManager(Map driverManagerMap,
+ DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
+ return new DatabaseEndpointManager(driverManagerMap, driverManagerDataSourceHelper);
}
@Bean
@@ -48,9 +50,18 @@ DatabaseTemplateGateway produceDatabaseTemplateGateway(DatabaseManager manager)
return new GeneralTemplateGateway(manager);
}
+ @Bean
+ DatabaseUserGateway produceDatabaseUserGateway(DatabaseManager manager) {
+ return new GeneralUserGateway(manager);
+ }
+
@Bean
ConnectionConfigurator produceConnectionConfiguration() {
return new ConnectionConfigurator();
}
+ @Bean
+ DatabaseOperationsGateway produceDatabaseOperationGateway(ServerConfigurationService serverConfigurationService, DatabaseManager databaseManager) {
+ return new GeneralDatabaseOperationGateway(serverConfigurationService, databaseManager);
+ }
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointManager.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointManager.java
index 46a553f..1897060 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointManager.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseEndpointManager.java
@@ -5,7 +5,6 @@
import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException;
import javax.inject.Inject;
-import java.sql.SQLException;
import java.util.Map;
/**
@@ -15,14 +14,17 @@
public class DatabaseEndpointManager implements DatabaseManager {
private final Map managers;
+ private final DriverManagerDataSourceHelper driverManagerDataSourceHelper;
@Inject
- DatabaseEndpointManager(Map managers) {
+ DatabaseEndpointManager(Map managers,
+ DriverManagerDataSourceHelper driverManagerDataSourceHelper) {
this.managers = managers;
+ this.driverManagerDataSourceHelper = driverManagerDataSourceHelper;
}
@Override
- public ConnectionDatabase get(String serverId) throws SQLException {
+ public ConnectionDatabase getConnectionToServer(String serverId) {
DriverManagerDataSource manager = managers.get(serverId);
if (manager == null) {
@@ -38,4 +40,13 @@ public ConnectionDatabase get(String serverId) throws SQLException {
);
}
+ @Override
+ public ConnectionDatabase getConnectionToDatabase(String serverId, String templateName) {
+ DriverManagerDataSource manager = driverManagerDataSourceHelper.getManager(serverId, templateName);
+ return new ConnectionDatabase(
+ new JdbcTemplate(manager),
+ manager.getUrl()
+ );
+ }
+
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseManager.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseManager.java
index cfdf0aa..f31e798 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseManager.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DatabaseManager.java
@@ -8,6 +8,19 @@
*/
public interface DatabaseManager {
- ConnectionDatabase get(String serverId) throws SQLException;
+ /**
+ * Returns connection to database with sepcified serverId.
+ * @param serverId server ID
+ * @return connection to database
+ */
+ ConnectionDatabase getConnectionToServer(String serverId) throws SQLException;
+
+ /**
+ * Returns connection to template in database with sepcified serverId.
+ * @param serverId server ID
+ * @param templateName template name
+ * @return connection to database
+ */
+ ConnectionDatabase getConnectionToDatabase(String serverId, String templateName) throws SQLException;
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceHelper.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceHelper.java
new file mode 100644
index 0000000..b2f4082
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceHelper.java
@@ -0,0 +1,76 @@
+package pl.gov.coi.cascades.server;
+
+import lombok.AllArgsConstructor;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import pl.wavesoftware.eid.exceptions.EidIllegalStateException;
+
+import javax.inject.Inject;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * @author Mariusz Wyszomierski
+ */
+@AllArgsConstructor
+public class DriverManagerDataSourceHelper {
+
+ private final ConnectionConfigurator connectionConfigurator;
+ private final ServerConfigurationService serverConfigurationService;
+ private final DriverManagerDataSourceProvider driverManagerDataSourceProvider;
+
+ /**
+ * Returns connection to database with given database name on server with given serverId
+ * defined in application configuration {@link ServerConfigurationService}.
+ *
+ * @param serverId server ID
+ * @param databaseName database name
+ * @return {@link DriverManagerDataSource}
+ */
+ DriverManagerDataSource getManager(String serverId, String databaseName) {
+ ServerDef serverConfiguration = getServerDef(serverId);
+ ConnectionConfiguration connectionConfiguration = connectionConfigurator.getConnectionConfiguration(serverConfiguration.getType());
+ ServerDef newServerConfiguration = serverConfiguration.getWithNewDatabaseName(databaseName);
+ return createDriverManagerDataSource(connectionConfiguration, newServerConfiguration);
+ }
+
+ /**
+ * Returns map of connections to servers defined in application configuration {@link ServerConfigurationService}.
+ * @return map of connections, serverId is the key
+ */
+ Map getManagersMap() {
+ Map managers = new HashMap<>();
+ for (ServerDef serverDef : serverConfigurationService.getManagedServers()) {
+ ConnectionConfiguration configuration = connectionConfigurator.getConnectionConfiguration(serverDef.getType());
+ DriverManagerDataSource manager = createDriverManagerDataSource(configuration, serverDef);
+ managers.put(serverDef.getServerId(), manager);
+ }
+ return managers;
+ }
+
+ private DriverManagerDataSource createDriverManagerDataSource(ConnectionConfiguration connectionConfiguration,
+ ServerDef serverConfiguration) {
+ DriverManagerDataSource manager = driverManagerDataSourceProvider.produce();
+ manager.setDriverClassName(connectionConfiguration.getDriverClass());
+ manager.setUrl(String.format(
+ connectionConfiguration.getJdbcUrlTemplate(),
+ serverConfiguration.getHost(),
+ serverConfiguration.getPort(),
+ serverConfiguration.getDbname())
+ );
+ manager.setPassword(serverConfiguration.getPassword());
+ manager.setUsername(serverConfiguration.getUser());
+ return manager;
+ }
+
+ private ServerDef getServerDef(String serverId) {
+ Optional matchingServerConfiguration = serverConfigurationService.getManagedServers().stream()
+ .filter(server -> serverId.equals(server.getServerId()))
+ .findFirst();
+ if (!matchingServerConfiguration.isPresent()) {
+ String errorMessage = String.format("There is no configuration for serverId: %s", serverId);
+ throw new EidIllegalStateException("20180625:205951", errorMessage);
+ }
+ return matchingServerConfiguration.get();
+ }
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProvider.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProvider.java
new file mode 100644
index 0000000..e09df0c
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProvider.java
@@ -0,0 +1,15 @@
+package pl.gov.coi.cascades.server;
+
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+/**
+ * @author Mariusz Wyszomierski
+ */
+public interface DriverManagerDataSourceProvider {
+
+ /**
+ * Provides instance of {@link DriverManagerDataSource}.
+ * @return {@link DriverManagerDataSource}
+ */
+ DriverManagerDataSource produce();
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProviderImpl.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProviderImpl.java
new file mode 100644
index 0000000..6c55466
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/DriverManagerDataSourceProviderImpl.java
@@ -0,0 +1,14 @@
+package pl.gov.coi.cascades.server;
+
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+
+/**
+ * @author Mariusz Wyszomierski
+ */
+public class DriverManagerDataSourceProviderImpl implements DriverManagerDataSourceProvider {
+
+ @Override
+ public DriverManagerDataSource produce() {
+ return new DriverManagerDataSource();
+ }
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGateway.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGateway.java
new file mode 100644
index 0000000..59bc9e6
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralDatabaseOperationGateway.java
@@ -0,0 +1,155 @@
+package pl.gov.coi.cascades.server;
+
+import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+import pl.gov.coi.cascades.contract.domain.DatabaseType;
+import pl.gov.coi.cascades.contract.domain.NetworkBind;
+import pl.gov.coi.cascades.contract.domain.Template;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseTypeImpl;
+import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException;
+
+import java.sql.SQLException;
+import java.util.Optional;
+
+/**
+ * @author Łukasz Małek
+ */
+@AllArgsConstructor
+public class GeneralDatabaseOperationGateway implements DatabaseOperationsGateway {
+
+ private static final String ORACLE = "ora12c";
+ private static final String POSTGRESQL = "pgsql";
+
+ private ServerConfigurationService serverConfigurationService;
+ private DatabaseManager databaseManager;
+
+ @Override
+ public DatabaseInstance createDatabase(DatabaseInstance databaseInstance) {
+ Template template = databaseInstance.getTemplate();
+ if (template != null) {
+ DatabaseInstance databaseInstanceWithSettings = databaseInstance
+ .setNetworkBind(getNetworkBind(template))
+ .setDatabaseType(getDatabaseType(template));
+
+ createDatabseInstance(databaseInstanceWithSettings);
+
+ return databaseInstanceWithSettings;
+ }
+
+ throw new EidIllegalArgumentException(
+ "20180706:151316",
+ "Template hasn't been found."
+ );
+ }
+
+ private void createDatabseInstance(DatabaseInstance databaseInstance) {
+ String databaseType = databaseInstance.getDatabaseType().getName();
+ ConnectionDatabase connectionDatabase = createConnectionToServer(databaseInstance.getTemplate());
+
+ if (databaseType.contains(POSTGRESQL)) {
+ String postgresCreateCommands = getPostgresCreateCommands(databaseInstance);
+ runSemicolonSeperatedSQL(connectionDatabase, postgresCreateCommands);
+ } else if (databaseType.contains(ORACLE)) {
+ String oracleCreateCommands = getOracleCreateCommands(databaseInstance);
+ runSemicolonSeperatedSQL(connectionDatabase, oracleCreateCommands);
+
+ } else {
+ throw new EidIllegalArgumentException(
+ "20180711:120816",
+ "Hasn't been found database type."
+ );
+ }
+ }
+
+ private String getPostgresCreateCommands(DatabaseInstance databaseInstance) {
+ String templateName = databaseInstance.getTemplate().getName();
+ String databaseName = databaseInstance.getDatabaseName();
+
+ return String.format("CREATE DATABASE %s TEMPLATE %s",
+ databaseName,
+ templateName
+ );
+ }
+
+ private String getOracleCreateCommands(DatabaseInstance databaseInstance) {
+ String templateName = databaseInstance.getTemplate().getName();
+ String databaseName = databaseInstance.getDatabaseName();
+ return new StringBuilder()
+ .append("ALTER SESSION SET container = CDB$ROOT;")
+ .append(String.format("CREATE PLUGGABLE DATABASE %s from %s",
+ databaseName,
+ templateName))
+ .append(String.format(
+ " file_name_convert = ('/u01/app/oracle/oradata/orcl12c/%s', '/u01/app/oracle/oradata/orcl12c/%s');",
+ templateName,
+ databaseName
+ )).append(String.format(
+ "ALTER PLUGGABLE DATABASE %s OPEN READ WRITE;",
+ databaseName
+ )).toString();
+ }
+
+ @Override
+ @Deprecated
+ public void deleteDatabase(DatabaseInstance databaseInstance) {
+ // TODO: write implementation
+ throw new UnsupportedOperationException("Not yet implemented!");
+ }
+
+ private NetworkBind getNetworkBind(Template template) {
+ ServerDef serverDef = findServerDef(template);
+ String host = serverDef.getHost();
+ int port = serverDef.getPort();
+
+ if (StringUtils.isNotBlank(host) && port != 0) {
+ return new NetworkBindImpl(host, port);
+ }
+
+ throw new EidIllegalArgumentException(
+ "20180628:191916",
+ "Hasn't been found connection settings."
+ );
+ }
+
+ private DatabaseType getDatabaseType(Template template) {
+ ServerDef serverDef = findServerDef(template);
+ if (serverDef != null && StringUtils.isNotBlank(serverDef.getType())) {
+ return new DatabaseTypeImpl(serverDef.getType());
+ }
+
+ throw new EidIllegalArgumentException(
+ "20180706:151716",
+ "Hasn't been found database type."
+ );
+ }
+
+ private ServerDef findServerDef(Template template) {
+ Optional correctServerDef = serverConfigurationService
+ .getManagedServers()
+ .stream()
+ .filter(p -> p.getServerId()
+ .equalsIgnoreCase(template.getServerId())
+ )
+ .findFirst();
+
+ return correctServerDef.orElse(null);
+ }
+
+ private ConnectionDatabase createConnectionToServer(Template template) {
+ try {
+ return databaseManager.getConnectionToServer(template.getServerId());
+ } catch (SQLException e) {
+ throw new EidIllegalArgumentException("20180711:114808", e);
+ }
+ }
+
+ private void runSemicolonSeperatedSQL(ConnectionDatabase connectionDatabase, String createDatabaseCommand) {
+ String[] queries = createDatabaseCommand.split(";");
+ for (String str : queries) {
+ connectionDatabase.getJdbcTemplate().execute(str);
+ }
+ }
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralTemplateGateway.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralTemplateGateway.java
index c7cf476..84a1a5b 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralTemplateGateway.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralTemplateGateway.java
@@ -21,6 +21,8 @@
public class GeneralTemplateGateway implements DatabaseTemplateGateway {
private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(GeneralTemplateGateway.class);
+ private static final String ORACLE = "oracle";
+ private static final String POSTGRESQL = "postgresql";
private Logger logger;
private DatabaseManager databaseManager;
@@ -41,58 +43,72 @@ public class GeneralTemplateGateway implements DatabaseTemplateGateway {
@Override
public void createTemplate(Template template, Path deploySQLScriptPath) {
try {
- ConnectionDatabase connectionDatabase = databaseManager.get(template.getServerId());
- StringBuilder commands = new StringBuilder();
- String script = readFileAsString(deploySQLScriptPath);
-
- if (connectionDatabase.getType().contains("oracle")) {
- commands.append(getOracleStartCommands(template));
- } else if (connectionDatabase.getType().contains("postgresql")) {
- commands.append(getPostgresStartCommands(template));
- }
- String[] queries = commands
- .append(script)
- .toString()
- .split(";");
- for (String str : queries) {
- connectionDatabase.getJdbcTemplate().execute(str);
- }
+ ConnectionDatabase connectionDatabase = databaseManager.getConnectionToServer(template.getServerId());
+ runCreateDatabaseCommand(connectionDatabase, template);
+ ConnectionDatabase connectionToTemplate = databaseManager.getConnectionToDatabase(template.getServerId(), template.getName());
+ runDeployScript(connectionToTemplate, deploySQLScriptPath);
+ runAfterDeployScript(connectionToTemplate, template);
} catch (SQLException e) {
throw new EidIllegalArgumentException("20170711:151221", e);
}
}
+ private void runDeployScript(ConnectionDatabase connectionToTemplate, Path deploySQLScriptPath) {
+ String deployScript = readFileAsString(deploySQLScriptPath);
+ runSemicolonSeperatedSQL(connectionToTemplate, deployScript);
+ }
+
+ private void runAfterDeployScript(ConnectionDatabase connectionToTemplate, Template template) {
+ if (connectionToTemplate.getType().contains(POSTGRESQL)) {
+ runSemicolonSeperatedSQL(connectionToTemplate, String.format(
+ "UPDATE pg_database SET datistemplate = TRUE WHERE datname = '%s';" +
+ "UPDATE pg_database SET datallowconn = FALSE WHERE datname = '%s'",
+ template.getName(), template.getName()
+ ));
+ }
+ }
+
+ private void runCreateDatabaseCommand(ConnectionDatabase connectionDatabase, Template template) {
+ String createDatabaseCommand = "";
+ if (connectionDatabase.getType().contains(ORACLE)) {
+ createDatabaseCommand = getOracleStartCommands(template.getName());
+ } else if (connectionDatabase.getType().contains(POSTGRESQL)) {
+ createDatabaseCommand = getPostgresStartCommands(template.getName());
+ }
+ runSemicolonSeperatedSQL(connectionDatabase, createDatabaseCommand);
+ }
+
+ private void runSemicolonSeperatedSQL(ConnectionDatabase connectionDatabase, String createDatabaseCommand) {
+ String[] queries = createDatabaseCommand.split(";");
+ for (String str : queries) {
+ connectionDatabase.getJdbcTemplate().execute(str);
+ }
+ }
+
@Override
public void deleteTemplate(Template template) {
try {
- ConnectionDatabase connectionDatabase = databaseManager.get(template.getServerId());
+ ConnectionDatabase connectionDatabase = databaseManager.getConnectionToServer(template.getServerId());
StringBuilder commands = new StringBuilder();
- if (connectionDatabase.getType().contains("ora12c")) {
- commands.append(getOracleFinishCommands(template));
- } else if (connectionDatabase.getType().contains("pgsql")) {
- commands.append(getPostgresFinishCommands(template));
+ if (connectionDatabase.getType().contains(ORACLE)) {
+ commands.append(getOracleFinishCommands(template.getName()));
+ } else if (connectionDatabase.getType().contains(POSTGRESQL)) {
+ commands.append(getPostgresFinishCommands(template.getName()));
}
- String[] queries = commands
- .toString()
- .split(";");
- for (String str : queries) {
- connectionDatabase.getJdbcTemplate().execute(str);
- }
+ runSemicolonSeperatedSQL(connectionDatabase, commands
+ .toString());
} catch (SQLException e) {
throw new EidIllegalArgumentException("20170726:135511", e);
}
}
- private static StringBuilder getPostgresFinishCommands(Template template) {
- StringBuilder commands = new StringBuilder();
- String deleteQuery = String.format(
+ private static String getPostgresFinishCommands(String templateName) {
+ return String.format(
"DROP DATABASE %s;",
- template.getName()
+ templateName
);
- commands.append(deleteQuery);
- return commands;
}
private static String readFileAsString(Path deploySQLScriptPath) {
@@ -106,46 +122,42 @@ private static String readFileAsString(Path deploySQLScriptPath) {
return fileData.toString();
}
- private static StringBuilder getOracleStartCommands(Template template) {
- StringBuilder commands = new StringBuilder();
- String createQuery = String.format(
- "CREATE PLUGGABLE DATABASE %s ADMIN USER admin IDENTIFIED BY ksdn#2Hd;",
- template.getName()
- );
- String alterQuery = String.format(
- "ALTER PLUGGABLE DATABASE %s OPEN READ WRITE;",
- template.getName()
- );
- commands.append(createQuery).append(alterQuery);
- return commands;
+ private static String getOracleStartCommands(String templateName) {
+ return new StringBuilder().append("ALTER SESSION SET container = CDB$ROOT;")
+ .append(String.format(
+ "CREATE PLUGGABLE DATABASE %s ADMIN USER admin IDENTIFIED BY ksdn#2Hd",
+ templateName
+ )).append(String.format(
+ " file_name_convert = ('/u01/app/oracle/oradata/orcl12c/pdbseed', '/u01/app/oracle/oradata/orcl12c/%s');",
+ templateName
+ )).append(String.format(
+ "ALTER PLUGGABLE DATABASE %s OPEN READ WRITE;",
+ templateName
+ )).toString();
}
- private static StringBuilder getPostgresStartCommands(Template template) {
- StringBuilder commands = new StringBuilder();
- String createQuery = String.format(
+ private static String getPostgresStartCommands(String templateName) {
+ return String.format(
"CREATE DATABASE %s TEMPLATE template0;",
- template.getName()
+ templateName
);
- commands.append(createQuery);
- return commands;
}
- private static StringBuilder getOracleFinishCommands(Template template) {
- StringBuilder commands = new StringBuilder();
- String disconnectQuery = String.format(
- "ALTER PLUGGABLE DATABASE %s CLOSE IMMEDIATE;",
- template.getName()
- );
- String deleteQuery = String.format(
- "DROP PLUGGABLE DATABASE %s INCLUDING DATAFILES;",
- template.getName()
- );
- commands.append(disconnectQuery).append(deleteQuery);
- return commands;
+ private static String getOracleFinishCommands(String templateName) {
+ return new StringBuilder()
+ .append(String.format(
+ "ALTER PLUGGABLE DATABASE %s CLOSE IMMEDIATE;",
+ templateName
+ )).append(String.format(
+ "DROP PLUGGABLE DATABASE %s INCLUDING DATAFILES;",
+ templateName
+ )).toString();
}
@Override
+ @Deprecated
public boolean canBeRemoved(Template template) {
- return false;
+ //TODO: implement this
+ throw new UnsupportedOperationException();
}
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralUserGateway.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralUserGateway.java
new file mode 100644
index 0000000..f1bd5ef
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/GeneralUserGateway.java
@@ -0,0 +1,171 @@
+package pl.gov.coi.cascades.server;
+
+import lombok.AllArgsConstructor;
+import pl.gov.coi.cascades.contract.domain.Template;
+import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
+import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException;
+
+import java.sql.SQLException;
+
+/**
+ * @author Łukasz Małek
+ */
+@AllArgsConstructor
+public class GeneralUserGateway implements DatabaseUserGateway {
+
+ private static final String ORACLE = "ora12c";
+ private static final String POSTGRESQL = "pgsql";
+ private DatabaseManager databaseManager;
+
+ @Override
+ public void createUser(DatabaseInstance databaseInstance) {
+ String databaseType = databaseInstance.getDatabaseType().getName();
+
+ if (databaseType.contains(POSTGRESQL)) {
+ ConnectionDatabase connectionDatabase = createConnectionToServer(databaseInstance.getTemplate());
+ String postgresCommands = getPostgresCreateUserCommands(databaseInstance);
+ runSemicolonSeperatedSQL(connectionDatabase, postgresCommands);
+
+ } else if (databaseType.contains(ORACLE)) {
+ ConnectionDatabase connectionDatabase = createConnectionToDatabase(databaseInstance);
+ String oracleCommands = getOracleCreateUserCommands(databaseInstance);
+ runSemicolonSeperatedSQL(connectionDatabase, oracleCommands);
+ } else {
+ throw new EidIllegalArgumentException(
+ "20180711:110816",
+ "Hasn't been found database type."
+ );
+ }
+ }
+
+ @Override
+ public void deleteUser(DatabaseInstance databaseInstance) {
+ String databaseType = databaseInstance.getDatabaseType().getName();
+
+ if (databaseType.contains(POSTGRESQL)) {
+ ConnectionDatabase connectionDatabase = createConnectionToServer(databaseInstance.getTemplate());
+ String postgresCommands = getPostgresDeleteUserCommands(
+ databaseInstance.getDatabaseName(),
+ databaseInstance.getCredentials().getUsername()
+ );
+
+ runSemicolonSeperatedSQL(connectionDatabase, postgresCommands);
+
+ } else if (databaseType.contains(ORACLE)) {
+ ConnectionDatabase connectionDatabase = createConnectionToDatabase(databaseInstance);
+ String oracleCommands = getOracleDeleteUserCommands(
+ databaseInstance.getCredentials().getUsername()
+ );
+
+ runSemicolonSeperatedSQL(connectionDatabase, oracleCommands);
+ }
+
+ }
+
+ private ConnectionDatabase createConnectionToServer(Template template) {
+ try {
+ return databaseManager.getConnectionToServer(template.getServerId());
+ } catch (SQLException e) {
+ throw new EidIllegalArgumentException("20180704:102108", e);
+ }
+ }
+
+ private ConnectionDatabase createConnectionToDatabase(DatabaseInstance databaseInstance) {
+ try {
+ return databaseManager.getConnectionToDatabase(databaseInstance.getTemplate().getServerId(), databaseInstance.getDatabaseName());
+ } catch (SQLException e) {
+ throw new EidIllegalArgumentException("20180711:095808", e);
+ }
+ }
+
+ private String getPostgresCreateUserCommands(DatabaseInstance databaseInstance) {
+ StringBuilder command = new StringBuilder();
+ UsernameAndPasswordCredentials credentials = databaseInstance.getCredentials();
+
+ command.append(getPostgresCreateUser(credentials));
+ command.append(getPostgresPermissions(
+ databaseInstance.getDatabaseName(),
+ credentials.getUsername())
+ );
+
+ return command.toString();
+ }
+
+ private String getOracleCreateUserCommands(DatabaseInstance databaseInstance) {
+ StringBuilder command = new StringBuilder();
+ UsernameAndPasswordCredentials credentials = databaseInstance.getCredentials();
+
+ command.append(getOracleCreateUser(credentials));
+ command.append(getOraclePermissions(
+ credentials.getUsername())
+ );
+
+ return command.toString();
+ }
+
+
+ private String getPostgresCreateUser(UsernameAndPasswordCredentials credentials) {
+ return String.format(
+ "CREATE USER %s WITH ENCRYPTED PASSWORD '%s';",
+ credentials.getUsername(),
+ credentials.getPassword()
+ );
+ }
+
+ private String getPostgresPermissions(String databaseName, String username) {
+ return String.format(
+ "GRANT ALL PRIVILEGES ON DATABASE %s TO %s;",
+ databaseName,
+ username
+ );
+ }
+
+ private String getOracleCreateUser(UsernameAndPasswordCredentials credentials) {
+ return String.format(
+ "CREATE USER %s IDENTIFIED BY \"%s\";",
+ credentials.getUsername(),
+ credentials.getPassword()
+ );
+ }
+
+ private String getOraclePermissions(String username) {
+ return String.format(
+ "GRANT DBA TO %s;",
+ username
+ );
+ }
+
+ private String getPostgresDeleteUserCommands(String databaseName, String username) {
+ StringBuilder commands = new StringBuilder();
+ commands.append(String.format(
+ "REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA public FROM %s;",
+ username
+ )).append(String.format(
+ "REVOKE ALL ON DATABASE %s FROM %s;",
+ databaseName,
+ username
+ )).append(String.format(
+ "DROP USER %s;",
+ username
+ ));
+
+ return commands.toString();
+ }
+
+ private String getOracleDeleteUserCommands(String username) {
+ return String.format(
+ "DROP USER %s;",
+ username
+ );
+
+ }
+
+ private void runSemicolonSeperatedSQL(ConnectionDatabase connectionDatabase, String createDatabaseCommand) {
+ String[] queries = createDatabaseCommand.split(";");
+ for (String str : queries) {
+ connectionDatabase.getJdbcTemplate().execute(str);
+ }
+ }
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/NetworkBindImpl.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/NetworkBindImpl.java
new file mode 100644
index 0000000..4802500
--- /dev/null
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/NetworkBindImpl.java
@@ -0,0 +1,16 @@
+package pl.gov.coi.cascades.server;
+
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+import pl.gov.coi.cascades.contract.domain.NetworkBind;
+
+@AllArgsConstructor
+@Getter
+@Setter
+public class NetworkBindImpl implements NetworkBind {
+
+ private String host;
+ private int port;
+}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ServerDef.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ServerDef.java
index cdf717c..0b9dd5e 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ServerDef.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/ServerDef.java
@@ -2,19 +2,15 @@
import lombok.AllArgsConstructor;
import lombok.Data;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import lombok.Setter;
+import lombok.NoArgsConstructor;
/**
* @author Agnieszka Celuch
* @since 26.07.17
*/
@Data
-@Getter
-@Setter
+@NoArgsConstructor
@AllArgsConstructor
-@RequiredArgsConstructor
public class ServerDef {
private String serverId;
private String type;
@@ -23,4 +19,21 @@ public class ServerDef {
private String password;
private String host;
private int port;
+
+ /**
+ * Get new {@link ServerDef} instance with the same data expect database name.
+ * @param newDatabaseName new database name
+ * @return new instance with new database name
+ */
+ public ServerDef getWithNewDatabaseName(String newDatabaseName) {
+ return new ServerDef(
+ this.serverId,
+ this.type,
+ newDatabaseName,
+ this.user,
+ this.password,
+ this.host,
+ this.port
+ );
+ }
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/DomainConfiguration.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/DomainConfiguration.java
index 2eaaa61..afc043c 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/DomainConfiguration.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/DomainConfiguration.java
@@ -36,12 +36,16 @@ UseCase produceLaunchNewDatabaseUseCase(LaunchNewDatabaseGatewayFacade launchNew
LaunchNewDatabaseGatewayFacade produceGateways(TemplateIdGateway templateIdGateway,
UserGateway userGateway,
DatabaseLimitGateway databaseLimitGateway,
- DatabaseInstanceGateway databaseInstanceGateway) {
+ DatabaseInstanceGateway databaseInstanceGateway,
+ DatabaseOperationsGateway databaseOperationsGateway,
+ DatabaseUserGateway databaseUserGateway) {
return new LaunchNewDatabaseGatewayFacade(
templateIdGateway,
userGateway,
databaseLimitGateway,
- databaseInstanceGateway
+ databaseInstanceGateway,
+ databaseOperationsGateway,
+ databaseUserGateway
);
}
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/OsgiDatabaseTypeClassNameService.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/OsgiDatabaseTypeClassNameService.java
index 2846437..882cc1a 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/OsgiDatabaseTypeClassNameService.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/domain/OsgiDatabaseTypeClassNameService.java
@@ -5,6 +5,7 @@
import org.slf4j.LoggerFactory;
import pl.gov.coi.cascades.contract.domain.DatabaseType;
import pl.gov.coi.cascades.server.OsgiBeanLocator;
+import pl.gov.coi.cascades.server.persistance.stub.DatabaseTypeStub;
import pl.wavesoftware.eid.exceptions.Eid;
import javax.inject.Inject;
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/HibernateConfiguration.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/HibernateConfiguration.java
index 092a345..131d00f 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/HibernateConfiguration.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/HibernateConfiguration.java
@@ -37,8 +37,8 @@ DatabaseIdGateway createDatabaseIdGateway(DatabaseInstanceMapper databaseInstanc
}
@Bean
- DatabaseInstanceGateway createDatabaseInstanceGateway() {
- return new DatabaseInstanceGatewayImpl();
+ DatabaseInstanceGateway createDatabaseInstanceGateway(DatabaseInstanceMapper databaseInstanceMapper) {
+ return new DatabaseInstanceGatewayImpl(databaseInstanceMapper);
}
@Bean
diff --git a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/stub/PersistanceStubByDefaultOnDevelopmentAutoConfiguration.java b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/stub/PersistanceStubByDefaultOnDevelopmentAutoConfiguration.java
index 9d5618b..4f4228c 100644
--- a/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/stub/PersistanceStubByDefaultOnDevelopmentAutoConfiguration.java
+++ b/cascades-server/configuration/src/main/java/pl/gov/coi/cascades/server/persistance/stub/PersistanceStubByDefaultOnDevelopmentAutoConfiguration.java
@@ -9,7 +9,9 @@
import pl.gov.coi.cascades.server.domain.DatabaseIdGateway;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTemplateGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.User;
import pl.gov.coi.cascades.server.domain.UserGateway;
@@ -40,6 +42,12 @@ DatabaseTemplateGateway produceDatabaseTemplateGateway() {
return new DatabaseTemplateGatewayStub();
}
+ @ConditionalOnMissingBean
+ @Bean
+ DatabaseUserGateway produceDatabaseUserGateway() {
+ return new DatabaseUserGatewayStub();
+ }
+
@ConditionalOnMissingBean
@Bean
TemplateIdGateway produceTemplateIdGateway() {
@@ -52,6 +60,12 @@ DatabaseInstanceGateway produceDatabaseInstanceGateway() {
return new DatabaseInstanceGatewayStub();
}
+ @ConditionalOnMissingBean
+ @Bean
+ DatabaseOperationsGateway produceDatabaseOperations() {
+ return new DatabaseOperationsStub();
+ }
+
@ConditionalOnMissingBean
@Bean
UserGateway produceUserGateway(@Named(STUB_DATABASE) Map
+
+ org.apache.commons
+ commons-lang3
+ 3.1
+
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseTypeImpl.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseTypeImpl.java
new file mode 100644
index 0000000..6631b9e
--- /dev/null
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseTypeImpl.java
@@ -0,0 +1,60 @@
+package pl.gov.coi.cascades.server.domain;
+
+import lombok.AllArgsConstructor;
+import pl.gov.coi.cascades.contract.domain.ConnectionStringProducer;
+import pl.gov.coi.cascades.contract.domain.DatabaseType;
+import pl.gov.coi.cascades.contract.domain.NetworkBind;
+import pl.wavesoftware.eid.exceptions.EidIllegalArgumentException;
+import pl.wavesoftware.eid.exceptions.EidIllegalStateException;
+
+@AllArgsConstructor
+public class DatabaseTypeImpl implements DatabaseType {
+
+ private static final String ORACLE_DATABASE = "jdbc:oracle:thin:@//%s:%d/%s";
+ private static final String POSTGRES_DATABASE = "jdbc:postgresql://%s:%d/%s";
+ private static final String PGPSQL = "pgpsql";
+ private static final String ORA12C = "ora12c";
+
+ private String name;
+
+ @Override
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public ConnectionStringProducer getConnectionStringProducer() {
+ if (name.equalsIgnoreCase(PGPSQL)) {
+ createProduce(POSTGRES_DATABASE);
+
+ } else if (name.equalsIgnoreCase(ORA12C)) {
+ createProduce(ORACLE_DATABASE);
+ }
+
+ throw new EidIllegalArgumentException(
+ "20180706:154716",
+ String.format("Given database type '%s' hasn't been recognised.", name)
+ );
+ }
+
+ private ConnectionStringProducer createProduce(String databaseType) {
+ new ConnectionStringProducer() {
+ @Override
+ public String produce(NetworkBind bind, String databaseName) {
+ return String.format(
+ databaseType,
+ bind.getHost(),
+ bind.getPort(),
+ databaseName
+ );
+ }
+ };
+
+ throw new EidIllegalStateException(
+ "20180706:154616",
+ "Can't create connection string producer"
+ );
+ }
+}
+
+
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacade.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacade.java
index a89f7c3..49db564 100644
--- a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacade.java
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacade.java
@@ -1,45 +1,31 @@
package pl.gov.coi.cascades.server.domain.launchdatabase;
+import lombok.AllArgsConstructor;
import pl.gov.coi.cascades.contract.domain.Template;
import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.User;
import pl.gov.coi.cascades.server.domain.UserGateway;
-import javax.inject.Inject;
import java.util.Optional;
/**
* @author Agnieszka Celuch
* @since 05.04.17.
*/
+@AllArgsConstructor
public class LaunchNewDatabaseGatewayFacade {
private TemplateIdGateway templateIdGateway;
private UserGateway userGateway;
private DatabaseLimitGateway databaseLimitGateway;
private DatabaseInstanceGateway databaseInstanceGateway;
-
- /**
- * Default parameter constructor.
- *
- * @param templateIdGateway Given gateway of templateId.
- * @param userGateway Given gateway of user.
- * @param databaseLimitGateway Given gateway of database limit.
- * @param databaseInstanceGateway Given gateway of database instance.
- */
- @Inject
- public LaunchNewDatabaseGatewayFacade(TemplateIdGateway templateIdGateway,
- UserGateway userGateway,
- DatabaseLimitGateway databaseLimitGateway,
- DatabaseInstanceGateway databaseInstanceGateway) {
- this.templateIdGateway = templateIdGateway;
- this.userGateway = userGateway;
- this.databaseLimitGateway = databaseLimitGateway;
- this.databaseInstanceGateway = databaseInstanceGateway;
- }
+ private DatabaseOperationsGateway databaseOperationsGateway;
+ private DatabaseUserGateway databaseUserGateway;
Optional findTemplateId(String templateId) {
return templateIdGateway.find(templateId);
@@ -58,7 +44,9 @@ void save(User user) {
}
DatabaseInstance launchDatabase(DatabaseInstance databaseInstance) {
- return databaseInstanceGateway.launchDatabase(databaseInstance);
+ DatabaseInstance databaseInstanceWithSettings = databaseOperationsGateway.createDatabase(databaseInstance);
+ databaseUserGateway.createUser(databaseInstanceWithSettings);
+ return databaseInstanceGateway.save(databaseInstanceWithSettings);
}
DatabaseLimitGateway getDatabaseLimitGateway() {
@@ -68,5 +56,4 @@ DatabaseLimitGateway getDatabaseLimitGateway() {
TemplateIdGateway getTemplateIdGateway() {
return templateIdGateway;
}
-
}
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImpl.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImpl.java
index baf720e..eeb3508 100644
--- a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImpl.java
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImpl.java
@@ -8,7 +8,6 @@
import pl.gov.coi.cascades.server.domain.DatabaseInstance.DatabaseInstanceBuilder;
import pl.gov.coi.cascades.server.domain.DatabaseStatus;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
-import pl.gov.coi.cascades.server.domain.DatabaseTypeDTO;
import pl.gov.coi.cascades.server.domain.User;
import java.sql.Date;
@@ -36,15 +35,12 @@ public void execute(Request request, Response response) {
Optional user = request.getUser() != null
? launchNewDatabaseGatewayFacade.findUser(request.getUser().getUsername())
: Optional.empty();
- DatabaseTypeDTO databaseTypeDTO = databaseTypeClassNameService
- .getDatabaseType(request.getType());
Validator.ValidatorBuilder validatorBuilder = Validator.builder()
.databaseLimitGateway(launchNewDatabaseGatewayFacade.getDatabaseLimitGateway())
.templateIdGateway(launchNewDatabaseGatewayFacade.getTemplateIdGateway())
.request(request)
- .response(response)
- .databaseTypeDTO(databaseTypeDTO);
+ .response(response);
user.ifPresent(validatorBuilder::user);
@@ -65,7 +61,6 @@ private void succeedResponse(Request request,
DatabaseInstanceBuilder candidateBuilder = DatabaseInstance.builder()
.databaseId(newId)
.databaseName(databaseName)
- .databaseType(validator.getDatabaseType())
.created(Date.from(Instant.now()))
.credentials(credentials)
.reuseTimes(0)
@@ -87,7 +82,7 @@ private void succeedResponse(Request request,
}
private static String generateDatabaseName(Request request,
- DatabaseNameGeneratorService databaseNameGeneratorService) {
+ DatabaseNameGeneratorService databaseNameGeneratorService) {
Optional instanceName = request.getInstanceName();
return instanceName.isPresent()
? databaseNameGeneratorService.generate(instanceName.get())
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorService.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorService.java
index d3eaf43..d558513 100644
--- a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorService.java
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorService.java
@@ -1,5 +1,6 @@
package pl.gov.coi.cascades.server.domain.launchdatabase;
+import org.apache.commons.lang3.RandomStringUtils;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
import java.security.SecureRandom;
@@ -9,12 +10,13 @@
public class UsernameAndPasswordCredentialsGeneratorService {
private static final int PASSWORD_LENGTH = 24;
- private static final int USERNAME_LENGTH = 8;
+ private static final int USERNAME_LENGTH_WITHOUT_FIRST_LETTER = 7;
private static final String CHAR_PASSWORD =
"0123456789" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"!@#$%^&*";
+ private static final int COUNT_LETTER = 1;
private Random rand;
/**
@@ -37,7 +39,11 @@ public UsernameAndPasswordCredentials generate() {
private static String generateUsername() {
String uuid = UUID.randomUUID().toString();
- return uuid.substring(0, USERNAME_LENGTH);
+ String randomLetter = RandomStringUtils.randomAlphabetic(COUNT_LETTER);
+ return String.format("%s%s",
+ randomLetter,
+ uuid.substring(0, USERNAME_LENGTH_WITHOUT_FIRST_LETTER)
+ );
}
private char[] generatePassword() {
@@ -48,5 +54,4 @@ private char[] generatePassword() {
}
return stringBuilder.toString().toCharArray();
}
-
}
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/Validator.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/Validator.java
index ec5c169..ec6652a 100644
--- a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/Validator.java
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/launchdatabase/Validator.java
@@ -43,7 +43,6 @@ public boolean validate() {
validateTemplateId();
validateUser();
validateLimitOfDatabases();
- validateDatabaseType();
return response.isSuccessful();
}
diff --git a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/loadtemplate/UseCaseImpl.java b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/loadtemplate/UseCaseImpl.java
index f2866be..ff32811 100644
--- a/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/loadtemplate/UseCaseImpl.java
+++ b/cascades-server/domain/logic/src/main/java/pl/gov/coi/cascades/server/domain/loadtemplate/UseCaseImpl.java
@@ -7,7 +7,9 @@
import lombok.Setter;
import pl.gov.coi.cascades.contract.domain.Template;
import pl.gov.coi.cascades.contract.domain.TemplateIdStatus;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseTemplateGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import java.nio.file.Path;
@@ -49,7 +51,7 @@ private Template createTemplate(MetadataHolder metadataHolder) {
String generatedId = templateIdGeneratorService.generateTemplateId();
return Template.builder()
- .id(generatedId)
+ .generatedId(generatedId)
.version(metadataHolder.getTemplateMetadata().getVersion())
.serverId(metadataHolder.getTemplateMetadata().getServerId())
.isDefault(metadataHolder.getTemplateMetadata().isDefault())
@@ -65,7 +67,7 @@ private void loadTemplate(Template template, Path path) {
private void succeedResponse(Template template, Response response) {
templateIdGateway.addTemplate(template);
- response.setId(template.getId());
+ response.setId(template.getGeneratedId());
response.setName(template.getName());
response.setDefault(template.isDefault());
response.setServerId(template.getServerId());
diff --git a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacadeTest.java b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacadeTest.java
index 0e8b2bd..0dba14d 100644
--- a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacadeTest.java
+++ b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/LaunchNewDatabaseGatewayFacadeTest.java
@@ -6,11 +6,13 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
-import pl.gov.coi.cascades.contract.domain.Template;
+import pl.gov.coi.cascades.contract.domain.NetworkBind;
import pl.gov.coi.cascades.contract.domain.Template;
import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.User;
import pl.gov.coi.cascades.server.domain.UserGateway;
@@ -18,6 +20,8 @@
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
@@ -50,6 +54,12 @@ public class LaunchNewDatabaseGatewayFacadeTest {
@Mock
private DatabaseInstanceGateway databaseInstanceGateway;
+ @Mock
+ private DatabaseUserGateway databaseUserGateway;
+
+ @Mock
+ private DatabaseOperationsGateway databaseOperations;
+
@Mock
private Template template;
@@ -62,7 +72,9 @@ public void setUp() {
templateIdGateway,
userGateway,
databaseLimitGateway,
- databaseInstanceGateway
+ databaseInstanceGateway,
+ databaseOperations,
+ databaseUserGateway
);
}
@@ -118,15 +130,17 @@ public void testSave() throws Exception {
@Test
public void testLaunchDatabase() throws Exception {
- // given
- when(databaseInstanceGateway.launchDatabase(databaseInstance)).thenReturn(databaseInstance);
+ //
+ when(databaseInstanceGateway.save(any(DatabaseInstance.class))).thenReturn(databaseInstance);
+ when(databaseOperations.createDatabase(any(DatabaseInstance.class))).thenReturn(databaseInstance);
// when
DatabaseInstance actual = facade.launchDatabase(databaseInstance);
// then
assertThat(actual).isNotNull();
- verify(databaseInstanceGateway, times(1)).launchDatabase(any(DatabaseInstance.class));
+ verify(databaseInstanceGateway, times(1)).save(any(DatabaseInstance.class));
+ verify(databaseOperations, times(1)).createDatabase(any(DatabaseInstance.class));
}
@Test
diff --git a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImplTest.java b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImplTest.java
index 4fceaed..2b97574 100644
--- a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImplTest.java
+++ b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UseCaseImplTest.java
@@ -6,13 +6,14 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
-import pl.gov.coi.cascades.contract.domain.*;
+import pl.gov.coi.cascades.contract.domain.DatabaseId;
+import pl.gov.coi.cascades.contract.domain.NetworkBind;
import pl.gov.coi.cascades.contract.domain.Template;
+import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
import pl.gov.coi.cascades.contract.service.Violation;
import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseLimitGateway;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
-import pl.gov.coi.cascades.server.domain.DatabaseTypeDTO;
import pl.gov.coi.cascades.server.domain.TemplateIdGateway;
import pl.gov.coi.cascades.server.domain.User;
import pl.gov.coi.cascades.server.persistance.stub.DatabaseIdGatewayStub;
@@ -36,9 +37,6 @@
*/
public class UseCaseImplTest {
- @Mock
- private DatabaseTypeDTO databaseTypeDTO;
-
@Mock
private LaunchNewDatabaseGatewayFacade launchNewDatabaseGatewayFacade;
@@ -72,9 +70,6 @@ public class UseCaseImplTest {
@Mock
private DatabaseLimitGateway databaseLimitGateway;
- @Mock
- private DatabaseType databaseType;
-
@Mock
private UsernameAndPasswordCredentials usernameAndPasswordCredentials;
@@ -100,7 +95,6 @@ public void testExecuteWhenUserIsNotPresent() throws Exception {
databaseIdGeneratorService
);
when(launchNewDatabaseGatewayFacade.findUser(anyString())).thenReturn(Optional.of(user));
- when(databaseTypeClassNameService.getDatabaseType(anyString())).thenReturn(databaseTypeDTO);
when(launchNewDatabaseGatewayFacade.getTemplateIdGateway()).thenReturn(templateIdGateway);
when(templateIdGateway.find(anyString())).thenReturn(Optional.of(id));
when(request.getTemplateId()).thenReturn(Optional.of(templateId));
@@ -122,7 +116,7 @@ public void testExecuteWhenErrorsOccurred() throws Exception {
String message = "Global limit of 0 launched database instances has been reached";
String path = "databaseLimit.globalLimit";
String type = "oracle";
- Template id = new Template("", "", null, false, "", null);
+ Template id = new Template(null,"", "", null, false, "", null);
String templateId = "oracle_template";
ResponseImpl response = new ResponseImpl();
User jrambo = UserGatewayStub.J_RAMBO.addDatabaseInstance(DatabaseIdGatewayStub.INSTANCE1);
@@ -140,10 +134,6 @@ public void testExecuteWhenErrorsOccurred() throws Exception {
when(templateIdGateway.find(anyString())).thenReturn(Optional.of(id));
when(launchNewDatabaseGatewayFacade.getDatabaseLimitGateway()).thenReturn(databaseLimitGateway);
when(databaseLimitGateway.isGlobalLimitExceeded()).thenReturn(true);
- DatabaseTypeDTOExtension databaseTypeDTOStub = new DatabaseTypeDTOExtension(type);
- when(databaseTypeClassNameService.getDatabaseType(anyString())).thenReturn(databaseTypeDTOStub);
- when(databaseTypeDTO.onFail(any())).thenReturn(databaseTypeDTOStub);
- when(databaseTypeDTO.onSuccess(any())).thenReturn(databaseTypeDTOStub);
// when
useCase.execute(
@@ -181,17 +171,12 @@ public void testExecuteWithoutErrors() throws Exception {
databaseTypeClassNameService,
databaseIdGeneratorService
);
- DatabaseTypeDTOExtension databaseTypeDTOStub = new DatabaseTypeDTOExtension(type);
when(launchNewDatabaseGatewayFacade.findUser(anyString())).thenReturn(Optional.of(jrambo));
- when(databaseTypeClassNameService.getDatabaseType(anyString())).thenReturn(databaseTypeDTOStub);
when(launchNewDatabaseGatewayFacade.getTemplateIdGateway()).thenReturn(templateIdGateway);
when(templateIdGateway.find(anyString())).thenReturn(Optional.of(id));
when(request.getTemplateId()).thenReturn(Optional.of(templateId));
when(request.getUser()).thenReturn(jrambo);
- when(request.getType()).thenReturn(type);
when(launchNewDatabaseGatewayFacade.getDatabaseLimitGateway()).thenReturn(databaseLimitGateway);
- when(databaseTypeDTO.onFail(any())).thenReturn(databaseTypeDTOStub);
- when(databaseTypeDTO.onSuccess(any())).thenReturn(databaseTypeDTOStub);
when(request.getInstanceName()).thenReturn(Optional.of(instanceName));
when(databaseNameGeneratorService.generate(anyString())).thenReturn(instanceName);
when(credentialsGeneratorService.generate()).thenReturn(usernameAndPasswordCredentials);
@@ -230,17 +215,12 @@ public void testExecuteWithInstanceNameNotPresent() throws Exception {
databaseTypeClassNameService,
databaseIdGeneratorService
);
- DatabaseTypeDTOExtension databaseTypeDTOStub = new DatabaseTypeDTOExtension(type);
when(launchNewDatabaseGatewayFacade.findUser(anyString())).thenReturn(Optional.of(jrambo));
- when(databaseTypeClassNameService.getDatabaseType(anyString())).thenReturn(databaseTypeDTOStub);
when(launchNewDatabaseGatewayFacade.getTemplateIdGateway()).thenReturn(templateIdGateway);
when(templateIdGateway.find(anyString())).thenReturn(Optional.of(id));
when(request.getTemplateId()).thenReturn(Optional.of(templateId));
when(request.getUser()).thenReturn(jrambo);
- when(request.getType()).thenReturn(type);
when(launchNewDatabaseGatewayFacade.getDatabaseLimitGateway()).thenReturn(databaseLimitGateway);
- when(databaseTypeDTO.onFail(any())).thenReturn(databaseTypeDTOStub);
- when(databaseTypeDTO.onSuccess(any())).thenReturn(databaseTypeDTOStub);
when(request.getInstanceName()).thenReturn(Optional.empty());
when(databaseNameGeneratorService.generate(anyString())).thenReturn(instanceName);
when(credentialsGeneratorService.generate()).thenReturn(usernameAndPasswordCredentials);
@@ -279,23 +259,6 @@ public void testBuilder() throws Exception {
assertThat(useCase).isNotNull();
}
- private static final class DatabaseTypeDTOExtension extends DatabaseTypeDTO {
- private DatabaseTypeDTOExtension(String type) {
- super(new DatabaseType() {
-
- @Override
- public String getName() {
- return type;
- }
-
- @Override
- public ConnectionStringProducer getConnectionStringProducer() {
- return null;
- }
- });
- }
- }
-
private static final class ResponseImpl implements Response {
private final Collection violations = new HashSet<>();
diff --git a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorServiceTest.java b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorServiceTest.java
index 5e06e49..426642e 100644
--- a/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorServiceTest.java
+++ b/cascades-server/domain/logic/src/test/java/pl/gov/coi/cascades/server/domain/launchdatabase/UsernameAndPasswordCredentialsGeneratorServiceTest.java
@@ -1,10 +1,11 @@
package pl.gov.coi.cascades.server.domain.launchdatabase;
+import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
-import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsGeneratorService;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertFalse;
/**
* @author Agnieszka Celuch
@@ -26,4 +27,16 @@ public void testGenerate() throws Exception {
assertThat(actual.getPassword()).hasSize(24);
}
+ @Test
+ public void shouldBeFirstLetterInUsername() {
+ // given
+ UsernameAndPasswordCredentialsGeneratorService credentials = new UsernameAndPasswordCredentialsGeneratorService();
+
+ // when
+ UsernameAndPasswordCredentials actual = credentials.generate();
+
+ // then
+ assertFalse(StringUtils.isNumeric(actual.getUsername().substring(0, 1)));
+ }
+
}
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstance.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstance.java
index a1079b9..ab45265 100644
--- a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstance.java
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstance.java
@@ -3,6 +3,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import lombok.ToString;
import pl.gov.coi.cascades.contract.domain.DatabaseId;
import pl.gov.coi.cascades.contract.domain.DatabaseType;
@@ -10,6 +11,7 @@
import pl.gov.coi.cascades.contract.domain.Template;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
+import javax.xml.crypto.Data;
import java.util.Date;
@ToString
@@ -96,6 +98,26 @@ private DatabaseInstance(DatabaseInstance databaseInstance,
);
}
+ /**
+ * Copy constructor to set database type of database.
+ *
+ * @param databaseInstance Given instance of database to copy.
+ */
+ private DatabaseInstance(DatabaseInstance databaseInstance,
+ DatabaseType databaseType) {
+ this(databaseInstance.getDatabaseId(),
+ databaseInstance.getTemplate(),
+ databaseType,
+ databaseInstance.getInstanceName(),
+ databaseInstance.getReuseTimes(),
+ databaseInstance.getDatabaseName(),
+ databaseInstance.getCredentials(),
+ databaseInstance.getNetworkBind(),
+ databaseInstance.getStatus(),
+ databaseInstance.getCreated()
+ );
+ }
+
/**
* Method gives date of database creation.
*
@@ -115,6 +137,16 @@ public DatabaseInstance setNetworkBind(NetworkBind networkBind) {
return new DatabaseInstance(this, networkBind);
}
+ /**
+ * Setter for database type.
+ *
+ * @param databaseType Given database type.
+ * @return Instance of database.
+ */
+ public DatabaseInstance setDatabaseType(DatabaseType databaseType) {
+ return new DatabaseInstance(this, databaseType);
+ }
+
/**
* Setter for status of database.
*
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceGateway.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceGateway.java
index 8625080..ab6ad2c 100644
--- a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceGateway.java
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceGateway.java
@@ -8,7 +8,7 @@ public interface DatabaseInstanceGateway {
* @param databaseInstance Given instance of database.
* @return Instance of database.
*/
- DatabaseInstance launchDatabase(DatabaseInstance databaseInstance);
+ DatabaseInstance save(DatabaseInstance databaseInstance);
void deleteDatabase(DatabaseInstance databaseInstance);
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseOperationsGateway.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseOperationsGateway.java
new file mode 100644
index 0000000..c4d988a
--- /dev/null
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseOperationsGateway.java
@@ -0,0 +1,20 @@
+package pl.gov.coi.cascades.server.domain;
+
+public interface DatabaseOperationsGateway {
+
+ /**
+ * Create new instance of database.
+ *
+ * @param databaseInstance Given instance of database.
+ * @return DatabaseInstance with connection settings
+ */
+ DatabaseInstance createDatabase(DatabaseInstance databaseInstance);
+
+ /**
+ * Delete database.
+ *
+ * @param databaseInstance Given instance of database.
+ * @return NetworkBind with connection settings
+ */
+ void deleteDatabase(DatabaseInstance databaseInstance);
+}
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseUserGateway.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseUserGateway.java
new file mode 100644
index 0000000..3a651f9
--- /dev/null
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/DatabaseUserGateway.java
@@ -0,0 +1,21 @@
+package pl.gov.coi.cascades.server.domain;
+
+/**
+ * @author Łukasz Małek
+ */
+public interface DatabaseUserGateway {
+
+ /**
+ * Creates a new user for the given username, password and adds permissions.
+ *
+ * @param databaseInstance Given databaseInstance data for create user.
+ */
+ void createUser(DatabaseInstance databaseInstance);
+
+ /**
+ * Deletes user from database.
+ *
+ * @param databaseInstance Given databaseInstance.
+ */
+ void deleteUser(DatabaseInstance databaseInstance);
+}
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/TemplateIdGateway.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/TemplateIdGateway.java
index 52aa0b5..e3f2b48 100644
--- a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/TemplateIdGateway.java
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/TemplateIdGateway.java
@@ -27,5 +27,4 @@ public interface TemplateIdGateway {
* @param template Given templateId to addTemplate.
*/
void addTemplate(Template template);
-
}
diff --git a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/UserGateway.java b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/UserGateway.java
index 2149d68..e37e5bd 100644
--- a/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/UserGateway.java
+++ b/cascades-server/domain/model/src/main/java/pl/gov/coi/cascades/server/domain/UserGateway.java
@@ -15,7 +15,7 @@ public interface UserGateway {
/**
* Method saves given user.
*
- * @param user Given user to save.
+ * @param user Given user to createInstance.
*/
void save(User user);
diff --git a/cascades-server/domain/model/src/test/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceTest.java b/cascades-server/domain/model/src/test/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceTest.java
index 4462c9c..bdbb20b 100644
--- a/cascades-server/domain/model/src/test/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceTest.java
+++ b/cascades-server/domain/model/src/test/java/pl/gov/coi/cascades/server/domain/DatabaseInstanceTest.java
@@ -215,6 +215,5 @@ private class NetworkBindImplTest implements NetworkBind {
@Getter
@Setter
private String host;
-
}
}
diff --git a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStub.java b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStub.java
index c319ac0..77f05c6 100644
--- a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStub.java
+++ b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStub.java
@@ -32,7 +32,7 @@ final class DatabaseInstanceGatewayStub implements DatabaseInstanceGateway {
}
@Override
- public DatabaseInstance launchDatabase(DatabaseInstance databaseInstance) {
+ public DatabaseInstance save(DatabaseInstance databaseInstance) {
DatabaseInstance databaseInstanceCopy = databaseInstance.setNetworkBind(NETWORK_BIND_STUB);
if (logger.isInfoEnabled()) {
logger.info(new Eid("20170419:001122").makeLogMessage(
diff --git a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsStub.java b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsStub.java
new file mode 100644
index 0000000..523fe09
--- /dev/null
+++ b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsStub.java
@@ -0,0 +1,52 @@
+package pl.gov.coi.cascades.server.persistance.stub;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
+import pl.gov.coi.cascades.server.domain.DatabaseOperationsGateway;
+import pl.wavesoftware.eid.exceptions.Eid;
+
+final class DatabaseOperationsStub implements DatabaseOperationsGateway {
+
+ private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(DatabaseOperationsStub.class);
+ private static final int PORT = 4312;
+ private static final String EXAMPLE_HOST_COM = "example@host.com";
+ private Logger logger;
+
+ DatabaseOperationsStub() {
+ this(DEFAULT_LOGGER);
+ }
+
+ @VisibleForTesting
+ DatabaseOperationsStub(Logger logger) {
+ this.logger = logger;
+ }
+
+ @Override
+ public DatabaseInstance createDatabase(DatabaseInstance databaseInstance) {
+ NetworkBindStub networkBindStub = new NetworkBindStub(PORT, EXAMPLE_HOST_COM);
+ DatabaseInstance databaseInstanceSettings = databaseInstance
+ .setNetworkBind(networkBindStub)
+ .setDatabaseType(new DatabaseTypeStub());
+
+ if (logger.isInfoEnabled()) {
+ logger.info(new Eid("20180628:181922").makeLogMessage(
+ "Database has been created. %s",
+ networkBindStub.toString() + databaseInstance.toString()
+ ));
+ }
+ return databaseInstanceSettings;
+ }
+
+ @Override
+ public void deleteDatabase(DatabaseInstance databaseInstance) {
+ if (logger.isInfoEnabled()) {
+ logger.info(new Eid("20180628:182022").makeLogMessage(
+ "Database has been deleted. %s",
+ databaseInstance.toString()
+ ));
+ }
+ }
+}
diff --git a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStub.java b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStub.java
new file mode 100644
index 0000000..342dcca
--- /dev/null
+++ b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStub.java
@@ -0,0 +1,48 @@
+package pl.gov.coi.cascades.server.persistance.stub;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+import pl.gov.coi.cascades.server.domain.DatabaseUserGateway;
+import pl.wavesoftware.eid.exceptions.Eid;
+
+/**
+ * @author Łukasz Małek
+ */
+public class DatabaseUserGatewayStub implements DatabaseUserGateway {
+
+ private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(DatabaseTemplateGatewayStub.class);
+ private Logger logger;
+
+ DatabaseUserGatewayStub() {
+ this(DEFAULT_LOGGER);
+ }
+
+ @VisibleForTesting
+ public DatabaseUserGatewayStub(Logger logger) {
+ this.logger = logger;
+ }
+
+ @Override
+ public void createUser(DatabaseInstance databaseInstance) {
+ if (logger.isInfoEnabled()) {
+ logger.info(new Eid("20180704:094608")
+ .makeLogMessage(
+ "Given user has been successfully created."
+ )
+ );
+ }
+ }
+
+ @Override
+ public void deleteUser(DatabaseInstance databaseInstance) {
+ if (logger.isInfoEnabled()) {
+ logger.info(new Eid("20180704:094708")
+ .makeLogMessage(
+ "Given user has been successfully deleted."
+ )
+ );
+ }
+ }
+}
diff --git a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/TemplateIdGatewayStub.java b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/TemplateIdGatewayStub.java
index a570a67..e44bc60 100644
--- a/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/TemplateIdGatewayStub.java
+++ b/cascades-server/persistance-stub/src/main/java/pl/gov/coi/cascades/server/persistance/stub/TemplateIdGatewayStub.java
@@ -17,6 +17,7 @@ public final class TemplateIdGatewayStub implements TemplateIdGateway {
private static final String VERSION = "0.0.1";
public static final Template TEMPLATE_ID1 = new Template(
+ 123L,
"oracle_template",
"hv5sj5mb",
TemplateIdStatus.CREATED,
@@ -25,6 +26,7 @@ public final class TemplateIdGatewayStub implements TemplateIdGateway {
VERSION
);
public static final Template TEMPLATE_ID2 = new Template(
+ 1234L,
"templateId2",
"hstrn5k7",
TemplateIdStatus.CREATED,
@@ -33,6 +35,7 @@ public final class TemplateIdGatewayStub implements TemplateIdGateway {
VERSION
);
public static final Template TEMPLATE_ID3 = new Template(
+ 12345L,
"templateId3",
"gv62mbka",
TemplateIdStatus.CREATED,
@@ -41,6 +44,7 @@ public final class TemplateIdGatewayStub implements TemplateIdGateway {
VERSION
);
public static final Template TEMPLATE_ID4 = new Template(
+ 123456L,
"templateId4",
"budjw2m7",
TemplateIdStatus.CREATED,
@@ -82,7 +86,7 @@ public static Map getAllTemplates() {
}
public void setTemplate(Template template) {
- templates.put(template.getId(), template);
+ templates.put(template.getGeneratedId(), template);
}
public Template getTemplate(String key) {
diff --git a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStubTest.java b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStubTest.java
index 8053cd0..433c557 100644
--- a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStubTest.java
+++ b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseInstanceGatewayStubTest.java
@@ -36,7 +36,7 @@ public void testLaunchDatabase() throws Exception {
when(logger.isInfoEnabled()).thenReturn(true);
// when
- DatabaseInstance actual = databaseInstanceGatewayStub.launchDatabase(
+ DatabaseInstance actual = databaseInstanceGatewayStub.save(
DatabaseIdGatewayStub.INSTANCE1
);
@@ -56,7 +56,7 @@ public void testLaunchDatabaseWhenLoggerIsNotInfoEnabled() throws Exception {
when(logger.isInfoEnabled()).thenReturn(false);
// when
- DatabaseInstance actual = databaseInstanceGatewayStub.launchDatabase(
+ DatabaseInstance actual = databaseInstanceGatewayStub.save(
DatabaseIdGatewayStub.INSTANCE1
);
diff --git a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsGatewayStubTest.java b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsGatewayStubTest.java
new file mode 100644
index 0000000..7663376
--- /dev/null
+++ b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseOperationsGatewayStubTest.java
@@ -0,0 +1,103 @@
+package pl.gov.coi.cascades.server.persistance.stub;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.slf4j.Logger;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.contains;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author Łukasz Małek
+ */
+public class DatabaseOperationsGatewayStubTest {
+
+ private DatabaseOperationsStub databaseOperationsStub;
+
+ @Mock
+ private Logger logger;
+
+ @Rule
+ public MockitoRule mockitoRule = MockitoJUnit.rule();
+
+ @Before
+ public void init() {
+ databaseOperationsStub = new DatabaseOperationsStub(
+ logger
+ );
+ }
+
+ @Test
+ public void shouldCreateDatabase() {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(true);
+
+ // when
+ DatabaseInstance result = databaseOperationsStub.createDatabase(
+ DatabaseIdGatewayStub.INSTANCE1
+ );
+
+ // then
+ assertThat(result).isNotNull();
+ assertNotNull(result.getNetworkBind());
+ assertThat(result).isNotEqualTo(DatabaseIdGatewayStub.INSTANCE1);
+ assertEquals("stub" ,result.getDatabaseType().getName());
+ verify(logger).info(contains("20180628:181922"));
+ verify(logger).info(contains("Database has been created."));
+ }
+
+ @Test
+ public void shouldCreateDatabaseWhenLoggerIsNotInfoEnabled() throws Exception {
+ //given
+ when(logger.isInfoEnabled()).thenReturn(false);
+
+ //when
+ DatabaseInstance result = databaseOperationsStub.createDatabase(
+ DatabaseIdGatewayStub.INSTANCE1
+ );
+
+ //then
+ assertThat(result).isNotNull();
+ assertThat(result).isNotEqualTo(DatabaseIdGatewayStub.INSTANCE1);
+ verify(logger, times(0)).info(anyString());
+ }
+
+ @Test
+ public void shouldDeleteDatabase() {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(true);
+
+ // when
+ databaseOperationsStub.deleteDatabase(
+ DatabaseIdGatewayStub.INSTANCE1
+ );
+
+ // then
+ verify(logger).info(contains("20180628:182022"));
+ verify(logger).info(contains("Database has been deleted."));
+ }
+
+ @Test
+ public void shouldDeleteDatabaseWhenLoggerIsNotInfoEnabled() throws Exception {
+ when(logger.isInfoEnabled()).thenReturn(false);
+
+ // when
+ databaseOperationsStub.deleteDatabase(
+ DatabaseIdGatewayStub.INSTANCE1
+ );
+
+ // then
+ verify(logger, times(0)).info(anyString());
+ }
+}
diff --git a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStubTest.java b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStubTest.java
new file mode 100644
index 0000000..7833709
--- /dev/null
+++ b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/DatabaseUserGatewayStubTest.java
@@ -0,0 +1,110 @@
+package pl.gov.coi.cascades.server.persistance.stub;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.slf4j.Logger;
+import pl.gov.coi.cascades.server.domain.DatabaseInstance;
+
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.contains;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author Łukasz Małek
+ */
+public class DatabaseUserGatewayStubTest {
+
+ @Mock
+ private DatabaseInstance databaseInstance;
+
+ @Mock
+ private Logger logger;
+
+ @Rule
+ public MockitoRule mockitoRule = MockitoJUnit.rule();
+
+ @Test
+ public void shouldCreateDefaultConstructor() {
+ // when
+ DatabaseUserGatewayStub userGatewayStub = new DatabaseUserGatewayStub();
+
+ // then
+ assertNotNull(userGatewayStub);
+ }
+
+ @Test
+ public void shouldDeleteUserWhenLoggerIsNotInfoEnabled() throws Exception {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(false);
+ DatabaseUserGatewayStub userGatewayStub = new DatabaseUserGatewayStub(
+ logger
+ );
+
+ // when
+ userGatewayStub.deleteUser(
+ databaseInstance
+ );
+
+ // then
+ verify(logger, times(0)).info(anyString());
+ }
+
+ @Test
+ public void shouldCreateUserWhenLoggerIsNotInfoEnabled() throws Exception {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(false);
+ DatabaseUserGatewayStub userGatewayStub = new DatabaseUserGatewayStub(
+ logger
+ );
+
+ // when
+ userGatewayStub.createUser(
+ databaseInstance
+ );
+
+ // then
+ verify(logger, times(0)).info(anyString());
+ }
+
+ @Test
+ public void testCreateTemplate() throws Exception {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(true);
+ DatabaseUserGatewayStub userGatewayStub = new DatabaseUserGatewayStub(
+ logger
+ );
+
+ // when
+ userGatewayStub.createUser(
+ databaseInstance
+ );
+
+ // then
+ verify(logger).info(contains("20180704:094608"));
+ verify(logger).info(contains("Given user has been successfully created."));
+ }
+
+ @Test
+ public void testDeleteTemplate() throws Exception {
+ // given
+ when(logger.isInfoEnabled()).thenReturn(true);
+ DatabaseUserGatewayStub userGatewayStub = new DatabaseUserGatewayStub(
+ logger
+ );
+
+ // when
+ userGatewayStub.deleteUser(
+ databaseInstance
+ );
+
+ // then
+ verify(logger).info(contains("20180704:094708"));
+ verify(logger).info(contains("Given user has been successfully deleted."));
+ }
+}
diff --git a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/TemplateGatewayStubTest.java b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/TemplateGatewayStubTest.java
index a0c3267..33cd3bd 100644
--- a/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/TemplateGatewayStubTest.java
+++ b/cascades-server/persistance-stub/src/test/java/pl/gov/coi/cascades/server/persistance/stub/TemplateGatewayStubTest.java
@@ -22,7 +22,7 @@ public void testFindWhenTemplateIdIsPresent() throws Exception {
TemplateIdGatewayStub templateIdGatewayStub = new TemplateIdGatewayStub();
// when
- Optional actual = templateIdGatewayStub.find(TemplateIdGatewayStub.TEMPLATE_ID2.getId());
+ Optional actual = templateIdGatewayStub.find(TemplateIdGatewayStub.TEMPLATE_ID2.getGeneratedId());
// then
assertThat(actual).isNotEqualTo(Optional.empty());
@@ -44,7 +44,7 @@ public void testGetDefaultTemplateId() throws Exception {
// then
assertThat(actual).isNotEqualTo(Optional.empty());
assertThat(actual.isPresent()).isTrue();
- assertThat(actual.get().getId()).isEqualTo(id);
+ assertThat(actual.get().getGeneratedId()).isEqualTo(id);
assertThat(actual.get().getStatus()).isEqualTo(status);
assertThat(actual.get().isDefault()).isTrue();
assertThat(actual.get().getServerId()).isEqualTo(serverId);
@@ -75,6 +75,7 @@ public void testSetTemplate() throws Exception {
// given
int NUMBER_OF_TEMPLATES = 5;
Template TEMPLATE_ID = new Template(
+ 12L,
"postgres_template",
"vh5n683n",
TemplateIdStatus.CREATED,
@@ -98,7 +99,7 @@ public void testSetTemplate() throws Exception {
TemplateIdGatewayStub.TEMPLATE_ID4,
TEMPLATE_ID
);
- templateIdGatewayStub.removeTemplate(TEMPLATE_ID.getId());
+ templateIdGatewayStub.removeTemplate(TEMPLATE_ID.getGeneratedId());
}
@Test
@@ -107,11 +108,11 @@ public void testGetTemplate() throws Exception {
TemplateIdGatewayStub templateIdGatewayStub = new TemplateIdGatewayStub();
// when
- Template actual = templateIdGatewayStub.getTemplate(TemplateIdGatewayStub.TEMPLATE_ID1.getId());
+ Template actual = templateIdGatewayStub.getTemplate(TemplateIdGatewayStub.TEMPLATE_ID1.getGeneratedId());
// then
assertThat(actual).isNotNull();
- assertThat(actual.getId()).isEqualTo(TemplateIdGatewayStub.TEMPLATE_ID1.getId());
+ assertThat(actual.getGeneratedId()).isEqualTo(TemplateIdGatewayStub.TEMPLATE_ID1.getGeneratedId());
assertThat(actual.getServerId()).isEqualTo(TemplateIdGatewayStub.TEMPLATE_ID1.getServerId());
assertThat(actual.getStatus()).isEqualTo(TemplateIdGatewayStub.TEMPLATE_ID1.getStatus());
}
@@ -138,7 +139,7 @@ public void testRemoveTemplate() throws Exception {
TemplateIdGatewayStub templateIdGatewayStub = new TemplateIdGatewayStub();
// when
- templateIdGatewayStub.removeTemplate(TemplateIdGatewayStub.TEMPLATE_ID1.getId());
+ templateIdGatewayStub.removeTemplate(TemplateIdGatewayStub.TEMPLATE_ID1.getGeneratedId());
Map actual = getAllTemplates();
// then
diff --git a/cascades-server/persistence-hibernate/pom.xml b/cascades-server/persistence-hibernate/pom.xml
index 1e2a5c0..cdcb393 100644
--- a/cascades-server/persistence-hibernate/pom.xml
+++ b/cascades-server/persistence-hibernate/pom.xml
@@ -67,6 +67,11 @@
true
provided
+
+ org.springframework.boot
+ spring-boot-test
+ test
+
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseInstanceGatewayImpl.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseInstanceGatewayImpl.java
index 39c4912..aca92b3 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseInstanceGatewayImpl.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseInstanceGatewayImpl.java
@@ -1,8 +1,14 @@
package pl.gov.coi.cascades.server.persistance.hibernate;
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import pl.gov.coi.cascades.server.domain.DatabaseInstance;
import pl.gov.coi.cascades.server.domain.DatabaseInstanceGateway;
+import pl.gov.coi.cascades.server.persistance.hibernate.mapper.DatabaseInstanceMapper;;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
/**
@@ -11,11 +17,37 @@
@Transactional
public class DatabaseInstanceGatewayImpl implements DatabaseInstanceGateway {
+ private static final Logger DEFAULT_LOGGER = LoggerFactory.getLogger(DatabaseInstanceGatewayImpl.class);
+ private Logger logger;
+ private final DatabaseInstanceMapper databaseInstanceMapper;
+ private EntityManager entityManager;
+
+
+ public DatabaseInstanceGatewayImpl(DatabaseInstanceMapper databaseInstanceMapper) {
+ this(
+ databaseInstanceMapper,
+ DEFAULT_LOGGER
+ );
+ }
+
+ @VisibleForTesting
+ DatabaseInstanceGatewayImpl(DatabaseInstanceMapper databaseInstanceMapper, Logger logger) {
+ this.databaseInstanceMapper = databaseInstanceMapper;
+ this.logger = logger;
+ }
+
+ @PersistenceContext
+ void setEntityManager(EntityManager entityManager) {
+ this.entityManager = entityManager;
+ }
+
@Override
- @Deprecated
- public DatabaseInstance launchDatabase(DatabaseInstance databaseInstance) {
- // TODO: write an implementation
- throw new UnsupportedOperationException("Not yet implemented!");
+ public DatabaseInstance save(DatabaseInstance databaseInstance) {
+ pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance databaseInstanceEntity
+ = databaseInstanceMapper.toHibernateEntity(databaseInstance);
+ entityManager.merge(databaseInstanceEntity);
+
+ return databaseInstance;
}
@Override
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImpl.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImpl.java
index 2b5515a..3cadb8e 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImpl.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImpl.java
@@ -11,25 +11,26 @@
@Transactional
public class DatabaseLimitGatewayImpl implements DatabaseLimitGateway {
+ private static final int USER_LIMIT = 100;
+ private static final boolean PERSONAL_LIMIT = false;
+ private static final boolean GLOBAL_LIMIT = false;
+
@Override
- @Deprecated
public boolean isPersonalLimitExceeded(User user) {
// TODO: write an implementation
- throw new UnsupportedOperationException("Not yet implemented!");
+ return PERSONAL_LIMIT;
}
@Override
- @Deprecated
public int getPersonalLimitPerUser(User user) {
// TODO: write an implementation
- throw new UnsupportedOperationException("Not yet implemented!");
+ return USER_LIMIT;
}
@Override
- @Deprecated
public boolean isGlobalLimitExceeded() {
// TODO: write an implementation
- throw new UnsupportedOperationException("Not yet implemented!");
+ return GLOBAL_LIMIT;
}
@Override
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImpl.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImpl.java
index 0493228..da48926 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImpl.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImpl.java
@@ -76,9 +76,6 @@ public Optional find(@Nullable String us
@Override
public void save(@Nonnull pl.gov.coi.cascades.server.domain.User user) {
User hibernateUser = userMapper.toHibernateEntity(user);
- Long id = hibernateUser.getId();
- User reference = entityManager.getReference(hibernateUser.getClass(), id);
- entityManager.persist(reference);
+ entityManager.merge(hibernateUser);
}
-
}
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/Eaba275Supplier.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/Eaba275Supplier.java
index ac8abb3..07cfd82 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/Eaba275Supplier.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/Eaba275Supplier.java
@@ -17,7 +17,7 @@ public class Eaba275Supplier implements Supplier {
public Template get() {
Template template = new Template();
template.setStatus(TemplateIdStatus.CREATED);
- template.setGeneratedId("dh6nd6483haurh21nt7vhf99");
+ template.setGeneratedId("sdfasdq1234");
template.setName("oracle_template");
template.setServerId("rgey65getg");
template.setDefault(true);
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/F4ab6a58Supplier.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/F4ab6a58Supplier.java
index cfb8b0f..9ccb104 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/F4ab6a58Supplier.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/development/supplier/template/F4ab6a58Supplier.java
@@ -17,7 +17,7 @@ public class F4ab6a58Supplier implements Supplier {
public Template get() {
Template template = new Template();
template.setStatus(TemplateIdStatus.CREATED);
- template.setGeneratedId("h4gg2j953hnajfrt31hfb6hs5");
+ template.setGeneratedId("dasda34");
template.setName("postgres_template");
template.setServerId("dgrt45gtyt");
template.setDefault(false);
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/entity/DatabaseInstance.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/entity/DatabaseInstance.java
index ac45393..cd185a7 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/entity/DatabaseInstance.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/entity/DatabaseInstance.java
@@ -12,7 +12,9 @@
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
+import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
+import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import java.util.Date;
@@ -55,7 +57,6 @@ public class DatabaseInstance {
@Enumerated(EnumType.STRING)
private DatabaseStatus status;
- @Column
private Date created;
}
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapper.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapper.java
index 2ce9601..78c9e0c 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapper.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapper.java
@@ -10,6 +10,7 @@
import pl.gov.coi.cascades.server.domain.DatabaseIdMapper;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
import pl.gov.coi.cascades.server.domain.DatabaseTypeDTO;
+import pl.gov.coi.cascades.server.domain.DatabaseTypeImpl;
import pl.gov.coi.cascades.server.domain.Mapper;
import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsImpl;
import pl.gov.coi.cascades.server.persistance.hibernate.entity.Credentials;
@@ -117,8 +118,8 @@ public pl.gov.coi.cascades.server.domain.DatabaseInstance fromHibernateEntity(@N
DatabaseId databaseId = databaseIdMapper.fromHibernateEntity(databaseInstance.getId());
Template template = templateIdMapper.fromHibernateEntity(databaseInstance.getTemplate());
- DatabaseTypeDTO databaseTypeDTO = databaseTypeClassNameService.getDatabaseType(databaseInstance.getType());
- DatabaseType databaseType = new DtoFetcher(databaseTypeDTO).getDatabaseType();
+
+ DatabaseType databaseType = new DatabaseTypeImpl(databaseInstance.getType());
UsernameAndPasswordCredentials credentials = new UsernameAndPasswordCredentialsImpl(
databaseInstance.getCredentials().getUsername(),
databaseInstance.getCredentials().getPassword().toCharArray()
@@ -157,7 +158,6 @@ private static final class NetworkBindImpl implements pl.gov.coi.cascades.contra
}
@Override
-
public String getHost() {
return host;
}
diff --git a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateIdMapper.java b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateIdMapper.java
index 0d12adf..d9c0319 100644
--- a/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateIdMapper.java
+++ b/cascades-server/persistence-hibernate/src/main/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateIdMapper.java
@@ -16,7 +16,7 @@ public class TemplateIdMapper implements MapperŁukasz Małek
*/
public class DatabaseInstanceGatewayImplTest {
- private DatabaseInstanceGatewayImpl databaseInstanceGateway;
+ @Mock
+ private DatabaseInstanceMapper databaseInstanceMapper;
+
+ @Mock
+ private EntityManager entityManager;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
+ private DatabaseInstanceGatewayImpl databaseInstanceGateway;
+
@Before
public void init() {
- databaseInstanceGateway = new DatabaseInstanceGatewayImpl();
+ databaseInstanceGateway = new DatabaseInstanceGatewayImpl(
+ databaseInstanceMapper
+ );
+ databaseInstanceGateway.setEntityManager(entityManager);
+ }
+
+ @Test
+ public void shouldSaveDatabaseInstance() {
+ //given
+ pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance databaseInstanceEntity
+ = new pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance();
+
+ given(databaseInstanceMapper.toHibernateEntity(any(DatabaseInstance.class))).willReturn(databaseInstanceEntity);
+
+ //when
+ DatabaseInstance result = databaseInstanceGateway.save(getDatabaseInstance());
+
+ //then
+ verify(entityManager).merge(eq(databaseInstanceEntity));
+ assertNotNull(result);
}
@Test(expected = UnsupportedOperationException.class)
- public void shouldLaunchDatabase() {
+ public void shouldDeleteDatabase (){
//when
- databaseInstanceGateway.launchDatabase(getDatabaseInstance());
+ databaseInstanceGateway.deleteDatabase(getDatabaseInstance());
}
@Test(expected = UnsupportedOperationException.class)
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImplTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImplTest.java
index d1116bd..6033e21 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImplTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/DatabaseLimitGatewayImplTest.java
@@ -3,11 +3,11 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import pl.gov.coi.cascades.server.domain.User;
-import pl.gov.coi.cascades.server.persistance.hibernate.mapper.DatabaseInstanceMapper;
+
+import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Łukasz Małek
@@ -18,33 +18,42 @@ public class DatabaseLimitGatewayImplTest {
private static final String ID = "id";
private static final String EMAIL = "email";
+ private DatabaseLimitGatewayImpl databaseLimitGateway;
+
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
- private DatabaseLimitGatewayImpl databaseLimitGateway;
-
@Before
public void init() {
- databaseLimitGateway =
- new DatabaseLimitGatewayImpl();
+ databaseLimitGateway = new DatabaseLimitGatewayImpl();
}
- @Test(expected = UnsupportedOperationException.class)
+
+ @Test
public void shouldBePersonalLimitExceeded() {
//when
- databaseLimitGateway.isPersonalLimitExceeded(getUser());
+ boolean result = databaseLimitGateway.isPersonalLimitExceeded(getUser());
+
+ //then
+ assertThat(result).isFalse();
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void shouldGetPersonalLimitPerUser() {
//when
- databaseLimitGateway.getPersonalLimitPerUser(getUser());
+ int result = databaseLimitGateway.getPersonalLimitPerUser(getUser());
+
+ //then
+ assertThat(result).isEqualTo(100);
}
- @Test(expected = UnsupportedOperationException.class)
+ @Test
public void shouldGlobalLimitExceeded() {
//when
- databaseLimitGateway.isGlobalLimitExceeded();
+ boolean result = databaseLimitGateway.isGlobalLimitExceeded();
+
+ //then
+ assertThat(result).isFalse();
}
@Test(expected = UnsupportedOperationException.class)
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/TemplateGatewayImplTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/TemplateGatewayImplTest.java
index d3baa91..0c6b62a 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/TemplateGatewayImplTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/TemplateGatewayImplTest.java
@@ -78,12 +78,14 @@ public void testGetDefaultTemplateIdWhenExceptionOccurred() throws Exception {
public void testGetDefaultTemplateId() throws Exception {
// given
String name = "123456789";
+ long id = 123L;
TemplateIdStatus status = TemplateIdStatus.CREATED;
String serverId = "hufuiht8t757";
String generatedId = "hfb6n2jg";
String version = "0.0.1";
pl.gov.coi.cascades.server.persistance.hibernate.entity.Template template =
new pl.gov.coi.cascades.server.persistance.hibernate.entity.Template();
+ template.setId(id);
template.setGeneratedId(generatedId);
template.setServerId(serverId);
template.setStatus(status);
@@ -104,6 +106,8 @@ public void testGetDefaultTemplateId() throws Exception {
// then
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getName()).isEqualTo(name);
+ assertThat(actual.get().getGeneratedId()).isEqualTo(generatedId);
+ assertThat(actual.get().getId()).isEqualTo(id);
assertThat(actual.get().getServerId()).isEqualTo(serverId);
assertThat(actual.get().getStatus().name()).isEqualTo(status.name());
assertThat(actual.get().isDefault()).isFalse();
@@ -113,12 +117,14 @@ public void testGetDefaultTemplateId() throws Exception {
public void testFind() throws Exception {
// given
String name = "123456789";
- String id = "gs46h77f";
+ long id = 123;
+ String generatedId = "gs46h77f";
String version = "0.0.1";
pl.gov.coi.cascades.contract.domain.TemplateIdStatus status = pl.gov.coi.cascades.contract.domain.TemplateIdStatus.CREATED;
String serverId = "hufuiht8t757";
Template template = new Template(
id,
+ generatedId,
name,
status,
false,
@@ -143,6 +149,7 @@ public void testFind() throws Exception {
// then
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getId()).isEqualTo(id);
+ assertThat(actual.get().getGeneratedId()).isEqualTo(generatedId);
assertThat(actual.get().getServerId()).isEqualTo(serverId);
assertThat(actual.get().getStatus()).isEqualTo(status);
assertThat(actual.get().isDefault()).isFalse();
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImplTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImplTest.java
index 2a22f3e..b1f3e6e 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImplTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/UserGatewayImplTest.java
@@ -88,8 +88,7 @@ public void testSave() throws Exception {
userGateway.save(user);
// then
- verify(entityManager, times(1)).getReference(any(), anyLong());
- verify(entityManager, times(1)).persist(any());
+ verify(entityManager, times(1)).merge(any());
}
@Test
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapperTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapperTest.java
index 1da4930..83c7f11 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapperTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/DatabaseInstanceMapperTest.java
@@ -11,6 +11,7 @@
import pl.gov.coi.cascades.server.domain.DatabaseStatus;
import pl.gov.coi.cascades.server.domain.DatabaseTypeClassNameService;
import pl.gov.coi.cascades.server.domain.DatabaseTypeDTO;
+import pl.gov.coi.cascades.server.domain.DatabaseTypeImpl;
import pl.gov.coi.cascades.server.persistance.hibernate.entity.Credentials;
import pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance;
import pl.gov.coi.cascades.server.persistance.hibernate.entity.NetworkBind;
@@ -81,6 +82,9 @@ public void testToHibernateEntity() throws Exception {
.isEqualTo(DatabaseIdGatewayStub.INSTANCE1.getTemplate().getName());
assertThat(actual.getTemplate()
.getGeneratedId())
+ .isEqualTo(DatabaseIdGatewayStub.INSTANCE1.getTemplate().getGeneratedId());
+ assertThat(actual.getTemplate()
+ .getId())
.isEqualTo(DatabaseIdGatewayStub.INSTANCE1.getTemplate().getId());
assertThat(actual.getTemplate()
.getStatus().name())
@@ -136,6 +140,7 @@ public void testFromHibernateEntity() throws Exception {
template.setServerId(SERVER_ID);
template.setVersion(VERSION);
template.setId(TEMPLATE_ID);
+ template.setGeneratedId(TEMPLATE_ID_NAME);
template.setGeneratedId(GENERATED_ID);
template.setStatus(TemplateIdStatus.CREATED);
pl.gov.coi.cascades.server.persistance.hibernate.entity.DatabaseInstance hibernateInstance
@@ -157,12 +162,13 @@ public void testFromHibernateEntity() throws Exception {
// then
assertThat(actual).isNotNull();
assertThat(actual.getDatabaseId().getId()).isEqualTo(DATABASE_ID);
- assertThat(actual.getTemplate().getId()).isEqualTo(GENERATED_ID);
+ assertThat(actual.getTemplate().getId()).isEqualTo(TEMPLATE_ID);
+ assertThat(actual.getTemplate().getGeneratedId()).isEqualTo(GENERATED_ID);
assertThat(actual.getTemplate().isDefault()).isEqualTo(template.isDefault());
assertThat(actual.getTemplate().getServerId()).isEqualTo(template.getServerId());
assertThat(actual.getTemplate().getStatus().name()).isEqualTo(template.getStatus().name());
assertThat(actual.getTemplate().getVersion()).isEqualTo(template.getVersion());
- assertThat(actual.getDatabaseType()).isEqualTo(null);
+ assertThat(actual.getDatabaseType()).isInstanceOfAny(DatabaseTypeImpl.class);
assertThat(actual.getInstanceName()).isEqualTo(INSTANCE_NAME);
assertThat(actual.getReuseTimes()).isEqualTo(1);
assertThat(actual.getDatabaseName()).isEqualTo(DATABASE_NAME);
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateMapperTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateMapperTest.java
index 35a9061..fc4fb30 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateMapperTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/TemplateMapperTest.java
@@ -16,12 +16,13 @@ public class TemplateMapperTest {
public void testToHibernateEntityWhenTemplateIdIsDeleted() throws Exception {
// given
TemplateIdMapper templateIdMapper = new TemplateIdMapper();
- String id = "58893453";
+ String generatedId = "58893453";
String name = "oracle_template";
String serverId = "gta73284";
String version = "0.0.1";
pl.gov.coi.cascades.contract.domain.Template template = new pl.gov.coi.cascades.contract.domain.Template(
- id,
+ null,
+ generatedId,
name,
TemplateIdStatus.DELETED,
true,
@@ -34,8 +35,9 @@ public void testToHibernateEntityWhenTemplateIdIsDeleted() throws Exception {
// then
assertThat(actual).isNotNull();
+ assertThat(actual.getId()).isNull();
assertThat(actual.getName()).isEqualTo(name);
- assertThat(actual.getGeneratedId()).isEqualTo(id);
+ assertThat(actual.getGeneratedId()).isEqualTo(generatedId);
assertThat(actual.isDefault()).isTrue();
assertThat(actual.getServerId()).isEqualTo(serverId);
assertThat(actual.getStatus().name()).isEqualTo(TemplateIdStatus.DELETED.name());
@@ -45,12 +47,14 @@ public void testToHibernateEntityWhenTemplateIdIsDeleted() throws Exception {
public void testToHibernateEntity() throws Exception {
// given
TemplateIdMapper templateIdMapper = new TemplateIdMapper();
- String id = "673735756";
+ String generatedId = "673735756";
+ long id = 123L;
String serverId = "fre5345";
String version = "0.0.1";
String name = "oracle_template";
pl.gov.coi.cascades.contract.domain.Template template = new pl.gov.coi.cascades.contract.domain.Template(
id,
+ generatedId,
name,
TemplateIdStatus.CREATED,
true,
@@ -63,7 +67,8 @@ public void testToHibernateEntity() throws Exception {
// then
assertThat(actual).isNotNull();
- assertThat(actual.getGeneratedId()).isEqualTo(id);
+ assertThat(actual.getGeneratedId()).isEqualTo(generatedId);
+ assertThat(actual.getId()).isEqualTo(id);
assertThat(actual.getName()).isEqualTo(name);
assertThat(actual.isDefault()).isTrue();
assertThat(actual.getServerId()).isEqualTo(serverId);
@@ -76,12 +81,14 @@ public void testFromHibernateEntity() throws Exception {
// given
TemplateIdMapper templateIdMapper = new TemplateIdMapper();
Template template = new Template();
- String id = "dg6jf2g7";
+ long id = 123L;
+ String generatedId = "dg6jf2g7";
String serverId = "fre5345";
String version = "0.0.1";
String name = "oracle_template";
template.setName(name);
- template.setGeneratedId(id);
+ template.setId(id);
+ template.setGeneratedId(generatedId);
template.setDefault(false);
template.setServerId(serverId);
template.setVersion(version);
@@ -94,7 +101,7 @@ public void testFromHibernateEntity() throws Exception {
// then
assertThat(actual).isNotNull();
- assertThat(actual.getId()).isEqualTo(id);
+ assertThat(actual.getGeneratedId()).isEqualTo(generatedId);
assertThat(actual.getName()).isEqualTo(name);
assertThat(actual.isDefault()).isFalse();
assertThat(actual.getServerId()).isEqualTo(serverId);
diff --git a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/UserMapperTest.java b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/UserMapperTest.java
index e8046e8..c03f96b 100644
--- a/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/UserMapperTest.java
+++ b/cascades-server/persistence-hibernate/src/test/java/pl/gov/coi/cascades/server/persistance/hibernate/mapper/UserMapperTest.java
@@ -49,6 +49,7 @@ public class UserMapperTest {
private static final String SERVER_ID = "5v36y5646";
private static final String VERSION = "0.0.1";
private static final String TEMPLATE_ID_NAME = "oracle_template";
+ private static final long ID_TEMPLATE = 123L;
private Date created = Date.from(Instant.now());
@Mock
@@ -111,6 +112,7 @@ public void testFromHibernateEntity() throws Exception {
hibernateInstance.setId(DATABASE_ID_AS_LONG);
Template template = new Template();
template.setDefault(false);
+ template.setId(ID_TEMPLATE);
template.setGeneratedId(GENERATED_ID);
template.setServerId(SERVER_ID);
template.setName(TEMPLATE_ID_NAME);
diff --git a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/InputTemplate.java b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/InputTemplate.java
index fc57911..1858252 100644
--- a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/InputTemplate.java
+++ b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/InputTemplate.java
@@ -15,10 +15,11 @@ class InputTemplate extends Template {
private static final boolean IS_DEFAULT = false;
private static final String DEFAULT_SERVER_ID = null;
private static final String VERSION = null;
- private static final String ID = null;
+ private static final String NAME = null;
+ private static final Long ID = null;
- InputTemplate(String name) {
- super(ID, name, DEFAULT_STATUS, IS_DEFAULT, DEFAULT_SERVER_ID, VERSION);
+ InputTemplate(String generatedId) {
+ super(ID, generatedId, NAME, DEFAULT_STATUS, IS_DEFAULT, DEFAULT_SERVER_ID, VERSION);
}
}
diff --git a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseController.java b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseController.java
index e1a2fa1..6cbc552 100644
--- a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseController.java
+++ b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseController.java
@@ -51,7 +51,6 @@ public ResponseEntity> launchDatabasePost(
User user = userSession.getSignedInUser();
Request.RequestBuilder requestBuilder = Request.builder()
- .type(request.getType())
.user(user);
optionalMapper.toJava8(request.getTemplate())
diff --git a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTO.java b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTO.java
index 5ef0acc..7157078 100644
--- a/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTO.java
+++ b/cascades-server/presentation/src/main/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTO.java
@@ -15,10 +15,9 @@ final class RemoteDatabaseRequestDTO extends RemoteDatabaseRequest {
/**
* {@inheritDoc}
*/
- public RemoteDatabaseRequestDTO(@JsonProperty("type") String type,
- @JsonProperty("templateId") @Nullable String templateId,
+ public RemoteDatabaseRequestDTO(@JsonProperty("templateId") @Nullable String templateId,
@JsonProperty("instanceName") @Nullable String instanceName) {
- super(type, createTemplateId(templateId), instanceName);
+ super(createTemplateId(templateId), instanceName);
}
@Nullable
diff --git a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseControllerTest.java b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseControllerTest.java
index a52efda..789c492 100644
--- a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseControllerTest.java
+++ b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/LaunchDatabaseControllerTest.java
@@ -35,7 +35,6 @@ public class LaunchDatabaseControllerTest {
@Test
public void testLaunchDatabasePost() throws Exception {
// given
- String type = "oracle";
String templateId = "oracle_template";
String instanceName = "ora84u8439";
String username = "jrambo";
@@ -49,7 +48,6 @@ public void testLaunchDatabasePost() throws Exception {
DefaultOptionalMapper optionalMapper = new DefaultOptionalMapper();
when(userSession.getSignedInUser()).thenReturn(user);
RemoteDatabaseRequestDTO request = new RemoteDatabaseRequestDTO(
- type,
templateId,
instanceName
);
@@ -71,5 +69,4 @@ public void testLaunchDatabasePost() throws Exception {
assertThat(actual.getBody()).isNotNull();
assertThat(actual.getBody().getViolations()).hasSize(0);
}
-
}
diff --git a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/PresenterTest.java b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/PresenterTest.java
index 0dc98dd..252c0bb 100644
--- a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/PresenterTest.java
+++ b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/PresenterTest.java
@@ -8,7 +8,6 @@
import org.mockito.junit.MockitoRule;
import org.springframework.http.HttpStatus;
import pl.gov.coi.cascades.contract.domain.DatabaseId;
-import pl.gov.coi.cascades.contract.domain.DatabaseType;
import pl.gov.coi.cascades.contract.domain.NetworkBind;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
import pl.gov.coi.cascades.contract.service.Violation;
@@ -26,9 +25,6 @@ public class PresenterTest {
@Mock
private DatabaseId databaseId;
- @Mock
- private DatabaseType databaseType;
-
@Mock
private NetworkBind networkBind;
diff --git a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTOTest.java b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTOTest.java
index 6c8cbb4..8e4f846 100644
--- a/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTOTest.java
+++ b/cascades-server/presentation/src/test/java/pl/gov/coi/cascades/server/presentation/launchdatabase/RemoteDatabaseRequestDTOTest.java
@@ -13,12 +13,10 @@ public class RemoteDatabaseRequestDTOTest {
@Test
public void testParameterConstructorWhenTemplateIdIsNotPresent() {
// given
- String type = "";
String instanceName = "oracler49903";
// when
RemoteDatabaseRequestDTO actual = new RemoteDatabaseRequestDTO(
- type,
null,
instanceName
);
@@ -31,13 +29,11 @@ public void testParameterConstructorWhenTemplateIdIsNotPresent() {
@Test
public void testParameterConstructorWhenTemplateIdIsPresent() {
// given
- String type = "";
String instanceName = "oracler49903";
String templateName = "oracle_template";
// when
RemoteDatabaseRequestDTO actual = new RemoteDatabaseRequestDTO(
- type,
templateName,
instanceName
);
@@ -45,7 +41,7 @@ public void testParameterConstructorWhenTemplateIdIsPresent() {
// then
assertThat(actual).isNotNull();
assertThat(actual.getTemplate().isPresent()).isTrue();
- assertThat(actual.getTemplate().get().getName()).isEqualTo(templateName);
+ assertThat(actual.getTemplate().get().getGeneratedId()).isEqualTo(templateName);
assertThat(actual.getTemplate().get()).isInstanceOf(InputTemplate.class);
}