Adding tests for MS-SQL/Azure SQL
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "$serviceName",
|
||||
"credentials": {
|
||||
"jdbcUrl": "jdbc:sqlserver:rawjdbcurl",
|
||||
"uri": "sqlserver://$user:$password@$hostname:$port/$name"
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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:mypassword@10.20.30.40: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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user