diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml index c0762de9..86ea23cf 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/pom.xml @@ -62,6 +62,22 @@ org.wso2.financial.services.accelerator org.wso2.financial.services.accelerator.common + + + org.testng + testng + test + + + org.mockito + mockito-core + test + + + org.mockito + mockito-testng + test + @@ -118,6 +134,11 @@ **/*Constants.class **/*Component.class **/*DataHolder.class + **/*Cache.class + **/*CacheKey.class + **/*APIRequestContext.class + **/*APIResponseContext.class + **/*ExecutorError.class @@ -158,8 +179,7 @@ INSTRUCTION COVEREDRATIO - - + 0.8 diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java index a846a20e..c0cd0735 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionListenerImpl.java @@ -68,7 +68,7 @@ public ExtensionResponseDTO preProcessRequest(RequestContextDTO requestContextDT @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO postProcessRequest(RequestContextDTO requestContextDTO) { - Map contextProps = getPropertiesFromCache(requestContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(requestContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); FSAPIRequestContext fsapiRequestContext = new FSAPIRequestContext(requestContextDTO, contextProps); @@ -91,7 +91,7 @@ public ExtensionResponseDTO postProcessRequest(RequestContextDTO requestContextD @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO preProcessResponse(ResponseContextDTO responseContextDTO) { - Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); FSAPIResponseContext fsapiResponseContext = new FSAPIResponseContext(responseContextDTO, contextProps); for (FinancialServicesGatewayExecutor gatewayExecutor : @@ -113,7 +113,7 @@ public ExtensionResponseDTO preProcessResponse(ResponseContextDTO responseContex @Generated(message = "Ignoring since the method has covered in other tests") public ExtensionResponseDTO postProcessResponse(ResponseContextDTO responseContextDTO) { - Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + + Map contextProps = getPropertiesFromCache(responseContextDTO.getMsgInfo().getMessageId() + GatewayConstants.CONTEXT_PROP_CACHE_KEY); FSAPIResponseContext fsapiResponseContext = new FSAPIResponseContext(responseContextDTO, contextProps); for (FinancialServicesGatewayExecutor gatewayExecutor : @@ -145,13 +145,13 @@ protected ExtensionResponseDTO getResponseDTOForRequest(FSAPIRequestContext fsap extensionResponseDTO.setStatusCode(statusCode); extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); } else if (fsapiRequestContext.getContextProps().containsKey(GatewayConstants.IS_RETURN_RESPONSE) && - (boolean) (fsapiRequestContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { + Boolean.parseBoolean(fsapiRequestContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { Map headers = fsapiRequestContext.getMsgInfo().getHeaders(); headers.put(GatewayConstants.CONTENT_TYPE_TAG, GatewayConstants.JSON_CONTENT_TYPE); fsapiRequestContext.getMsgInfo().setHeaders(headers); extensionResponseDTO.setHeaders(headers); if (fsapiRequestContext.getContextProps().containsKey(GatewayConstants.MODIFIED_STATUS)) { - extensionResponseDTO.setStatusCode((int) (fsapiRequestContext.getContextProps() + extensionResponseDTO.setStatusCode(Integer.parseInt(fsapiRequestContext.getContextProps() .get(GatewayConstants.MODIFIED_STATUS))); } extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); @@ -185,14 +185,14 @@ protected ExtensionResponseDTO getResponseDTOForResponse(FSAPIResponseContext fs extensionResponseDTO.setStatusCode(statusCode); extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); } else if (fsapiResponseContext.getContextProps().containsKey(GatewayConstants.IS_RETURN_RESPONSE) && - (boolean) (fsapiResponseContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { + Boolean.parseBoolean(fsapiResponseContext.getContextProps().get(GatewayConstants.IS_RETURN_RESPONSE))) { Map headers = fsapiResponseContext.getMsgInfo().getHeaders(); headers.put(GatewayConstants.CONTENT_TYPE_TAG, GatewayConstants.JSON_CONTENT_TYPE); fsapiResponseContext.getMsgInfo().setHeaders(headers); extensionResponseDTO.setHeaders(headers); if (fsapiResponseContext.getContextProps().containsKey(GatewayConstants.MODIFIED_STATUS)) { - extensionResponseDTO.setStatusCode((int) (fsapiResponseContext.getContextProps() - .get(GatewayConstants.MODIFIED_STATUS))); + extensionResponseDTO.setStatusCode((Integer.parseInt(fsapiResponseContext.getContextProps() + .get(GatewayConstants.MODIFIED_STATUS)))); } extensionResponseDTO.setResponseStatus(ExtensionResponseStatus.RETURN_ERROR.toString()); } else { @@ -221,7 +221,7 @@ public String getType() { * @param key unique cache key * @param contextProps properties to store */ - private void setPropertiesToCache(String key, Map contextProps) { + private void setPropertiesToCache(String key, Map contextProps) { GatewayDataHolder.getGatewayCache().addToCache(GatewayCacheKey.of(key), contextProps); } @@ -232,9 +232,9 @@ private void setPropertiesToCache(String key, Map contextProps) * @param key unique cache key * @return context properties */ - private Map getPropertiesFromCache(String key) { + private Map getPropertiesFromCache(String key) { Object cachedObject = GatewayDataHolder.getGatewayCache().getFromCache(GatewayCacheKey.of(key)); - return cachedObject == null ? new HashMap<>() : (Map) cachedObject; + return cachedObject == null ? new HashMap<>() : (Map) cachedObject; } /** diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java index 5cc58b9e..3746337f 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIRequestContext.java @@ -48,7 +48,7 @@ public class FSAPIRequestContext extends RequestContextDTO { private static final Log log = LogFactory.getLog(FSAPIRequestContext.class); private final RequestContextDTO requestContextDTO; - private Map contextProps; + private Map contextProps; private String modifiedPayload; private String requestPayload; private Map addedHeaders; @@ -57,7 +57,7 @@ public class FSAPIRequestContext extends RequestContextDTO { private String consentId; private OpenAPI openAPI; - public FSAPIRequestContext(RequestContextDTO requestContextDTO, Map contextProps) { + public FSAPIRequestContext(RequestContextDTO requestContextDTO, Map contextProps) { this.requestContextDTO = requestContextDTO; this.contextProps = contextProps; @@ -115,12 +115,12 @@ public void setAddedHeaders(Map addedHeaders) { this.addedHeaders = addedHeaders; } - public Map getContextProps() { + public Map getContextProps() { return contextProps; } - public void setContextProps(Map contextProps) { + public void setContextProps(Map contextProps) { this.contextProps = contextProps; } diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java index f7c4a809..562a6d77 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/executor/model/FSAPIResponseContext.java @@ -41,14 +41,14 @@ public class FSAPIResponseContext extends ResponseContextDTO { private static final Log log = LogFactory.getLog(FSAPIResponseContext.class); private ResponseContextDTO responseContextDTO; - private Map contextProps; + private Map contextProps; private String responsePayload; private String modifiedPayload; private Map addedHeaders; private boolean isError; private ArrayList errors; - public FSAPIResponseContext(ResponseContextDTO responseContextDTO, Map contextProps) { + public FSAPIResponseContext(ResponseContextDTO responseContextDTO, Map contextProps) { this.responseContextDTO = responseContextDTO; this.contextProps = contextProps; @@ -115,12 +115,12 @@ public void setAddedHeaders(Map addedHeaders) { this.addedHeaders = addedHeaders; } - public Map getContextProps() { + public Map getContextProps() { return contextProps; } - public void setContextProps(Map contextProps) { + public void setContextProps(Map contextProps) { this.contextProps = contextProps; } diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java index b83effd4..9c7ddc3b 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/main/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtils.java @@ -124,7 +124,7 @@ public static String getAPIMgtConfig(String key) { } /** - * Method to obatain basic auth header. + * Method to obtain basic auth header. * * @param username Username of Auth header * @param password Password of Auth header diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/GatewayTestConstants.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/GatewayTestConstants.java new file mode 100644 index 00000000..4629ba3c --- /dev/null +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/GatewayTestConstants.java @@ -0,0 +1,49 @@ +/** +* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). +*

+* WSO2 LLC. 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.financial.services.accelerator.gateway; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Constants for Gateway test cases. + */ +public class GatewayTestConstants { + + public static final String VALID_EXECUTOR_CLASS = + "org.wso2.financial.services.accelerator.gateway.executor.core.MockOBExecutor"; + public static final Map VALID_EXECUTOR_MAP = Stream.of( + new AbstractMap.SimpleImmutableEntry<>(1, VALID_EXECUTOR_CLASS)) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + public static final Map> FULL_VALIDATOR_MAP = Stream.of( + new AbstractMap.SimpleImmutableEntry<>("Default", VALID_EXECUTOR_MAP), + new AbstractMap.SimpleImmutableEntry<>("DCR", VALID_EXECUTOR_MAP)) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + public static final String CUSTOM_PAYLOAD = "{\"custom\":\"payload\"}"; + public static final String B64_PAYLOAD = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva" + + "G4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ"; + public static final String TEST_JWT = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwI" + + "iwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; + public static final String XML_PAYLOAD = "" + + "Test Content2024-09-30" + + ""; +} diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouterTest.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouterTest.java new file mode 100644 index 00000000..296f0849 --- /dev/null +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/DefaultRequestRouterTest.java @@ -0,0 +1,141 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. 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.financial.services.accelerator.gateway.executor.core; + +import io.swagger.v3.oas.models.OpenAPI; +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.apimgt.common.gateway.dto.MsgInfoDTO; +import org.wso2.financial.services.accelerator.common.util.FinancialServicesUtils; +import org.wso2.financial.services.accelerator.gateway.GatewayTestConstants; +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIRequestContext; +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIResponseContext; +import org.wso2.financial.services.accelerator.gateway.util.GatewayConstants; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Test for default request router. + */ +public class DefaultRequestRouterTest { + + DefaultRequestRouter defaultRequestRouter; + OpenAPI openAPI; + + @BeforeClass + public void beforeClass() { + + defaultRequestRouter = new DefaultRequestRouter(); + defaultRequestRouter.setExecutorMap(initExecutors()); + openAPI = new OpenAPI(); + openAPI.setExtensions(new HashMap<>()); + } + + @Test(priority = 1) + public void testDCRRequestsForRouter() { + + FSAPIRequestContext obapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + FSAPIResponseContext obapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setResource("/anyAPIcall/register"); + Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI); + Mockito.when(obapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForRequest(obapiRequestContext)); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForResponse(obapiResponseContext)); + + } + + @Test(priority = 1) + public void testAccountRequestsForRouter() { + + FSAPIRequestContext obapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + FSAPIResponseContext obapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setResource("/anyAPIcall"); + Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI); + Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForRequest(obapiRequestContext)); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForResponse(obapiResponseContext)); + } + + @Test(priority = 2) + public void testNonRegulatoryAPIcall() { + + FSAPIRequestContext obapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + FSAPIResponseContext obapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setResource("/anyAPIcall"); + Map extensions = new HashMap<>(); + Map contextProps = new HashMap<>(); + extensions.put(GatewayConstants.API_TYPE_CUSTOM_PROP, GatewayConstants.API_TYPE_NON_REGULATORY); + contextProps.put(GatewayConstants.API_TYPE_CUSTOM_PROP, GatewayConstants.API_TYPE_NON_REGULATORY); + openAPI.setExtensions(extensions); + Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI); + Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps); + Assert.assertEquals(defaultRequestRouter.getExecutorsForRequest(obapiRequestContext).size(), 0); + Assert.assertEquals(defaultRequestRouter.getExecutorsForResponse(obapiResponseContext).size(), 0); + } + + @Test(priority = 2) + public void testRegulatoryAPIcall() { + + FSAPIRequestContext obapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + FSAPIResponseContext obapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setResource("/anyAPIcall"); + Map extensions = new HashMap<>(); + Map contextProps = new HashMap<>(); + extensions.put(GatewayConstants.API_TYPE_CUSTOM_PROP, GatewayConstants.API_TYPE_NON_REGULATORY); + contextProps.put(GatewayConstants.API_TYPE_CUSTOM_PROP, "regulatory"); + openAPI.setExtensions(extensions); + Mockito.when(obapiRequestContext.getOpenAPI()).thenReturn(openAPI); + Mockito.when(obapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(obapiResponseContext.getContextProps()).thenReturn(contextProps); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForRequest(obapiRequestContext)); + Assert.assertNotNull(defaultRequestRouter.getExecutorsForResponse(obapiResponseContext)); + } + + public static Map> initExecutors() { + + Map> executors = new HashMap<>(); + Map> fullValidatorMap = GatewayTestConstants.FULL_VALIDATOR_MAP; + for (Map.Entry> stringMapEntry : fullValidatorMap.entrySet()) { + List executorList = new ArrayList<>(); + Map executorNames = stringMapEntry.getValue(); + for (Map.Entry executorEntity : executorNames.entrySet()) { + FinancialServicesGatewayExecutor object = (FinancialServicesGatewayExecutor) + FinancialServicesUtils.getClassInstanceFromFQN(executorEntity.getValue()); + executorList.add(object); + } + executors.put(stringMapEntry.getKey(), executorList); + } + return executors; + + } +} diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionImplTest.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionImplTest.java new file mode 100644 index 00000000..ec8e1a68 --- /dev/null +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/FSExtensionImplTest.java @@ -0,0 +1,233 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. 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.financial.services.accelerator.gateway.executor.core; + +import org.apache.http.HttpStatus; +import org.json.JSONObject; +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.wso2.carbon.apimgt.common.gateway.dto.ExtensionResponseDTO; +import org.wso2.carbon.apimgt.common.gateway.dto.ExtensionResponseStatus; +import org.wso2.carbon.apimgt.common.gateway.dto.MsgInfoDTO; +import org.wso2.financial.services.accelerator.gateway.GatewayTestConstants; +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIRequestContext; +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIResponseContext; +import org.wso2.financial.services.accelerator.gateway.util.GatewayConstants; + +import java.util.HashMap; +import java.util.Map; + +/** + * Test for open Banking extension implementation. + */ +public class FSExtensionImplTest { + + private static FSAPIRequestContext fsapiRequestContext; + private static FSAPIResponseContext fsapiResponseContext; + private static final FSExtensionListenerImpl fsExtensionListener = new FSExtensionListenerImpl(); + + @BeforeClass + public static void beforeClass() { + + } + + @Test(priority = 1) + public void testMinimumFlow() { + + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setHeaders(new HashMap<>()); + + fsapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + Mockito.when(fsapiRequestContext.isError()).thenReturn(false); + Mockito.when(fsapiRequestContext.getModifiedPayload()).thenReturn(null); + Mockito.when(fsapiRequestContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + + fsapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + Mockito.when(fsapiResponseContext.isError()).thenReturn(false); + Mockito.when(fsapiResponseContext.getModifiedPayload()).thenReturn(null); + Mockito.when(fsapiResponseContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + + ExtensionResponseDTO responseDTOForRequest = fsExtensionListener.getResponseDTOForRequest(fsapiRequestContext); + Assert.assertEquals(responseDTOForRequest.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertNull(responseDTOForRequest.getHeaders()); + Assert.assertNull(responseDTOForRequest.getPayload()); + Assert.assertNull(responseDTOForRequest.getCustomProperty()); + Assert.assertEquals(responseDTOForRequest.getStatusCode(), 0); + + ExtensionResponseDTO responseDTOForResponse = + fsExtensionListener.getResponseDTOForResponse(fsapiResponseContext); + + Assert.assertEquals(responseDTOForResponse.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertNull(responseDTOForResponse.getHeaders()); + Assert.assertNull(responseDTOForResponse.getPayload()); + Assert.assertNull(responseDTOForResponse.getCustomProperty()); + Assert.assertEquals(responseDTOForResponse.getStatusCode(), 0); + + } + + @Test(priority = 1) + public void testAddedHeaders() { + + fsapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + fsapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + Mockito.when(fsapiRequestContext.isError()).thenReturn(false); + Mockito.when(fsapiRequestContext.getModifiedPayload()).thenReturn(null); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setHeaders(new HashMap<>()); + Mockito.when(fsapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Map addedHeaders = new HashMap<>(); + addedHeaders.put("custom", "header"); + Mockito.when(fsapiRequestContext.getAddedHeaders()).thenReturn(addedHeaders); + + ExtensionResponseDTO responseDTOForRequest = fsExtensionListener.getResponseDTOForRequest(fsapiRequestContext); + Assert.assertEquals(responseDTOForRequest.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertEquals(responseDTOForRequest.getHeaders().get("custom"), "header"); + Assert.assertNull(responseDTOForRequest.getPayload()); + Assert.assertNull(responseDTOForRequest.getCustomProperty()); + Assert.assertEquals(responseDTOForRequest.getStatusCode(), 0); + + Mockito.when(fsapiResponseContext.isError()).thenReturn(false); + Mockito.when(fsapiResponseContext.getModifiedPayload()).thenReturn(null); + Mockito.when(fsapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(fsapiResponseContext.getAddedHeaders()).thenReturn(addedHeaders); + + ExtensionResponseDTO responseDTOForResponse = + fsExtensionListener.getResponseDTOForResponse(fsapiResponseContext); + Assert.assertEquals(responseDTOForResponse.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertEquals(responseDTOForResponse.getHeaders().get("custom"), "header"); + Assert.assertNull(responseDTOForResponse.getPayload()); + Assert.assertNull(responseDTOForResponse.getCustomProperty()); + Assert.assertEquals(responseDTOForResponse.getStatusCode(), 0); + + } + + @Test(priority = 1) + public void testModifiedPayload() { + + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setHeaders(new HashMap<>()); + + fsapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + fsapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + Mockito.when(fsapiRequestContext.isError()).thenReturn(false); + Mockito.when(fsapiRequestContext.getModifiedPayload()).thenReturn(GatewayTestConstants.CUSTOM_PAYLOAD); + Mockito.when(fsapiRequestContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + + ExtensionResponseDTO responseDTOForRequest = fsExtensionListener.getResponseDTOForRequest(fsapiRequestContext); + Assert.assertEquals(responseDTOForRequest.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertNull(responseDTOForRequest.getHeaders()); + Assert.assertNotNull(responseDTOForRequest.getPayload()); + Assert.assertNull(responseDTOForRequest.getCustomProperty()); + Assert.assertEquals(responseDTOForRequest.getStatusCode(), 0); + + Mockito.when(fsapiResponseContext.isError()).thenReturn(false); + Mockito.when(fsapiResponseContext.getModifiedPayload()).thenReturn(GatewayTestConstants.CUSTOM_PAYLOAD); + Mockito.when(fsapiResponseContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + + ExtensionResponseDTO responseDTOForResponse = + fsExtensionListener.getResponseDTOForResponse(fsapiResponseContext); + Assert.assertEquals(responseDTOForResponse.getResponseStatus(), ExtensionResponseStatus.CONTINUE.toString()); + Assert.assertNull(responseDTOForResponse.getHeaders()); + Assert.assertNotNull(responseDTOForResponse.getPayload()); + Assert.assertNull(responseDTOForResponse.getCustomProperty()); + Assert.assertEquals(responseDTOForResponse.getStatusCode(), 0); + } + + @Test(priority = 1) + public void testErrorFlow() { + + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setHeaders(new HashMap<>()); + + fsapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + Mockito.when(fsapiRequestContext.isError()).thenReturn(true); + JSONObject errorJSON = new JSONObject(); + errorJSON.put("error", true); + Mockito.when(fsapiRequestContext.getModifiedPayload()).thenReturn(errorJSON.toString()); + Mockito.when(fsapiRequestContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + + fsapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + Mockito.when(fsapiResponseContext.isError()).thenReturn(true); + Mockito.when(fsapiResponseContext.getModifiedPayload()).thenReturn(errorJSON.toString()); + Mockito.when(fsapiResponseContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + + ExtensionResponseDTO responseDTOForRequest = fsExtensionListener.getResponseDTOForRequest(fsapiRequestContext); + Assert.assertEquals(responseDTOForRequest.getResponseStatus(), ExtensionResponseStatus.RETURN_ERROR.toString()); + Assert.assertNull(responseDTOForRequest.getHeaders()); + Assert.assertNotNull(responseDTOForRequest.getPayload()); + Assert.assertNull(responseDTOForRequest.getCustomProperty()); + Assert.assertEquals(responseDTOForRequest.getStatusCode(), 500); + + ExtensionResponseDTO responseDTOForResponse = + fsExtensionListener.getResponseDTOForResponse(fsapiResponseContext); + + Assert.assertEquals(responseDTOForResponse.getResponseStatus(), + ExtensionResponseStatus.RETURN_ERROR.toString()); + Assert.assertNull(responseDTOForResponse.getHeaders()); + Assert.assertNotNull(responseDTOForResponse.getPayload()); + Assert.assertNull(responseDTOForResponse.getCustomProperty()); + Assert.assertEquals(responseDTOForResponse.getStatusCode(), 500); + + } + + @Test(priority = 1) + public void testFlowWithReturnResponseTrue() { + + Map contextProps = new HashMap<>(); + contextProps.put(GatewayConstants.IS_RETURN_RESPONSE, "true"); + contextProps.put(GatewayConstants.MODIFIED_STATUS, String.valueOf(HttpStatus.SC_CREATED)); + MsgInfoDTO msgInfoDTO = new MsgInfoDTO(); + msgInfoDTO.setHeaders(new HashMap<>()); + + fsapiRequestContext = Mockito.mock(FSAPIRequestContext.class); + Mockito.when(fsapiRequestContext.isError()).thenReturn(false); + Mockito.when(fsapiRequestContext.getModifiedPayload()).thenReturn(null); + Mockito.when(fsapiRequestContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiRequestContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(fsapiRequestContext.getContextProps()).thenReturn(contextProps); + + fsapiResponseContext = Mockito.mock(FSAPIResponseContext.class); + Mockito.when(fsapiResponseContext.isError()).thenReturn(false); + Mockito.when(fsapiResponseContext.getModifiedPayload()).thenReturn(null); + Mockito.when(fsapiResponseContext.getAddedHeaders()).thenReturn(new HashMap<>()); + Mockito.when(fsapiResponseContext.getMsgInfo()).thenReturn(msgInfoDTO); + Mockito.when(fsapiResponseContext.getContextProps()).thenReturn(contextProps); + + ExtensionResponseDTO responseDTOForRequest = fsExtensionListener.getResponseDTOForRequest(fsapiRequestContext); + Assert.assertEquals(responseDTOForRequest.getResponseStatus(), ExtensionResponseStatus.RETURN_ERROR.toString()); + Assert.assertEquals(responseDTOForRequest.getStatusCode(), HttpStatus.SC_CREATED); + + ExtensionResponseDTO responseDTOForResponse = + fsExtensionListener.getResponseDTOForResponse(fsapiResponseContext); + + Assert.assertEquals(responseDTOForResponse.getResponseStatus(), + ExtensionResponseStatus.RETURN_ERROR.toString()); + Assert.assertEquals(responseDTOForResponse.getStatusCode(), HttpStatus.SC_CREATED); + + } + +} diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/MockOBExecutor.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/MockOBExecutor.java new file mode 100644 index 00000000..55cc2eb7 --- /dev/null +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/executor/core/MockOBExecutor.java @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. 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.financial.services.accelerator.gateway.executor.core; + + +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIRequestContext; +import org.wso2.financial.services.accelerator.gateway.executor.model.FSAPIResponseContext; + +/** + * Mock Open banking executor for testing. + */ +public class MockOBExecutor implements FinancialServicesGatewayExecutor { + + @Override + public void preProcessRequest(FSAPIRequestContext fsapiRequestContext) { + + fsapiRequestContext.setModifiedPayload("{}"); + } + + /** + * Method to handle post request. + * + * @param fsapiRequestContext FS request context object + */ + @Override + public void postProcessRequest(FSAPIRequestContext fsapiRequestContext) { + + } + + + /** + * Method to handle pre response. + * + * @param fsapiResponseContext FS response context object + */ + @Override + public void preProcessResponse(FSAPIResponseContext fsapiResponseContext) { + + } + + /** + * Method to handle post response. + * + * @param fsapiResponseContext FS response context object + */ + @Override + public void postProcessResponse(FSAPIResponseContext fsapiResponseContext) { + + } + +} diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtilsTest.java b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtilsTest.java new file mode 100644 index 00000000..bf07c177 --- /dev/null +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/java/org/wso2/financial/services/accelerator/gateway/util/GatewayUtilsTest.java @@ -0,0 +1,108 @@ +/** + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + *

+ * WSO2 LLC. 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.financial.services.accelerator.gateway.util; + +import org.json.JSONObject; +import org.testng.Assert; +import org.testng.annotations.Test; +import org.wso2.financial.services.accelerator.gateway.GatewayTestConstants; + +import java.io.UnsupportedEncodingException; + +/** + * Test class for Gateway utility methods. + */ +public class GatewayUtilsTest { + + @Test(priority = 1) + public void testIsValidJWTToken() { + + Assert.assertTrue(GatewayUtils.isValidJWTToken(GatewayTestConstants.TEST_JWT)); + } + + @Test(priority = 2) + public void testB64Encode() throws UnsupportedEncodingException { + + JSONObject payload = GatewayUtils.decodeBase64(GatewayTestConstants.B64_PAYLOAD); + Assert.assertEquals(payload.getString("sub"), "1234567890"); + Assert.assertEquals(payload.getString("name"), "John Doe"); + Assert.assertEquals(payload.getInt("iat"), 1516239022); + } + + @Test(priority = 3) + public void testBasicAuthHeader() { + + Assert.assertEquals(GatewayUtils.getBasicAuthHeader("admin", "admin"), + "Basic YWRtaW46YWRtaW4="); + } + + @Test(priority = 4) + public void testGetTextPayload() { + + Assert.assertEquals(GatewayUtils.getTextPayload(GatewayTestConstants.XML_PAYLOAD), "Test Content"); + } + + @Test (priority = 5) + public void testIsEligibleRequest() { + + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.JSON_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.APPLICATION_XML_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.TEXT_XML_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.JSON_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.APPLICATION_XML_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleRequest(GatewayConstants.TEXT_XML_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + } + + @Test (priority = 6) + public void testIsEligibleResponse() { + + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.JSON_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.APPLICATION_XML_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.TEXT_XML_CONTENT_TYPE, + GatewayConstants.POST_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.JSON_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.APPLICATION_XML_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.TEXT_XML_CONTENT_TYPE, + GatewayConstants.PUT_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.JSON_CONTENT_TYPE, + GatewayConstants.GET_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.APPLICATION_XML_CONTENT_TYPE, + GatewayConstants.GET_HTTP_METHOD)); + Assert.assertTrue(GatewayUtils.isEligibleResponse(GatewayConstants.TEXT_XML_CONTENT_TYPE, + GatewayConstants.GET_HTTP_METHOD)); + } + + @Test(priority = 7) + public void testJWTPayloadLoad() { + + Assert.assertEquals(GatewayUtils.getPayloadFromJWT(GatewayTestConstants.TEST_JWT), + GatewayTestConstants.B64_PAYLOAD); + } + +} diff --git a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/resources/testng.xml b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/resources/testng.xml index 5749fe3c..8e4cd5f5 100644 --- a/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/resources/testng.xml +++ b/financial-services-accelerator/components/org.wso2.financial.services.accelerator.gateway/src/test/resources/testng.xml @@ -19,5 +19,10 @@ + + + + +