From 0bd4e89deda32fe8bc606f5b624020eb97d1667f Mon Sep 17 00:00:00 2001 From: zhangfan345 Date: Mon, 5 Sep 2022 10:58:36 +0800 Subject: Modify the path of test Issue-ID: USECASEUI-716 Signed-off-by: zhangfan345 Change-Id: I284bb1dd4176743a9277a00827857400b5d8f390 --- .../IntentAnalysisApplicationTests.java | 53 ++++++ .../adapters/policy/PolicyServiceTest.java | 182 +++++++++++++++++++++ .../intentanalysis/service/IntentServiceTest.java | 170 +++++++++++++++++++ .../test/IntentAnalysisApplicationTests.java | 53 ------ .../test/adapters/policy/PolicyServiceTest.java | 182 --------------------- .../test/service/IntentServiceTest.java | 171 ------------------- .../test/util/RestFulServicesTest.java | 78 --------- .../intentanalysis/test/util/TestCall.java | 92 ----------- .../intentanalysis/util/RestFulServicesTest.java | 58 +++++++ .../usecaseui/intentanalysis/util/TestCall.java | 92 +++++++++++ 10 files changed, 555 insertions(+), 576 deletions(-) create mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplicationTests.java create mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyServiceTest.java create mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/IntentServiceTest.java delete mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/IntentAnalysisApplicationTests.java delete mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/adapters/policy/PolicyServiceTest.java delete mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/service/IntentServiceTest.java delete mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/RestFulServicesTest.java delete mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/TestCall.java create mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/RestFulServicesTest.java create mode 100644 intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/TestCall.java diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplicationTests.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplicationTests.java new file mode 100644 index 0000000..965ec6d --- /dev/null +++ b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/IntentAnalysisApplicationTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.usecaseui.intentanalysis; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.mybatis.spring.annotation.MapperScan; +import org.onap.usecaseui.intentanalysis.util.SpringContextUtil; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.filter.OncePerRequestFilter; + +@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}, scanBasePackages = "org.onap.usecaseui.intentanalysis") +@MapperScan(basePackages = {"org.onap.usecaseui.intentanalysis.mapper"}) +@EnableScheduling + +public class IntentAnalysisApplicationTests { + + public static void main(String[] args) throws Exception { + ApplicationContext applicationContext = SpringApplication.run(IntentAnalysisApplicationTests.class, args); + SpringContextUtil.setApplicationContext(applicationContext); + } + + @Bean + public OncePerRequestFilter accessTokenFilter() { + return new OncePerRequestFilter() { + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, + FilterChain filterChain) throws ServletException, IOException { + filterChain.doFilter(request, response); + } + }; + } +} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyServiceTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyServiceTest.java new file mode 100644 index 0000000..ea253e7 --- /dev/null +++ b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/adapters/policy/PolicyServiceTest.java @@ -0,0 +1,182 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.usecaseui.intentanalysis.adapters.policy; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +import java.io.IOException; +import mockit.MockUp; +import okhttp3.MediaType; +import okhttp3.ResponseBody; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall; +import org.onap.usecaseui.intentanalysis.adapters.policy.impl.PolicyServiceImpl; +import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests; +import org.onap.usecaseui.intentanalysis.util.TestCall; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest(classes = IntentAnalysisApplicationTests.class) +@RunWith(SpringRunner.class) +public class PolicyServiceTest { + + private static final int CREATE_POLICY_TYPE_FAILED = 0x01; + + private static final int CREATE_POLICY_FAILED = 0x02; + + private static final int DEPLOY_POLICY_FAILED = 0x04; + + private static final int UNDEPLOY_POLICY_FAILED = 0x08; + + private static final int DELETE_POLICY_FAILED = 0x10; + + private static final int QUERY_POLICY_NOT_EXIST = 0x20; + + private static final Logger LOGGER = LoggerFactory.getLogger(PolicyServiceTest.class); + + @Autowired + private PolicyServiceImpl policyService; + + private PolicyAPICall policyAPICall; + + private MockUp mockup; + + @Test + public void testCreateAndDeployCLLPolicySuccess() throws IOException { + mockUpPolicyApiCall(0); + boolean result = policyService.createAndDeployModifyCLLPolicy(); + Assert.assertTrue(result); + } + + + @Test + public void testCreateAndDeployCLLPolicyFailedCreateFailed() throws IOException { + mockUpPolicyApiCall(CREATE_POLICY_FAILED); + boolean result = policyService.createAndDeployModifyCLLPolicy(); + Assert.assertFalse(result); + } + + @Test + public void testCreateAndDeployCLLPolicyFailedDeployFailed() throws IOException { + mockUpPolicyApiCall(DEPLOY_POLICY_FAILED); + boolean result = policyService.createAndDeployModifyCLLPolicy(); + Assert.assertFalse(result); + } + + @Test + public void testUndeployAndRemoveCLLPolicySuccess() throws IOException { + mockUpPolicyApiCall(0); + boolean result = policyService.undeployAndRemoveModifyCLLPolicy(); + Assert.assertTrue(result); + } + + @Test + public void testUndeployAndRemoveCLLPolicySuccessPolicyNotExist() throws IOException { + mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST); + boolean result = policyService.undeployAndRemoveModifyCLLPolicy(); + Assert.assertTrue(result); + } + + @Test + public void testUpdateIntentConfigPolicySuccess() throws IOException { + mockUpPolicyApiCall(0); + boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); + Assert.assertTrue(result); + } + + @Test + public void testUpdateIntentConfigPolicySuccessPolicyNotExist(){ + mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST); + boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); + Assert.assertTrue(result); + } + + @Test + public void testUpdateIntentConfigPolicyFailedCreatePolicyTypeFailed(){ + mockUpPolicyApiCall(CREATE_POLICY_TYPE_FAILED); + boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); + Assert.assertFalse(result); + } + + @Test + public void testUpdateIntentConfigPolicyFailedCreatePolicyFailed(){ + mockUpPolicyApiCall(CREATE_POLICY_FAILED); + boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); + Assert.assertFalse(result); + } + + @Test + public void testUpdateIntentConfigPolicyFailedDeployPolicyFailed(){ + mockUpPolicyApiCall(DEPLOY_POLICY_FAILED); + boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); + Assert.assertFalse(result); + } + + private void mockUpPolicyApiCall(int failedFlag) { + policyAPICall = mock(PolicyAPICall.class); + policyService.setPolicyAPICall(policyAPICall); + ResponseBody mockedSuccessResponse = ResponseBody.create(MediaType.parse("application/json"), + "Test Success Result"); + if ((CREATE_POLICY_FAILED & failedFlag) > 0) { + when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn( + TestCall.failedCall("Create policy failed")); + } else { + when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn( + TestCall.successfulCall(mockedSuccessResponse)); + } + if ((CREATE_POLICY_TYPE_FAILED & failedFlag) > 0) { + when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.failedCall("Create policy type failed")); + } else { + when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); + } + + if ((DEPLOY_POLICY_FAILED & failedFlag) > 0) { + when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.failedCall("Deploy policy failed")); + } else { + when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); + } + + if ((UNDEPLOY_POLICY_FAILED & failedFlag) > 0) { + when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.failedCall("Undeploy policy failed")); + } else { + when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); + } + + if ((DELETE_POLICY_FAILED & failedFlag) > 0) { + when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn( + TestCall.failedCall("Delete policy failed")); + } else { + when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn( + TestCall.successfulCall(mockedSuccessResponse)); + } + + if ((QUERY_POLICY_NOT_EXIST & failedFlag) > 0) { + when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn( + TestCall.failedCall("Get policy failed")); + } else { + when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn( + TestCall.successfulCall(mockedSuccessResponse)); + } + } +} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/IntentServiceTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/IntentServiceTest.java new file mode 100644 index 0000000..9e93b87 --- /dev/null +++ b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/service/IntentServiceTest.java @@ -0,0 +1,170 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.usecaseui.intentanalysis.service; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.usecaseui.intentanalysis.bean.enums.*; +import org.onap.usecaseui.intentanalysis.bean.models.Intent; +import org.onap.usecaseui.intentanalysis.bean.models.Condition; +import org.onap.usecaseui.intentanalysis.bean.models.FulfilmentInfo; +import org.onap.usecaseui.intentanalysis.bean.models.Context; +import org.onap.usecaseui.intentanalysis.bean.models.Expectation; +import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget; +import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject; +import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests; +import org.onap.usecaseui.intentanalysis.util.SpringContextUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest(classes = IntentAnalysisApplicationTests.class) +@RunWith(SpringRunner.class) +public class IntentServiceTest extends AbstractJUnit4SpringContextTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(IntentServiceTest.class); + + private static final String TEST_INTENT_ID_1 = "intentId1"; + + private static final String TEST_INTENT_ID_2 = "intentId2"; + + private static final String TEST_INTENT_NAME = "CLL Business intent"; + + @Autowired + private IntentService intentService; + + @Before + public void setUp() { + SpringContextUtil.setApplicationContext(applicationContext); + } + + @Test + public void testCreateIntentSuccess() throws IOException { + Intent intent = new Intent(); + Expectation expectation1 = new Expectation(); + ExpectationTarget target1 = new ExpectationTarget(); + ExpectationObject object1 = new ExpectationObject(); + Context intentContext = new Context(); + FulfilmentInfo intentFulfilmentInfo = new FulfilmentInfo(); + Condition targetCondition1 = new Condition(); + targetCondition1.setConditionId("conditionId"); + targetCondition1.setConditionName("conditionName"); + targetCondition1.setOperator(OperatorType.valueOf("EQUALTO")); + targetCondition1.setConditionValue("conditionValue"); + List targetConditionList = new ArrayList<>(); + targetConditionList.add(targetCondition1); + target1.setTargetId("targetId"); + target1.setTargetName("targetName"); + target1.setTargetConditions(targetConditionList); + List expectationTargetList = new ArrayList<>(); + expectationTargetList.add(target1); + object1.setObjectType(ObjectType.valueOf("OBJECT1")); + object1.setObjectInstance("objectInstance"); + expectation1.setExpectationId("expectationId"); + expectation1.setExpectationName("expectationName"); + expectation1.setExpectationType(ExpectationType.valueOf("DELIVERY")); + expectation1.setExpectationObject(object1); + expectation1.setExpectationTargets(expectationTargetList); + List expectationList = new ArrayList<>(); + expectationList.add(expectation1); + intentContext.setContextId("intentContextId"); + intentContext.setContextName("intentContextName"); + List intentContextList = new ArrayList<>(); + intentContextList.add(intentContext); + intentFulfilmentInfo.setFulfilmentStatus(FulfilmentStatus.valueOf("NOT_FULFILLED")); + intentFulfilmentInfo.setNotFulfilledReason("NotFulfilledReason"); + intentFulfilmentInfo.setNotFulfilledState(NotFulfilledState.valueOf("COMPLIANT")); + intent.setIntentId("testIntentId"); + intent.setIntentName("testIntentName"); + intent.setIntentContexts(intentContextList); + intent.setIntentExpectations(expectationList); + intent.setIntentFulfilmentInfo(intentFulfilmentInfo); + + Intent intentTmp = intentService.createIntent(intent); + Assert.assertNotNull(intentTmp); + } + + @Test + public void testGetIntentListSuccess() { + List intentList = intentService.getIntentList(); + Assert.assertNotNull(intentList); + } + + @Test + public void testGetIntentSuccess() { + Intent intent = intentService.getIntent(TEST_INTENT_ID_1); + Assert.assertNotNull(intent); + } + + @Test + public void testGetIntentByNameSuccess() { + List intentList = intentService.getIntentByName(TEST_INTENT_NAME); + Assert.assertNotNull(intentList); + + } + + @Test + public void testUpdateIntentSuccess() { + Intent intent = intentService.getIntent(TEST_INTENT_ID_1); + intent.setIntentName("new intent name"); + List contextList = intent.getIntentContexts(); + Context intentContext = contextList.get(0); + intentContext.setContextName("new context name"); + contextList.set(0, intentContext); + intent.setIntentContexts(contextList); + FulfilmentInfo intentFulfilmentInfo = intent.getIntentFulfilmentInfo(); + intentFulfilmentInfo.setNotFulfilledReason("new reason"); + intent.setIntentFulfilmentInfo(intentFulfilmentInfo); + List expectationList = intent.getIntentExpectations(); + Expectation expectation = expectationList.get(0); + expectation.setExpectationName("new expectation name"); + ExpectationObject expectationObject = expectation.getExpectationObject(); + expectationObject.setObjectInstance("new object instance"); + expectation.setExpectationObject(expectationObject); + List expectationTargetList = expectation.getExpectationTargets(); + ExpectationTarget expectationTarget = expectationTargetList.get(0); + expectationTarget.setTargetName("new target name"); + List targetConditionList = expectationTarget.getTargetConditions(); + Condition targetCondition = targetConditionList.get(0); + targetCondition.setConditionName("new conditon name"); + targetConditionList.set(0, targetCondition); + expectationTarget.setTargetConditions(targetConditionList); + expectationTargetList.remove(2); + expectationTargetList.set(0, expectationTarget); + expectation.setExpectationTargets(expectationTargetList); + expectationList.set(0, expectation); + intent.setIntentExpectations(expectationList); + + Intent updatedIntent = intentService.updateIntent(intent); + Assert.assertEquals("new intent name", updatedIntent.getIntentName()); + + } + + @Test + public void testDeleteIntentSuccess() { + intentService.deleteIntent(TEST_INTENT_ID_2); + Intent intent = intentService.getIntent(TEST_INTENT_ID_2); + Assert.assertNull(intent); + } +} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/IntentAnalysisApplicationTests.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/IntentAnalysisApplicationTests.java deleted file mode 100644 index 529f08d..0000000 --- a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/IntentAnalysisApplicationTests.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2022 Huawei Technologies Co., Ltd. - * - * Licensed 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.onap.usecaseui.intentanalysis.test; - -import java.io.IOException; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.mybatis.spring.annotation.MapperScan; -import org.onap.usecaseui.intentanalysis.util.SpringContextUtil; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.filter.OncePerRequestFilter; - -@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}, scanBasePackages = "org.onap.usecaseui.intentanalysis") -@MapperScan(basePackages = {"org.onap.usecaseui.intentanalysis.mapper"}) -@EnableScheduling - -public class IntentAnalysisApplicationTests { - - public static void main(String[] args) throws Exception { - ApplicationContext applicationContext = SpringApplication.run(IntentAnalysisApplicationTests.class, args); - SpringContextUtil.setApplicationContext(applicationContext); - } - - @Bean - public OncePerRequestFilter accessTokenFilter() { - return new OncePerRequestFilter() { - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, - FilterChain filterChain) throws ServletException, IOException { - filterChain.doFilter(request, response); - } - }; - } -} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/adapters/policy/PolicyServiceTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/adapters/policy/PolicyServiceTest.java deleted file mode 100644 index 11084e3..0000000 --- a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/adapters/policy/PolicyServiceTest.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright 2022 Huawei Technologies Co., Ltd. - * - * Licensed 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.onap.usecaseui.intentanalysis.test.adapters.policy; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - - -import java.io.IOException; -import mockit.MockUp; -import okhttp3.MediaType; -import okhttp3.ResponseBody; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall; -import org.onap.usecaseui.intentanalysis.adapters.policy.impl.PolicyServiceImpl; -import org.onap.usecaseui.intentanalysis.test.IntentAnalysisApplicationTests; -import org.onap.usecaseui.intentanalysis.test.util.TestCall; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@SpringBootTest(classes = IntentAnalysisApplicationTests.class) -@RunWith(SpringRunner.class) -public class PolicyServiceTest { - - private static final int CREATE_POLICY_TYPE_FAILED = 0x01; - - private static final int CREATE_POLICY_FAILED = 0x02; - - private static final int DEPLOY_POLICY_FAILED = 0x04; - - private static final int UNDEPLOY_POLICY_FAILED = 0x08; - - private static final int DELETE_POLICY_FAILED = 0x10; - - private static final int QUERY_POLICY_NOT_EXIST = 0x20; - - private static final Logger LOGGER = LoggerFactory.getLogger(PolicyServiceTest.class); - - @Autowired - private PolicyServiceImpl policyService; - - private PolicyAPICall policyAPICall; - - private MockUp mockup; - - @Test - public void testCreateAndDeployCLLPolicySuccess() throws IOException { - mockUpPolicyApiCall(0); - boolean result = policyService.createAndDeployModifyCLLPolicy(); - Assert.assertTrue(result); - } - - - @Test - public void testCreateAndDeployCLLPolicyFailedCreateFailed() throws IOException { - mockUpPolicyApiCall(CREATE_POLICY_FAILED); - boolean result = policyService.createAndDeployModifyCLLPolicy(); - Assert.assertFalse(result); - } - - @Test - public void testCreateAndDeployCLLPolicyFailedDeployFailed() throws IOException { - mockUpPolicyApiCall(DEPLOY_POLICY_FAILED); - boolean result = policyService.createAndDeployModifyCLLPolicy(); - Assert.assertFalse(result); - } - - @Test - public void testUndeployAndRemoveCLLPolicySuccess() throws IOException { - mockUpPolicyApiCall(0); - boolean result = policyService.undeployAndRemoveModifyCLLPolicy(); - Assert.assertTrue(result); - } - - @Test - public void testUndeployAndRemoveCLLPolicySuccessPolicyNotExist() throws IOException { - mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST); - boolean result = policyService.undeployAndRemoveModifyCLLPolicy(); - Assert.assertTrue(result); - } - - @Test - public void testUpdateIntentConfigPolicySuccess() throws IOException { - mockUpPolicyApiCall(0); - boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); - Assert.assertTrue(result); - } - - @Test - public void testUpdateIntentConfigPolicySuccessPolicyNotExist(){ - mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST); - boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); - Assert.assertTrue(result); - } - - @Test - public void testUpdateIntentConfigPolicyFailedCreatePolicyTypeFailed(){ - mockUpPolicyApiCall(CREATE_POLICY_TYPE_FAILED); - boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); - Assert.assertFalse(result); - } - - @Test - public void testUpdateIntentConfigPolicyFailedCreatePolicyFailed(){ - mockUpPolicyApiCall(CREATE_POLICY_FAILED); - boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); - Assert.assertFalse(result); - } - - @Test - public void testUpdateIntentConfigPolicyFailedDeployPolicyFailed(){ - mockUpPolicyApiCall(DEPLOY_POLICY_FAILED); - boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", true); - Assert.assertFalse(result); - } - - private void mockUpPolicyApiCall(int failedFlag) { - policyAPICall = mock(PolicyAPICall.class); - policyService.setPolicyAPICall(policyAPICall); - ResponseBody mockedSuccessResponse = ResponseBody.create(MediaType.parse("application/json"), - "Test Success Result"); - if ((CREATE_POLICY_FAILED & failedFlag) > 0) { - when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn( - TestCall.failedCall("Create policy failed")); - } else { - when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn( - TestCall.successfulCall(mockedSuccessResponse)); - } - if ((CREATE_POLICY_TYPE_FAILED & failedFlag) > 0) { - when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.failedCall("Create policy type failed")); - } else { - when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); - } - - if ((DEPLOY_POLICY_FAILED & failedFlag) > 0) { - when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.failedCall("Deploy policy failed")); - } else { - when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); - } - - if ((UNDEPLOY_POLICY_FAILED & failedFlag) > 0) { - when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.failedCall("Undeploy policy failed")); - } else { - when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.successfulCall(mockedSuccessResponse)); - } - - if ((DELETE_POLICY_FAILED & failedFlag) > 0) { - when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn( - TestCall.failedCall("Delete policy failed")); - } else { - when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn( - TestCall.successfulCall(mockedSuccessResponse)); - } - - if ((QUERY_POLICY_NOT_EXIST & failedFlag) > 0) { - when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn( - TestCall.failedCall("Get policy failed")); - } else { - when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn( - TestCall.successfulCall(mockedSuccessResponse)); - } - } -} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/service/IntentServiceTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/service/IntentServiceTest.java deleted file mode 100644 index 6dc1062..0000000 --- a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/service/IntentServiceTest.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright 2022 Huawei Technologies Co., Ltd. - * - * Licensed 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.onap.usecaseui.intentanalysis.test.service; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.usecaseui.intentanalysis.bean.enums.*; -import org.onap.usecaseui.intentanalysis.bean.models.Intent; -import org.onap.usecaseui.intentanalysis.bean.models.Condition; -import org.onap.usecaseui.intentanalysis.bean.models.FulfilmentInfo; -import org.onap.usecaseui.intentanalysis.bean.models.Context; -import org.onap.usecaseui.intentanalysis.bean.models.Expectation; -import org.onap.usecaseui.intentanalysis.bean.models.ExpectationTarget; -import org.onap.usecaseui.intentanalysis.bean.models.ExpectationObject; -import org.onap.usecaseui.intentanalysis.service.IntentService; -import org.onap.usecaseui.intentanalysis.test.IntentAnalysisApplicationTests; -import org.onap.usecaseui.intentanalysis.util.SpringContextUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.test.context.junit4.SpringRunner; - -@SpringBootTest(classes = IntentAnalysisApplicationTests.class) -@RunWith(SpringRunner.class) -public class IntentServiceTest extends AbstractJUnit4SpringContextTests { - - private static final Logger LOGGER = LoggerFactory.getLogger(IntentServiceTest.class); - - private static final String TEST_INTENT_ID_1 = "intentId1"; - - private static final String TEST_INTENT_ID_2 = "intentId2"; - - private static final String TEST_INTENT_NAME = "CLL Business intent"; - - @Autowired - private IntentService intentService; - - @Before - public void setUp() { - SpringContextUtil.setApplicationContext(applicationContext); - } - - @Test - public void testCreateIntentSuccess() throws IOException { - Intent intent = new Intent(); - Expectation expectation1 = new Expectation(); - ExpectationTarget target1 = new ExpectationTarget(); - ExpectationObject object1 = new ExpectationObject(); - Context intentContext = new Context(); - FulfilmentInfo intentFulfilmentInfo = new FulfilmentInfo(); - Condition targetCondition1 = new Condition(); - targetCondition1.setConditionId("conditionId"); - targetCondition1.setConditionName("conditionName"); - targetCondition1.setOperator(OperatorType.valueOf("EQUALTO")); - targetCondition1.setConditionValue("conditionValue"); - List targetConditionList = new ArrayList<>(); - targetConditionList.add(targetCondition1); - target1.setTargetId("targetId"); - target1.setTargetName("targetName"); - target1.setTargetConditions(targetConditionList); - List expectationTargetList = new ArrayList<>(); - expectationTargetList.add(target1); - object1.setObjectType(ObjectType.valueOf("OBJECT1")); - object1.setObjectInstance("objectInstance"); - expectation1.setExpectationId("expectationId"); - expectation1.setExpectationName("expectationName"); - expectation1.setExpectationType(ExpectationType.valueOf("DELIVERY")); - expectation1.setExpectationObject(object1); - expectation1.setExpectationTargets(expectationTargetList); - List expectationList = new ArrayList<>(); - expectationList.add(expectation1); - intentContext.setContextId("intentContextId"); - intentContext.setContextName("intentContextName"); - List intentContextList = new ArrayList<>(); - intentContextList.add(intentContext); - intentFulfilmentInfo.setFulfilmentStatus(FulfilmentStatus.valueOf("NOT_FULFILLED")); - intentFulfilmentInfo.setNotFulfilledReason("NotFulfilledReason"); - intentFulfilmentInfo.setNotFulfilledState(NotFulfilledState.valueOf("COMPLIANT")); - intent.setIntentId("testIntentId"); - intent.setIntentName("testIntentName"); - intent.setIntentContexts(intentContextList); - intent.setIntentExpectations(expectationList); - intent.setIntentFulfilmentInfo(intentFulfilmentInfo); - - Intent intentTmp = intentService.createIntent(intent); - Assert.assertNotNull(intentTmp); - } - - @Test - public void testGetIntentListSuccess() { - List intentList = intentService.getIntentList(); - Assert.assertNotNull(intentList); - } - - @Test - public void testGetIntentSuccess() { - Intent intent = intentService.getIntent(TEST_INTENT_ID_1); - Assert.assertNotNull(intent); - } - - @Test - public void testGetIntentByNameSuccess() { - List intentList = intentService.getIntentByName(TEST_INTENT_NAME); - Assert.assertNotNull(intentList); - - } - - @Test - public void testUpdateIntentSuccess() { - Intent intent = intentService.getIntent(TEST_INTENT_ID_1); - intent.setIntentName("new intent name"); - List contextList = intent.getIntentContexts(); - Context intentContext = contextList.get(0); - intentContext.setContextName("new context name"); - contextList.set(0, intentContext); - intent.setIntentContexts(contextList); - FulfilmentInfo intentFulfilmentInfo = intent.getIntentFulfilmentInfo(); - intentFulfilmentInfo.setNotFulfilledReason("new reason"); - intent.setIntentFulfilmentInfo(intentFulfilmentInfo); - List expectationList = intent.getIntentExpectations(); - Expectation expectation = expectationList.get(0); - expectation.setExpectationName("new expectation name"); - ExpectationObject expectationObject = expectation.getExpectationObject(); - expectationObject.setObjectInstance("new object instance"); - expectation.setExpectationObject(expectationObject); - List expectationTargetList = expectation.getExpectationTargets(); - ExpectationTarget expectationTarget = expectationTargetList.get(0); - expectationTarget.setTargetName("new target name"); - List targetConditionList = expectationTarget.getTargetConditions(); - Condition targetCondition = targetConditionList.get(0); - targetCondition.setConditionName("new conditon name"); - targetConditionList.set(0, targetCondition); - expectationTarget.setTargetConditions(targetConditionList); - expectationTargetList.remove(2); - expectationTargetList.set(0, expectationTarget); - expectation.setExpectationTargets(expectationTargetList); - expectationList.set(0, expectation); - intent.setIntentExpectations(expectationList); - - Intent updatedIntent = intentService.updateIntent(intent); - Assert.assertEquals("new intent name", updatedIntent.getIntentName()); - - } - - @Test - public void testDeleteIntentSuccess() { - intentService.deleteIntent(TEST_INTENT_ID_2); - Intent intent = intentService.getIntent(TEST_INTENT_ID_2); - Assert.assertNull(intent); - } -} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/RestFulServicesTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/RestFulServicesTest.java deleted file mode 100644 index 6af9820..0000000 --- a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/RestFulServicesTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2022 Huawei Technologies Co., Ltd. - * - * Licensed 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.onap.usecaseui.intentanalysis.test.util; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.Principal; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpUpgradeHandler; -import javax.servlet.http.Part; -import okhttp3.RequestBody; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall; -import org.onap.usecaseui.intentanalysis.test.IntentAnalysisApplicationTests; -import org.onap.usecaseui.intentanalysis.util.RestfulServices; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.test.context.junit4.SpringRunner; - -@SpringBootTest(classes = IntentAnalysisApplicationTests.class) -@RunWith(SpringRunner.class) -public class RestFulServicesTest { - - @Test - public void testCreateSuccess() { - PolicyAPICall call = RestfulServices.create("https://localhost/testurl/", PolicyAPICall.class); - Assert.assertNotNull(call); - } - - @Test - public void testCreateWithAuthSuccess() { - PolicyAPICall call = RestfulServices.create(PolicyAPICall.class, "testUser", "testPwd"); - Assert.assertNotNull(call); - } - - @Test - public void testGetMSBAddressSuccess() { - String msbAddress = RestfulServices.getMSBIAGAddress(); - Assert.assertNotNull(msbAddress); - } - - @Test - public void testExtractBodySuccess() throws IOException { - HttpServletRequest request = new MockHttpServletRequest(); - RequestBody requestBody = RestfulServices.extractBody(request); - Assert.assertNotNull(requestBody); - } -} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/TestCall.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/TestCall.java deleted file mode 100644 index d9cc4a8..0000000 --- a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/test/util/TestCall.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2022 Huawei Technologies Co., Ltd. - * - * Licensed 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.onap.usecaseui.intentanalysis.test.util; - -import java.io.IOException; -import okhttp3.MediaType; -import okhttp3.Request; -import okhttp3.ResponseBody; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -public class TestCall implements Call { - - private int code; - - private T result; - - private String errorMsg; - - private TestCall(int code, T result, String errorMsg) { - this.code = code; - this.result = result; - this.errorMsg = errorMsg; - } - - @Override - public Response execute() throws IOException { - if (code >= 200 && code < 300) { - return Response.success(code, result); - } - if (code >= 400 && code < 500) { - return Response.error(code, ResponseBody.create(MediaType.parse("application/json"), errorMsg)); - } - throw new IOException("Exception happens"); - } - - public static TestCall successfulCall(T result) { - return new TestCall<>(200, result, null); - } - - public static TestCall failedCall(String errorMsg) { - return new TestCall<>(400, null, errorMsg); - } - - public static TestCall exceptionCall() { - return new TestCall<>(-1, null, null); - } - - @Override - public void enqueue(Callback callback) { - - } - - @Override - public boolean isExecuted() { - return false; - } - - @Override - public void cancel() { - - } - - @Override - public boolean isCanceled() { - return false; - } - - @Override - public Call clone() { - return null; - } - - @Override - public Request request() { - return null; - } - -} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/RestFulServicesTest.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/RestFulServicesTest.java new file mode 100644 index 0000000..b19e9a1 --- /dev/null +++ b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/RestFulServicesTest.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.usecaseui.intentanalysis.util; + +import java.io.IOException; +import javax.servlet.http.HttpServletRequest; + +import okhttp3.RequestBody; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall; +import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest(classes = IntentAnalysisApplicationTests.class) +@RunWith(SpringRunner.class) +public class RestFulServicesTest { + + @Test + public void testCreateSuccess() { + PolicyAPICall call = RestfulServices.create("https://localhost/testurl/", PolicyAPICall.class); + Assert.assertNotNull(call); + } + + @Test + public void testCreateWithAuthSuccess() { + PolicyAPICall call = RestfulServices.create(PolicyAPICall.class, "testUser", "testPwd"); + Assert.assertNotNull(call); + } + + @Test + public void testGetMSBAddressSuccess() { + String msbAddress = RestfulServices.getMSBIAGAddress(); + Assert.assertNotNull(msbAddress); + } + + @Test + public void testExtractBodySuccess() throws IOException { + HttpServletRequest request = new MockHttpServletRequest(); + RequestBody requestBody = RestfulServices.extractBody(request); + Assert.assertNotNull(requestBody); + } +} diff --git a/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/TestCall.java b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/TestCall.java new file mode 100644 index 0000000..5454097 --- /dev/null +++ b/intentanalysis/src/test/java/org/onap/usecaseui/intentanalysis/util/TestCall.java @@ -0,0 +1,92 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.usecaseui.intentanalysis.util; + +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.Request; +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +public class TestCall implements Call { + + private int code; + + private T result; + + private String errorMsg; + + private TestCall(int code, T result, String errorMsg) { + this.code = code; + this.result = result; + this.errorMsg = errorMsg; + } + + @Override + public Response execute() throws IOException { + if (code >= 200 && code < 300) { + return Response.success(code, result); + } + if (code >= 400 && code < 500) { + return Response.error(code, ResponseBody.create(MediaType.parse("application/json"), errorMsg)); + } + throw new IOException("Exception happens"); + } + + public static TestCall successfulCall(T result) { + return new TestCall<>(200, result, null); + } + + public static TestCall failedCall(String errorMsg) { + return new TestCall<>(400, null, errorMsg); + } + + public static TestCall exceptionCall() { + return new TestCall<>(-1, null, null); + } + + @Override + public void enqueue(Callback callback) { + + } + + @Override + public boolean isExecuted() { + return false; + } + + @Override + public void cancel() { + + } + + @Override + public boolean isCanceled() { + return false; + } + + @Override + public Call clone() { + return null; + } + + @Override + public Request request() { + return null; + } + +} -- cgit 1.2.3-korg