Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing #181

Open
wants to merge 2 commits into
base: unit-testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions components/dbutil/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,30 @@ public static void initializeDatasources() throws SQLException, DBUtilException
}
}

/**
* @deprecated use {@link #initializeConnectDatasource(Context ctx)} instead
*/
@Deprecated
public static void initializeConnectDatasource() throws SQLException, DBUtilException {
try {
initializeConnectDatasource(new InitialContext());
} catch (NamingException e) {
handleException("Error while looking up the data source: " + CONNECT_DB, e);
}
}

/**
* Initialize datasources.
*
* @throws SQLException the SQL exception
* the db util exception
*/
public static void initializeConnectDatasource() throws SQLException, DBUtilException {
public static void initializeConnectDatasource(Context ctx) throws SQLException, DBUtilException {
if (connectDatasource != null) {
return;
}

try {
Context ctx = new InitialContext();
connectDatasource = (DataSource) ctx.lookup(CONNECT_DB);

} catch (NamingException e) {
Expand Down Expand Up @@ -165,33 +176,37 @@ public static Connection getDBConnection() throws SQLException, DBUtilException

public static Connection getConnectDbConnection() throws SQLException, DBUtilException {
initializeConnectDatasource();

if (connectDatasource != null) {
return connectDatasource.getConnection();
} else {
throw new SQLException("Datasource not initialized properly");
}
}

/**
* @deprecated use {@link #getDbConnection(DataSourceNames dataSourceName,Context ctx) instead
*/
public static synchronized Connection getDbConnection(DataSourceNames dataSourceName) throws Exception {
return getDbConnection(dataSourceName,new InitialContext());
}

/**
* Gets the db connection.
*
* @return the db connection
* @throws SQLException the SQL exception
*/
public static synchronized Connection getDbConnection(DataSourceNames dataSourceName) throws Exception {
public static synchronized Connection getDbConnection(DataSourceNames dataSourceName,Context ctx) throws Exception {

try {
if (!dbDataSourceMap.containsKey(dataSourceName)) {

Context ctx = new InitialContext();
dbDataSourceMap.put(dataSourceName, (DataSource) ctx.lookup(dataSourceName.jndiName()));
}

DataSource dbDatasource = dbDataSourceMap.get(dataSourceName);

if (dbDatasource != null) {

log.info(dataSourceName.toString() + " DB Initialize successfully.");
return dbDatasource.getConnection();
} else {
Expand Down Expand Up @@ -342,9 +357,6 @@ public static String format(double doubData, int precision, int scale) throws Ex
/**
* Format.
*
* @param decData the dec data
* @param precision the precision
* @param scale the scale
* @return the string
* @throws Exception the exception
*/
Expand Down Expand Up @@ -548,15 +560,22 @@ private static void closeStatement(Statement statement) {
}
}

public static Map<DataSourceNames, String> getDbNames() {
/**
* @deprecated use {@link #getDbNames(Context ctx)} instead
*/
public static Map<DataSourceNames, String> getDbNames() throws NamingException {
return getDbNames(new InitialContext());
}

public static Map<DataSourceNames, String> getDbNames(Context ctx) {

if (dbNames == null) {
dbNames = new HashMap<>();
Connection con = null;

for (DataSourceNames name : DataSourceNames.values()) {
try {
con = DbUtils.getDbConnection(name);
con = DbUtils.getDbConnection(name,ctx);
if (con != null) {
dbNames.put(name, con.getCatalog());
con.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package com.wso2telco.core.dbutils;

import com.wso2telco.core.dbutils.util.DataSourceNames;
import org.assertj.core.api.Assertions;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;

public class DbUtilsTest {
DbUtils dbUtils;

@BeforeTest
public void setUp() {
dbUtils = DbUtils.getInstance();
}

@Test
public void testInitializeConnectDatasource_whenRightName() throws SQLException, DBUtilException, NamingException {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
DbUtils.getInstance().initializeConnectDatasource(ctxMock);
verify(ctxMock, times(1)).lookup("jdbc/CONNECT_DB");

}

@Test
public void testInitializeConnectDatasource_whenWrongName() throws SQLException, DBUtilException, NamingException {

DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
DbUtils.getInstance().initializeConnectDatasource(ctxMock);
verify(ctxMock, never()).lookup("wrongdatabase");

}

@Test
public void testGetDBConnection_whenDataSourceName_WSO2AM_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2AM_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}
@Test
public void testGetDBConnection_whenDataSourceName_WSO2AM_STATS_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2AM_STATS_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}

@Test
public void testGetDBConnection_whenDataSourceName_WSO2UM_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2UM_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}

@Test
public void testGetDBConnection_whenDataSourceName_WSO2REG_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2REG_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}
@Test
public void testGetDBConnection_whenDataSourceName_WSO2TELCO_DEP_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2TELCO_DEP_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}

@Test
public void testGetDBConnection_whenDataSourceName_WSO2TELCO_RATE_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2TELCO_RATE_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}

@Test
public void testGetDBConnection_whenDataSourceName_WSO2TELCO_MANDATE_DB() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(DataSourceNames.WSO2TELCO_MANDATE_DB,ctxMock);
verify(dataSourceMock,times(1)).getConnection();

}


@Test(expectedExceptions = Exception.class)
public void testGetDBConnection_whenDataSourceName_Null() throws Exception {
DataSource dataSourceMock = Mockito.mock(DataSource.class);
Context ctxMock = Mockito.mock(Context.class);
Connection conMock =Mockito.mock(Connection.class);
when(ctxMock.lookup(anyString())).thenReturn(dataSourceMock);
when(dataSourceMock.getConnection()).thenReturn(conMock);
DbUtils.getDbConnection(null,ctxMock);
// verify(dataSourceMock,times(1)).getConnection();

}



@Test
public void testFormat_whenFinalLengthLargerThanStringLength() throws Exception {
int finalLength =20;
String strData = "chamara";
Assertions.assertThat(DbUtils.format(strData,finalLength).length()).isEqualTo(finalLength);
}

@Test
public void testFormat_whenStringLengthLargerThanFinalLength() throws Exception {
int finalLength =2;
String strData = "chamara";
Assertions.assertThat(DbUtils.format(strData,finalLength).length()).isEqualTo(finalLength);
}
@Test
public void testFormat_whenIntDataLengthLargerThanFinalLength() throws Exception {
int intData =100;
int finalLength = 40;
Assertions.assertThat(DbUtils.format(intData,finalLength).length()).isEqualTo(finalLength);
}
@Test
public void testFormat_whenIntDataLengthSmallerThanFinalLength() throws Exception {
int intData =1;
int finalLength = 40;
Assertions.assertThat(DbUtils.format(intData,finalLength).length()).isEqualTo(finalLength);
}

@Test
public void testFormat_shouldReturnToPrecisionLength() throws Exception {
double doubleData =100.999999999;
int precision = 10;
int scale =4;
Assertions.assertThat(DbUtils.format(doubleData,precision,scale).length()).isEqualTo(precision+1);
}

@Test
public void testFormat_shouldReturnCorrectScale() throws Exception {
double doubleData =100.999999999;
int precision = 10;
int scale =4;

String doubleToString = DbUtils.format(doubleData, precision, scale);
String[] stringArray =doubleToString.split("\\.");
Assertions.assertThat(stringArray[1].length()).isEqualTo(scale);

}
@Test
public void testFormat_whenBigDecimalShouldReturnToPrecisionLength() throws Exception {
BigDecimal decData = new BigDecimal(232424234);
int precision = 10;
int scale =4;
Assertions.assertThat(DbUtils.format(decData,precision,scale).length()).isEqualTo(precision+1);
}
@Test
public void testFormat_whenBigDecimalShouldReturnCorrectScale() throws Exception {
BigDecimal decData = new BigDecimal(23242.234);
int precision = 10;
int scale =5;
System.out.println(DbUtils.format(decData, precision, scale));
String doubleToString = DbUtils.format(decData, precision, scale);
String[] stringArray =doubleToString.split("\\.");
Assertions.assertThat(stringArray[1].length()).isEqualTo(scale);
}

@Test
public void testDisconnect() throws Exception {
Connection moccon = Mockito.mock(Connection.class);
DbUtils.getInstance().disconnect(moccon);
verify(moccon,times(1)).commit();
verify(moccon,times(1)).close();
}


@Test
public void testGetDbNames() throws NamingException {
Context ctxMock = Mockito.mock(Context.class);
DbUtils dbutil =DbUtils.getInstance();
dbutil.getDbNames(ctxMock);
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.wso2telco.core.dbutils;

import org.assertj.core.api.Assertions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class OperatorendpointTest {
private Operatorendpoint operatorendpoint;

@BeforeTest
public void initialtest() {
operatorendpoint = new Operatorendpoint(2, "operatercode", "http//localhost", "/api");
}

@Test
public void testgetOperatorid_whenoperateridcorrect() {
Assertions.assertThat(operatorendpoint.getApi()).isEqualTo("http//localhost");
}

@Test
public void testgetEndpoint() {
Assertions.assertThat(operatorendpoint.getEndpoint()).isEqualTo("/api");
}

@Test
public void testgetId() {
Assertions.assertThat(operatorendpoint.getOperatorid()).isEqualTo(2);
}

@Test
public void testsetoperatoecode() {
Assertions.assertThat(operatorendpoint.getOperatorcode()).isEqualTo("operatercode");
}
}
Loading