Skip to content

Commit

Permalink
Merge pull request #76 from dcrissman/adding-unit-tests
Browse files Browse the repository at this point in the history
Adding unit tests
  • Loading branch information
jewzaam committed Dec 4, 2014
2 parents 086814c + 4e5ee6d commit de32868
Show file tree
Hide file tree
Showing 21 changed files with 1,434 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package com.redhat.lightblue.common.rdbms;

import org.h2.jdbcx.JdbcConnectionPool;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.h2.jdbcx.JdbcConnectionPool;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class RDBMSUtilsTest {

@Rule
public ExpectedException expectedEx = ExpectedException.none();

@Test
public void testGetDatasSource_DataSourceNotFound(){
expectedEx.expect(com.redhat.lightblue.util.Error.class);
expectedEx.expectMessage("{\"objectType\":\"error\",\"context\":\"RDBMSUtils/getDataSource\",\"errorCode\":\""
+ RDBMSConstants.ERR_DATASOURCE_NOT_FOUND
+ "\",\"msg\":\"Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial\"}");

RDBMSUtils.getDataSource("Fake Data Source that was never defined");
}

@Test
public void testGetDataSource() throws Exception {
// Create initial context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@
*/
package com.redhat.lightblue.config.rdbms;

import com.fasterxml.jackson.databind.JsonNode;
import com.redhat.lightblue.common.rdbms.RDBMSUtils;
import com.redhat.lightblue.config.DataSourceConfiguration;
import com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.redhat.lightblue.common.rdbms.RDBMSUtils;
import com.redhat.lightblue.config.DataSourceConfiguration;
import com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParser;

/**
*
* @author lcestari
Expand Down Expand Up @@ -77,7 +80,7 @@ public void initializeFromJson(JsonNode node) {
if (x != null) {
JNDI = x.asText();
} else {
throw new IllegalStateException("No datasourceName was found: " + node.toString());
throw new IllegalStateException("No JNDI was found: " + node.toString());
}
dataSourceJDNIMap.put(datasourceName, JNDI);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.redhat.lightblue.config.rdbms;

import com.fasterxml.jackson.databind.JsonNode;
import com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParser;
import com.redhat.lightblue.util.JsonUtils;
import com.redhat.lightblue.util.test.FileUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.h2.jdbcx.JdbcConnectionPool;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.io.InputStreamReader;

import static org.junit.Assert.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParser;
import com.redhat.lightblue.util.JsonUtils;
import com.redhat.lightblue.util.test.FileUtil;

public class RDBMSDataSourceConfigurationTest {

Expand Down Expand Up @@ -67,5 +70,32 @@ public void testEqualsHashCodeToString() throws Exception {
assertEquals(test.toString(),cut.toString());
}

@Test(expected = IllegalArgumentException.class)
public void testInitializeFromJson_InvalidMetadataDataStoreParser() throws IOException{
JsonNode node = JsonUtils.json("{\"metadataDataStoreParser\":\"fakeValue\"}");

new RDBMSDataSourceConfiguration().initializeFromJson(node);
}

@Test(expected = IllegalStateException.class)
public void testInitializeFromJson_ConnectionNotFound() throws IOException{
JsonNode emptyNode = JsonUtils.json("{}");

new RDBMSDataSourceConfiguration().initializeFromJson(emptyNode);
}

@Test(expected = IllegalStateException.class)
public void testInitializeFromJson_DatasourceNotFound() throws IOException{
JsonNode emptyNode = JsonUtils.json("{\"connections\":[{\"JNDI\":\"some made up value\"}]}");

new RDBMSDataSourceConfiguration().initializeFromJson(emptyNode);
}

@Test(expected = IllegalStateException.class)
public void testInitializeFromJson_JNDINotFound() throws IOException{
JsonNode emptyNode = JsonUtils.json("{\"connections\":[{\"datasourceName\":\"some made up value\"}]}");

new RDBMSDataSourceConfiguration().initializeFromJson(emptyNode);
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
package com.redhat.lightblue.config.rdbms;

import com.redhat.lightblue.common.rdbms.RDBMSDataStore;
import com.redhat.lightblue.config.DataSourcesConfiguration;
import org.junit.After;
import org.junit.Test;

import javax.sql.DataSource;
import static org.junit.Assert.assertEquals;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

import static org.junit.Assert.*;
import javax.sql.DataSource;

public class RDBMSDataSourceMapTest {
import org.junit.Test;

RDBMSDataSourceMap cut;
import com.redhat.lightblue.common.rdbms.RDBMSDataStore;
import com.redhat.lightblue.config.DataSourcesConfiguration;

@After
public void after(){
cut = null;
}
public class RDBMSDataSourceMapTest {

private final int NUMBER = 123098;

@Test
public void testGet() throws Exception {
DataSourcesConfiguration ds = new DataSourcesConfiguration();
DataSourcesConfiguration dsc = new DataSourcesConfiguration();
RDBMSDataSourceConfiguration datasource = new RDBMSDataSourceConfiguration(){
@Override
public DataSource getDataSource(String name) {
Expand All @@ -37,15 +30,42 @@ public DataSource getDataSource(String name) {
};
datasource.setDatabaseName("testDB");
datasource.setMetadataDataStoreParser(Class.forName("com.redhat.lightblue.metadata.rdbms.impl.RDBMSDataStoreParser"));
ds.add("test", datasource);
dsc.add("test", datasource);
datasource.getDataSourceJDNIMap().put("test","jndi");
cut = new RDBMSDataSourceMap(ds);
RDBMSDataSourceMap dsMap = new RDBMSDataSourceMap(dsc);
RDBMSDataStore store = new RDBMSDataStore("testDB",null);

DataSource dataSource = cut.get(store);
DataSource dataSource = dsMap.get(store);
assertEquals(new StubDataSource(), dataSource);
}

@Test(expected = IllegalArgumentException.class)
public void testGet_DatasourceAndDatabaseNull() throws ClassNotFoundException{
DataSourcesConfiguration dsc = new DataSourcesConfiguration();
dsc.add("test", new RDBMSDataSourceConfiguration());

RDBMSDataStore store = new RDBMSDataStore(null, null);
new RDBMSDataSourceMap(dsc).get(store);
}

@Test(expected = IllegalArgumentException.class)
public void testGet_NoDatasourceForName() throws ClassNotFoundException{
DataSourcesConfiguration dsc = new DataSourcesConfiguration();
dsc.add("test", new RDBMSDataSourceConfiguration());

RDBMSDataStore store = new RDBMSDataStore(null, "fake");
new RDBMSDataSourceMap(dsc).get(store);
}

@Test(expected = IllegalArgumentException.class)
public void testGet_NoDatabaseForName() throws ClassNotFoundException{
DataSourcesConfiguration dsc = new DataSourcesConfiguration();
dsc.add("test", new RDBMSDataSourceConfiguration());

RDBMSDataStore store = new RDBMSDataStore("fake", null);
new RDBMSDataSourceMap(dsc).get(store);
}

private class StubDataSource implements DataSource {
@Override public Connection getConnection() throws SQLException {return null;}
@Override public Connection getConnection(String s, String s2) throws SQLException {return null;}
Expand All @@ -57,8 +77,12 @@ private class StubDataSource implements DataSource {
@Override public <T> T unwrap(Class<T> tClass) throws SQLException {return null;}
@Override public boolean isWrapperFor(Class<?> aClass) throws SQLException {return false;}
@Override public boolean equals(Object obj) {
if(obj == null) return false;
if (getClass() != obj.getClass()) return false;
if(obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return this.hashCode() == obj.hashCode();
}
@Override public int hashCode() {return NUMBER;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,33 @@
*/
package com.redhat.lightblue.crud.rdbms;

import com.redhat.lightblue.metadata.rdbms.converter.DynVar;
import com.redhat.lightblue.metadata.rdbms.converter.RowMapper;
import com.redhat.lightblue.metadata.rdbms.converter.RDBMSContext;
import com.redhat.lightblue.metadata.rdbms.util.Column;

import javax.lang.model.type.NullType;
import java.math.BigDecimal;
import java.sql.*;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.lang.model.type.NullType;

import com.redhat.lightblue.metadata.rdbms.converter.DynVar;
import com.redhat.lightblue.metadata.rdbms.converter.RDBMSContext;
import com.redhat.lightblue.metadata.rdbms.converter.RowMapper;
import com.redhat.lightblue.metadata.rdbms.util.Column;

/**
* Maps a row into DynVar
*
* @author lcestari
*/
public class VariableUpdateRowMapper<Void> implements RowMapper<Void> {
private RDBMSContext rdbmsContext;
private final RDBMSContext rdbmsContext;

public VariableUpdateRowMapper(RDBMSContext rdbmsContext) {
this.rdbmsContext = rdbmsContext;
Expand All @@ -47,7 +56,6 @@ public Void map(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i = 1; i < columnCount + 1; i++) {
int position = i;
String name = rsmd.getColumnName(i);
String alias = rsmd.getColumnLabel(i);
String clazz = rsmd.getColumnClassName(i);
Expand Down
Loading

0 comments on commit de32868

Please sign in to comment.