2f9cf20f43c853fab35ddc61fffeb97036e46bae
[usecase-ui/intent-analysis.git] /
1 /*
2  * Copyright 2022 Huawei Technologies Co., Ltd.
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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
12  * the License.
13  */
14
15 package org.onap.usecaseui.intentanalysis.adapters.policy;
16
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;
21
22
23 import java.io.IOException;
24 import mockit.MockUp;
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;
39
40 @SpringBootTest(classes = IntentAnalysisApplicationTests.class)
41 @RunWith(SpringRunner.class)
42 public class PolicyServiceTest {
43
44     private static final int CREATE_POLICY_TYPE_FAILED = 0x01;
45
46     private static final int CREATE_POLICY_FAILED = 0x02;
47
48     private static final int DEPLOY_POLICY_FAILED = 0x04;
49
50     private static final int UNDEPLOY_POLICY_FAILED = 0x08;
51
52     private static final int DELETE_POLICY_FAILED = 0x10;
53
54     private static final int QUERY_POLICY_NOT_EXIST = 0x20;
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyServiceTest.class);
57
58     @Autowired
59     private PolicyServiceImpl policyService;
60
61     private PolicyAPICall policyAPICall;
62
63     private MockUp mockup;
64
65     @Test
66     public void testCreateAndDeployCLLPolicySuccess() throws IOException {
67         mockUpPolicyApiCall(0);
68         boolean result = policyService.createAndDeployModifyCLLPolicy();
69         Assert.assertTrue(result);
70     }
71
72
73     @Test
74     public void testCreateAndDeployCLLPolicyFailedCreateFailed() throws IOException {
75         mockUpPolicyApiCall(CREATE_POLICY_FAILED);
76         boolean result = policyService.createAndDeployModifyCLLPolicy();
77         Assert.assertFalse(result);
78     }
79
80     @Test
81     public void testCreateAndDeployCLLPolicyFailedDeployFailed() throws IOException {
82         mockUpPolicyApiCall(DEPLOY_POLICY_FAILED);
83         boolean result = policyService.createAndDeployModifyCLLPolicy();
84         Assert.assertFalse(result);
85     }
86
87     @Test
88     public void testUndeployAndRemoveCLLPolicySuccess() throws IOException {
89         mockUpPolicyApiCall(0);
90         boolean result = policyService.undeployAndRemoveModifyCLLPolicy();
91         Assert.assertTrue(result);
92     }
93
94     @Test
95     public void testUndeployAndRemoveCLLPolicySuccessPolicyNotExist() throws IOException {
96         mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST);
97         boolean result = policyService.undeployAndRemoveModifyCLLPolicy();
98         Assert.assertTrue(result);
99     }
100
101     @Test
102     public void testUpdateIntentConfigPolicySuccess() throws IOException {
103         mockUpPolicyApiCall(0);
104         boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
105         Assert.assertTrue(result);
106     }
107
108     @Test
109     public void testUpdateIntentConfigPolicySuccessPolicyNotExist(){
110         mockUpPolicyApiCall(QUERY_POLICY_NOT_EXIST);
111         boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
112         Assert.assertTrue(result);
113     }
114
115     @Test
116     public void testUpdateIntentConfigPolicyFailedCreatePolicyTypeFailed(){
117         mockUpPolicyApiCall(CREATE_POLICY_TYPE_FAILED);
118         boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
119         Assert.assertFalse(result);
120     }
121
122     @Test
123     public void testUpdateIntentConfigPolicyFailedCreatePolicyFailed(){
124         mockUpPolicyApiCall(CREATE_POLICY_FAILED);
125         boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
126         Assert.assertFalse(result);
127     }
128
129     @Test
130     public void testUpdateIntentConfigPolicyFailedDeployPolicyFailed(){
131         mockUpPolicyApiCall(DEPLOY_POLICY_FAILED);
132         boolean result = policyService.updateIntentConfigPolicy("testCLLID", "1000", "true");
133         Assert.assertFalse(result);
134     }
135
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"));
144         } else {
145             when(policyAPICall.createPolicy(anyString(), anyString(), any())).thenReturn(
146                 TestCall.successfulCall(mockedSuccessResponse));
147         }
148         if ((CREATE_POLICY_TYPE_FAILED & failedFlag) > 0) {
149             when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.failedCall("Create policy type failed"));
150         } else {
151             when(policyAPICall.createPolicyType(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
152         }
153
154         if ((DEPLOY_POLICY_FAILED & failedFlag) > 0) {
155             when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.failedCall("Deploy policy failed"));
156         } else {
157             when(policyAPICall.deployPolicy(any())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
158         }
159
160         if ((UNDEPLOY_POLICY_FAILED & failedFlag) > 0) {
161             when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.failedCall("Undeploy policy failed"));
162         } else {
163             when(policyAPICall.undeployPolicy(anyString())).thenReturn(TestCall.successfulCall(mockedSuccessResponse));
164         }
165
166         if ((DELETE_POLICY_FAILED & failedFlag) > 0) {
167             when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn(
168                 TestCall.failedCall("Delete policy failed"));
169         } else {
170             when(policyAPICall.removePolicy(anyString(), anyString())).thenReturn(
171                 TestCall.successfulCall(mockedSuccessResponse));
172         }
173
174         if ((QUERY_POLICY_NOT_EXIST & failedFlag) > 0) {
175             when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn(
176                 TestCall.failedCall("Get policy failed"));
177         } else {
178             when(policyAPICall.getPolicy(anyString(), anyString(), anyString(), anyString())).thenReturn(
179                 TestCall.successfulCall(mockedSuccessResponse));
180         }
181     }
182 }