2 * Copyright 2022 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
15 package org.onap.usecaseui.intentanalysis.adapters.policy;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.ArgumentMatchers.anyString;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
23 import java.io.IOException;
25 import okhttp3.MediaType;
26 import okhttp3.ResponseBody;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.usecaseui.intentanalysis.adapters.policy.apicall.PolicyAPICall;
31 import org.onap.usecaseui.intentanalysis.adapters.policy.impl.PolicyServiceImpl;
32 import org.onap.usecaseui.intentanalysis.IntentAnalysisApplicationTests;
33 import org.onap.usecaseui.intentanalysis.util.TestCall;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.test.context.junit4.SpringRunner;
40 @SpringBootTest(classes = IntentAnalysisApplicationTests.class)
41 @RunWith(SpringRunner.class)
42 public class PolicyServiceTest {
44 private static final int CREATE_POLICY_TYPE_FAILED = 0x01;
46 private static final int CREATE_POLICY_FAILED = 0x02;
48 private static final int DEPLOY_POLICY_FAILED = 0x04;
50 private static final int UNDEPLOY_POLICY_FAILED = 0x08;
52 private static final int DELETE_POLICY_FAILED = 0x10;
54 private static final int QUERY_POLICY_NOT_EXIST = 0x20;
56 private static final Logger LOGGER = LoggerFactory.getLogger(PolicyServiceTest.class);
59 private PolicyServiceImpl policyService;
61 private PolicyAPICall policyAPICall;
63 private MockUp mockup;
66 public void testCreateAndDeployCLLPolicySuccess() throws IOException {
67 mockUpPolicyApiCall(0);
68 boolean result = policyService.createAndDeployModifyCLLPolicy();
69 Assert.assertTrue(result);
74 public void testCreateAndDeployCLLPolicyFailedCreateFailed() throws IOException {
75 mockUpPolicyApiCall(CREATE_POLICY_FAILED);
76 boolean result = policyService.createAndDeployModifyCLLPolicy();
77 Assert.assertFalse(result);
81 public void testCreateAndDeployCLLPolicyFailedDeployFailed() throws IOException {
82 mockUpPolicyApiCall(DEPLOY_POLICY_FAILED);
83 boolean result = policyService.createAndDeployModifyCLLPolicy();
84 Assert.assertFalse(result);
88 public void testUndeployAndRemoveCLLPolicySuccess() throws IOException {
89 mockUpPolicyApiCall(0);
90 boolean result = policyService.undeployAndRemoveModifyCLLPolicy();
91 Assert.assertTrue(result);
95 public void testUndeployAndRemoveCLLPolicySuccessPolicyNotExist() throws IOException {
96 mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST);
97 boolean result = policyService.undeployAndRemoveModifyCLLPolicy();
98 Assert.assertTrue(result);
102 public void testUpdateIntentConfigPolicySuccess() throws IOException {
103 mockUpPolicyApiCall(0);
104 boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
105 Assert.assertTrue(result);
109 public void testUpdateIntentConfigPolicySuccessPolicyNotExist(){
110 mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST);
111 boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
112 Assert.assertTrue(result);
116 public void testUpdateIntentConfigPolicyFailedCreatePolicyTypeFailed(){
117 mockUpPolicyApiCall(CREATE_POLICY_TYPE_FAILED);
118 boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
119 Assert.assertFalse(result);
123 public void testUpdateIntentConfigPolicyFailedCreatePolicyFailed(){
124 mockUpPolicyApiCall(CREATE_POLICY_FAILED);
125 boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
126 Assert.assertFalse(result);
130 public void testUpdateIntentConfigPolicyFailedDeployPolicyFailed(){
131 mockUpPolicyApiCall(DEPLOY_POLICY_FAILED);
132 boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
133 Assert.assertFalse(result);
136 private void mockUpPolicyApiCall(int failedFlag) {
137 policyAPICall = mock(PolicyAPICall.class);
138 policyService.setPolicyAPICall(policyAPICall);
139 ResponseBody mockedSuccessResponse = ResponseBody.create(MediaType.parse("application/json"),
140 "Test Success Result");
141 if ((CREATE_POLICY_FAILED & failedFlag) > 0) {
142 when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn(
143 TestCall.failedCall("Create policy failed"));
145 when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn(
146 TestCall.successfulCall(mockedSuccessResponse));
148 if ((CREATE_POLICY_TYPE_FAILED & failedFlag) > 0) {
149 when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.failedCall("Create policy type failed"));
151 when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
154 if ((DEPLOY_POLICY_FAILED & failedFlag) > 0) {
155 when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.failedCall("Deploy policy failed"));
157 when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
160 if ((UNDEPLOY_POLICY_FAILED & failedFlag) > 0) {
161 when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.failedCall("Undeploy policy failed"));
163 when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
166 if ((DELETE_POLICY_FAILED & failedFlag) > 0) {
167 when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn(
168 TestCall.failedCall("Delete policy failed"));
170 when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn(
171 TestCall.successfulCall(mockedSuccessResponse));
174 if ((QUERY_POLICY_NOT_EXIST & failedFlag) > 0) {
175 when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn(
176 TestCall.failedCall("Get policy failed"));
178 when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn(
179 TestCall.successfulCall(mockedSuccessResponse));