Skip to content

Commit

Permalink
Merge pull request #127 from gtantachuco-pivotal/master
Browse files Browse the repository at this point in the history
Added support for MS-SQL and Azure SQL DB
  • Loading branch information
scottfrederick committed Jul 24, 2015
2 parents bde7ae2 + fb27a51 commit 9e78abe
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 0 deletions.
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ org.springframework.cloud.cloudfoundry.MonitoringServiceInfoCreator
org.springframework.cloud.cloudfoundry.SmtpServiceInfoCreator
org.springframework.cloud.cloudfoundry.OracleServiceInfoCreator
org.springframework.cloud.cloudfoundry.DB2ServiceInfoCreator
org.springframework.cloud.cloudfoundry.SqlServerServiceInfoCreator
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;
}
}
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"
}
}
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());
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ org.springframework.cloud.service.keyval.RedisConnectionFactoryCreator
org.springframework.cloud.service.document.MongoDbFactoryCreator
org.springframework.cloud.service.messaging.RabbitConnectionFactoryCreator
org.springframework.cloud.service.smtp.MailSenderCreator
org.springframework.cloud.service.relational.SqlServerDataSourceCreator
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");
}
}
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";
}
}

0 comments on commit 9e78abe

Please sign in to comment.