Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lashinijay committed Nov 5, 2024
1 parent 4b4c4cb commit a4457de
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.oauth2.dao;

import org.apache.commons.dbcp.BasicDataSource;
import org.mockito.MockedStatic;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
import org.wso2.carbon.identity.oauth2.dao.util.DAOUtils;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mockStatic;
import static org.testng.Assert.assertEquals;

@WithCarbonHome
@Listeners(MockitoTestNGListener.class)
public class AccessTokenDAOImplTest {

private AccessTokenDAOImpl accessTokenDAO;
private static Map<String, BasicDataSource> dataSourceMap = new HashMap<>();

public static final String H2_SCRIPT_NAME = "identity.sql";
public static final String H2_SCRIPT2_NAME = "insert_token_binding.sql";
public static final String DB_NAME = "AccessTokenDB";
Connection connection = null;
private MockedStatic<IdentityDatabaseUtil> identityDatabaseUtil;

@BeforeClass
public void initTest() throws Exception {

try {
DAOUtils.initializeBatchDataSource(DB_NAME, H2_SCRIPT_NAME, H2_SCRIPT2_NAME);
} catch (Exception e) {
throw new IdentityOAuth2Exception("Error while initializing the data source", e);
}
accessTokenDAO = new AccessTokenDAOImpl();
}

@BeforeMethod
public void setUp() throws Exception {

connection = DAOUtils.getConnection(DB_NAME);
}

@AfterMethod
public void closeup() throws Exception {

if (connection != null) {
connection.close();
}
identityDatabaseUtil.close();
}

@AfterClass
public void tearDown() throws Exception {

closeH2Base(DB_NAME);
}

@Test
public void getSessionIdentifierByTokenId() throws Exception {

connection = DAOUtils.getConnection(DB_NAME);
identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class);

identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(false))
.thenReturn(connection);
assertEquals(accessTokenDAO.getSessionIdentifierByTokenId("2sa9a678f890877856y66e75f605d456"),
"4503eb1561bfd6bf237b7e05c15afaff21f511d81135423015a747ee7e3f0bc0");
}

private static void closeH2Base(String databaseName) throws Exception {

BasicDataSource dataSource = dataSourceMap.get(databaseName);
if (dataSource != null) {
dataSource.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
INSERT INTO IDN_OAUTH_CONSUMER_APPS (CONSUMER_KEY, CONSUMER_SECRET, USERNAME, TENANT_ID, USER_DOMAIN, APP_NAME,
OAUTH_VERSION, CALLBACK_URL, GRANT_TYPES, APP_STATE) VALUES
('ca19a540f544777860e44e75f605d924', '87n9a540f544777860e44e75f605d425', 'user1', 1234, 'PRIMARY',
'myApp', 'OAuth-2.0', 'http://localhost:8080/redirect',
'refresh_token implicit password iwa:ntlm client_credentials authorization_code', 'ACTIVE');

INSERT INTO IDN_OAUTH2_ACCESS_TOKEN (TOKEN_ID, ACCESS_TOKEN, REFRESH_TOKEN, CONSUMER_KEY_ID, AUTHZ_USER, TENANT_ID,
USER_DOMAIN, USER_TYPE, GRANT_TYPE, TIME_CREATED, REFRESH_TOKEN_TIME_CREATED, VALIDITY_PERIOD,
REFRESH_TOKEN_VALIDITY_PERIOD, TOKEN_SCOPE_HASH, TOKEN_STATE, TOKEN_STATE_ID, SUBJECT_IDENTIFIER,
ACCESS_TOKEN_HASH, REFRESH_TOKEN_HASH, IDP_ID, AUTHORIZED_ORGANIZATION) VALUES
('2sa9a678f890877856y66e75f605d456', 'd43e8da324a33bdc941b9b95cad6a6a2',
'2881c5a375d03dc0ba12787386451b29', 1, 'user1', 1234, 'PRIMARY', 'APPLICATION_USER', 'password', NOW(),
NOW(), 3600, 14400, '369db21a386ae433e65c0ff34d35708d', 'ACTIVE', 'NONE', 'user1', NULL, NULL, 1, 'NONE');

INSERT INTO IDN_OAUTH2_ACCESS_TOKEN_SCOPE (TOKEN_ID, TOKEN_SCOPE, TENANT_ID) VALUES
('2sa9a678f890877856y66e75f605d456', 'default', 1234);

INSERT INTO IDP (TENANT_ID, NAME, UUID) VALUES (1234, 'LOCAL', 5678);

INSERT INTO IDN_OAUTH2_TOKEN_BINDING (TOKEN_ID, TOKEN_BINDING_TYPE, TOKEN_BINDING_REF, TOKEN_BINDING_VALUE,
TENANT_ID) VALUES('2sa9a678f890877856y66e75f605d456', 'DEFAULT', 'bb9e4ed2cd4342ac0d6563e5fa6d2300',
'4503eb1561bfd6bf237b7e05c15afaff21f511d81135423015a747ee7e3f0bc0', '-1234');
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<class name="org.wso2.carbon.identity.oauth2.cache.JWKSCacheKeyTest"/>
<class name="org.wso2.carbon.identity.oauth2.cache.JWKSCacheTest"/>
<class name="org.wso2.carbon.identity.oauth2.dao.AccessContextTokenDOTest"/>
<class name="org.wso2.carbon.identity.oauth2.dao.AccessTokenDAOImplTest"/>
<class name="org.wso2.carbon.identity.oauth2.dao.AuthContextTokenDOTest"/>
<class name="org.wso2.carbon.identity.oauth2.dao.ScopeMgtDAOTest"/>
<class name="org.wso2.carbon.identity.oauth2.dao.AuthorizationCodeDAOImplTest"/>
Expand Down

0 comments on commit a4457de

Please sign in to comment.