diff --git a/components/dbutil/pom.xml b/components/dbutil/pom.xml
index f018d7ec..b88c7128 100644
--- a/components/dbutil/pom.xml
+++ b/components/dbutil/pom.xml
@@ -115,6 +115,21 @@
jta
1.1
+
+ org.testng
+ testng
+
+
+ org.assertj
+ assertj-core
+
+
+ org.mockito
+ mockito-core
+ 3.0.0
+ test
+
+
diff --git a/components/dbutil/src/main/java/com/wso2telco/core/dbutils/DbUtils.java b/components/dbutil/src/main/java/com/wso2telco/core/dbutils/DbUtils.java
index 435175b8..760cb24e 100644
--- a/components/dbutil/src/main/java/com/wso2telco/core/dbutils/DbUtils.java
+++ b/components/dbutil/src/main/java/com/wso2telco/core/dbutils/DbUtils.java
@@ -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) {
@@ -165,7 +176,6 @@ public static Connection getDBConnection() throws SQLException, DBUtilException
public static Connection getConnectDbConnection() throws SQLException, DBUtilException {
initializeConnectDatasource();
-
if (connectDatasource != null) {
return connectDatasource.getConnection();
} else {
@@ -173,25 +183,30 @@ public static Connection getConnectDbConnection() throws SQLException, DBUtilExc
}
}
+ /**
+ * @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 {
@@ -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
*/
@@ -548,7 +560,14 @@ private static void closeStatement(Statement statement) {
}
}
- public static Map getDbNames() {
+ /**
+ * @deprecated use {@link #getDbNames(Context ctx)} instead
+ */
+ public static Map getDbNames() throws NamingException {
+ return getDbNames(new InitialContext());
+ }
+
+ public static Map getDbNames(Context ctx) {
if (dbNames == null) {
dbNames = new HashMap<>();
@@ -556,7 +575,7 @@ public static Map getDbNames() {
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();
diff --git a/components/dbutil/src/test/java/com/wso2telco/core/dbutils/DbUtilsTest.java b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/DbUtilsTest.java
new file mode 100644
index 00000000..36ac5bff
--- /dev/null
+++ b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/DbUtilsTest.java
@@ -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);
+ }
+
+
+}
+
diff --git a/components/dbutil/src/test/java/com/wso2telco/core/dbutils/OperatorendpointTest.java b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/OperatorendpointTest.java
new file mode 100644
index 00000000..e0eb74d9
--- /dev/null
+++ b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/OperatorendpointTest.java
@@ -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");
+ }
+}
diff --git a/components/dbutil/src/test/java/com/wso2telco/core/dbutils/fileutils/PropertyFileReaderTest.java b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/fileutils/PropertyFileReaderTest.java
new file mode 100644
index 00000000..394626c9
--- /dev/null
+++ b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/fileutils/PropertyFileReaderTest.java
@@ -0,0 +1,34 @@
+package com.wso2telco.core.dbutils.fileutils;
+
+import org.assertj.core.api.Assertions;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import java.util.Properties;
+
+
+public class PropertyFileReaderTest {
+
+ @BeforeTest
+ public void setUp() {
+ PropertyFileReaderTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
+ try {
+ System.setProperty("carbon.config.dir.path", PropertyFileReaderTest.class.getProtectionDomain().getCodeSource().getLocation().getPath());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testGetFileReader_whenValidPathGiven() {
+ Properties properties = PropertyFileReader.getFileReader().getProperties("test.txt");
+ Assertions.assertThat(properties.getProperty("key1")).isEqualTo("value1");
+
+ }
+
+ @Test
+ public void testGetFileReader_fileNotFoundException() {
+ Properties properties = PropertyFileReader.getFileReader().getProperties("wrong.txt");
+ Assertions.assertThat(properties.getProperty("key1")).isEqualTo(null);
+ }
+}
diff --git a/components/dbutil/src/test/java/com/wso2telco/core/dbutils/hibernateutil/HibernateAbstractDAOTest.java b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/hibernateutil/HibernateAbstractDAOTest.java
new file mode 100644
index 00000000..5ccefcf6
--- /dev/null
+++ b/components/dbutil/src/test/java/com/wso2telco/core/dbutils/hibernateutil/HibernateAbstractDAOTest.java
@@ -0,0 +1,76 @@
+package com.wso2telco.core.dbutils.hibernateutil;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyObject;
+import static org.mockito.Mockito.*;
+
+public class HibernateAbstractDAOTest {
+
+ @Mock HibernateAbstractDAO hibernateAbstractDAO;
+
+ @BeforeMethod
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ doCallRealMethod().when(hibernateAbstractDAO).save(anyObject());
+ doCallRealMethod().when(hibernateAbstractDAO).saveOrUpdate(anyObject());
+ doCallRealMethod().when(hibernateAbstractDAO).saveList(anyList());
+ doCallRealMethod().when(hibernateAbstractDAO).saveOrUpdateList(anyList());
+
+ }
+
+
+ @Test
+ public void testSave_whenThereIsNoException() throws Exception {
+ Session session = Mockito.mock(Session.class);
+ Transaction transaction =Mockito.mock(Transaction.class);
+ when(session.beginTransaction()).thenReturn(transaction);
+ when(hibernateAbstractDAO.getSession()).thenReturn(session);
+ hibernateAbstractDAO.save(anyObject());
+ verify(session,times(1)).save(anyObject());
+ verify(transaction,times(1)).commit();
+
+ }
+
+ @Test
+ public void testSaveOrUpdate_whenThereIsNoException() throws Exception {
+ Session session = Mockito.mock(Session.class);
+ Transaction transaction =Mockito.mock(Transaction.class);
+ when(session.beginTransaction()).thenReturn(transaction);
+ when(hibernateAbstractDAO.getSession()).thenReturn(session);
+ hibernateAbstractDAO.saveOrUpdate(anyObject());
+ verify(session,times(1)).saveOrUpdate(anyObject());
+
+ }
+
+ @Test
+ public void testSaveList_whenThereIsNoException_ShouldCallTimesEqualToListLength() throws Exception {
+ Session session = Mockito.mock(Session.class);
+ Transaction transaction =Mockito.mock(Transaction.class);
+ when(session.beginTransaction()).thenReturn(transaction);
+ when(hibernateAbstractDAO.getSession()).thenReturn(session);
+ List