-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from gtantachuco-pivotal/master
Added support for MS-SQL and Azure SQL DB
- Loading branch information
Showing
9 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...tor/src/main/java/org/springframework/cloud/cloudfoundry/SqlServerServiceInfoCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.springframework.cloud.cloudfoundry; | ||
|
||
import org.springframework.cloud.service.common.SqlServerServiceInfo; | ||
|
||
public class SqlServerServiceInfoCreator extends | ||
RelationalServiceInfoCreator<SqlServerServiceInfo> { | ||
|
||
public SqlServerServiceInfoCreator() { | ||
super(new Tags(), SqlServerServiceInfo.SQLSERVER_SCHEME); } | ||
|
||
@Override | ||
public SqlServerServiceInfo createServiceInfo(String id, String url) { | ||
return new SqlServerServiceInfo(id, url); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ava/org/springframework/cloud/cloudfoundry/CloudFoundryConnectorSqlServerServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.springframework.cloud.cloudfoundry; | ||
|
||
import org.junit.Test; | ||
import org.springframework.cloud.service.BaseServiceInfo; | ||
import org.springframework.cloud.service.ServiceInfo; | ||
import org.springframework.cloud.service.common.RelationalServiceInfo; | ||
import org.springframework.cloud.service.common.SqlServerServiceInfo; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class CloudFoundryConnectorSqlServerServiceTest extends AbstractUserProvidedServiceInfoCreatorTest { | ||
|
||
private static final String INSTANCE_NAME = "database"; | ||
private static final String SQLSERVER_SCHEME = "sqlserver:"; | ||
private static final String SERVICE_NAME = "sqlserver-ups"; | ||
|
||
@Test | ||
public void sqlServerServiceCreation() { | ||
when(mockEnvironment.getEnvValue("VCAP_SERVICES")) | ||
.thenReturn(getServicesPayload( | ||
getUserProvidedServicePayload(SERVICE_NAME, hostname, port, username, password, INSTANCE_NAME, SQLSERVER_SCHEME))); | ||
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos(); | ||
|
||
SqlServerServiceInfo info = (SqlServerServiceInfo) getServiceInfo(serviceInfos, SERVICE_NAME); | ||
assertServiceFoundOfType(info, SqlServerServiceInfo.class); | ||
assertEquals(getSqlServerJdbcUrl(INSTANCE_NAME), info.getJdbcUrl()); | ||
} | ||
|
||
@Test | ||
public void sqlServerServiceCreationWithNoUri() { | ||
when(mockEnvironment.getEnvValue("VCAP_SERVICES")) | ||
.thenReturn(getServicesPayload( | ||
getUserProvidedServicePayloadWithNoUri(SERVICE_NAME, hostname, port, username, password, INSTANCE_NAME))); | ||
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos(); | ||
|
||
BaseServiceInfo info = (BaseServiceInfo) getServiceInfo(serviceInfos, SERVICE_NAME); | ||
assertFalse(SqlServerServiceInfo.class.isAssignableFrom(info.getClass())); // service was not detected as SQL-Server | ||
assertNotNull(info); | ||
} | ||
|
||
@Test | ||
public void sqlServerServiceCreationWithJdbcUrl() { | ||
when(mockEnvironment.getEnvValue("VCAP_SERVICES")) | ||
.thenReturn(getServicesPayload( | ||
getSqlServerServicePayloadWithJdbcurl(SERVICE_NAME, hostname, port, username, password, INSTANCE_NAME, SQLSERVER_SCHEME))); | ||
List<ServiceInfo> serviceInfos = testCloudConnector.getServiceInfos(); | ||
|
||
SqlServerServiceInfo info = (SqlServerServiceInfo) getServiceInfo(serviceInfos, SERVICE_NAME); | ||
assertServiceFoundOfType(info, SqlServerServiceInfo.class); | ||
assertEquals(RelationalServiceInfo.JDBC_PREFIX + "sqlserver:rawjdbcurl", info.getJdbcUrl()); | ||
} | ||
|
||
protected String getSqlServerServicePayloadWithJdbcurl(String serviceName, String hostname, int port, | ||
String user, String password, String name, String scheme) { | ||
String payload = getRelationalPayload("test-sqlserver-info-jdbc-url.json", serviceName, | ||
hostname, port, user, password, name); | ||
return payload.replace("$scheme", scheme); | ||
} | ||
|
||
private String getSqlServerJdbcUrl(String name) { | ||
return "jdbc:sqlserver://" + hostname + ":" + port + ";database=" + name + ";user=" + username + ";password=" + password; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...c/test/resources/org/springframework/cloud/cloudfoundry/test-sqlserver-info-jdbc-url.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "$serviceName", | ||
"credentials": { | ||
"jdbcUrl": "jdbc:sqlserver:rawjdbcurl", | ||
"uri": "sqlserver://$user:$password@$hostname:$port/$name" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...oud-core/src/main/java/org/springframework/cloud/service/common/SqlServerServiceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.springframework.cloud.service.common; | ||
|
||
import org.springframework.cloud.service.ServiceInfo; | ||
|
||
@ServiceInfo.ServiceLabel("sqlserver") | ||
public class SqlServerServiceInfo extends RelationalServiceInfo { | ||
private static final String JDBC_URL_TYPE = "sqlserver"; | ||
|
||
public static final String SQLSERVER_SCHEME = JDBC_URL_TYPE; | ||
|
||
public SqlServerServiceInfo(String id, String url) { | ||
super(id, url, JDBC_URL_TYPE); | ||
} | ||
|
||
@Override | ||
public String getJdbcUrl() | ||
{ | ||
return String.format("jdbc:%s://%s:%d;database=%s;user=%s;password=%s;", | ||
jdbcUrlDatabaseType, | ||
getHost(), getPort(), getPath(), getUserName(), getPassword()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...rc/main/java/org/springframework/cloud/service/relational/SqlServerDataSourceCreator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.springframework.cloud.service.relational; | ||
|
||
import org.springframework.cloud.service.common.SqlServerServiceInfo; | ||
import org.springframework.cloud.service.relational.DataSourceCreator; | ||
|
||
public class SqlServerDataSourceCreator extends DataSourceCreator<SqlServerServiceInfo> { | ||
|
||
private static final String[] DRIVERS = new String[]{"com.microsoft.sqlserver.jdbc.SQLServerDriver"}; | ||
private static final String VALIDATION_QUERY = "SELECT 1"; | ||
|
||
public SqlServerDataSourceCreator() { | ||
super("spring-cloud.sqlserver.driver", DRIVERS, VALIDATION_QUERY); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...est/java/org/springframework/cloud/service/relational/SqlServerDataSourceFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.springframework.cloud.service.relational; | ||
|
||
import org.springframework.cloud.service.common.SqlServerServiceInfo; | ||
|
||
public class SqlServerDataSourceFactoryTest extends AbstractDataSourceFactoryTest<SqlServerServiceInfo> { | ||
public SqlServerServiceInfo getTestServiceInfo(String id) { | ||
return new SqlServerServiceInfo(id, "sqlserver://username:pass@host:port/db"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...c/test/java/org/springframework/cloud/service/relational/SqlServerServiceCreatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.springframework.cloud.service.relational; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Before; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.cloud.service.common.SqlServerServiceInfo; | ||
|
||
public class SqlServerServiceCreatorTest extends AbstractDataSourceCreatorTest<SqlServerDataSourceCreator, SqlServerServiceInfo> { | ||
@Mock private SqlServerServiceInfo mockSqlServerServiceInfo; | ||
|
||
@Before | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
// set a dummy JDBC driver since we can't include a real SQL-Server driver in the project due to licensing restrictions | ||
System.setProperty("spring-cloud.sqlserver.driver", "com.example.Driver"); | ||
} | ||
|
||
@Override | ||
public SqlServerServiceInfo createServiceInfo() { | ||
when(mockSqlServerServiceInfo.getJdbcUrl()).thenReturn("sqlserver://myuser:[email protected]:1433/database-123"); | ||
|
||
return mockSqlServerServiceInfo; | ||
} | ||
|
||
@Override | ||
public String getDriverName() { | ||
return "com.example.Driver"; | ||
} | ||
|
||
@Override | ||
public SqlServerDataSourceCreator getCreator() { | ||
return new SqlServerDataSourceCreator(); | ||
} | ||
|
||
@Override | ||
public String getValidationQueryStart() { | ||
return "SELECT 1"; | ||
} | ||
} |