Rework some sonar critical and major bugs
[clamp.git] / src / test / java / org / onap / clamp / clds / it / PolicyClientIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.util.Map;
30 import java.util.UUID;
31 import java.util.concurrent.TimeUnit;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.clamp.clds.AbstractIT;
37 import org.onap.clamp.clds.client.req.OperationalPolicyReq;
38 import org.onap.clamp.clds.client.req.StringMatchPolicyReq;
39 import org.onap.clamp.clds.client.req.TcaMPolicyReq;
40 import org.onap.clamp.clds.model.CldsEvent;
41 import org.onap.clamp.clds.model.prop.ModelProperties;
42 import org.onap.clamp.clds.model.prop.Policy;
43 import org.onap.clamp.clds.model.prop.PolicyChain;
44 import org.onap.clamp.clds.model.prop.StringMatch;
45 import org.onap.clamp.clds.model.prop.Tca;
46 import org.onap.clamp.clds.util.ResourceFileUtil;
47 import org.onap.policy.api.AttributeType;
48 import org.skyscreamer.jsonassert.JSONAssert;
49 import org.springframework.boot.test.context.SpringBootTest;
50 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
51 import org.springframework.test.context.junit4.SpringRunner;
52
53 /**
54  * Test Policy API in org.onap.clamp.ClampDesigner.client package - replicate
55  * Policy Delegates in tests.
56  */
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 public class PolicyClientIT extends AbstractIT {
60     String modelProp;
61     String modelBpmnProp;
62     String modelName;
63     String controlName;
64
65     /**
66      * Initialize Test.
67      */
68     @Before
69     public void setUp() throws IOException {
70         modelProp = ResourceFileUtil.getResourceAsString("example/modelProp.json");
71         modelBpmnProp = ResourceFileUtil.getResourceAsString("example/modelBpmnProp.json");
72         modelName = "example-model06";
73         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
74     }
75
76     private void createUpdateStringMatch(String actionCd) throws Exception {
77         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
78         StringMatch stringMatch = prop.getType(StringMatch.class);
79         if (stringMatch.isFound()) {
80             String stringMatchPolicyRequestUuid = UUID.randomUUID().toString();
81
82             String policyJson = StringMatchPolicyReq.format(refProp, prop);
83             String correctValue = ResourceFileUtil.getResourceAsString("expected/stringmatch.json");
84             JSONAssert.assertEquals(policyJson, correctValue, true);
85             String responseMessage = "";
86             try {
87                 responseMessage = policyClient.sendMicroServiceInJson(policyJson, prop, stringMatchPolicyRequestUuid);
88             } catch (Exception e) {
89                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
90             }
91             System.out.println(responseMessage);
92         }
93     }
94
95     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
96         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
97         Policy policy = prop.getType(Policy.class);
98         if (policy.isFound()) {
99             for (PolicyChain policyChain : policy.getPolicyChains()) {
100                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
101
102                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
103                         prop, policy.getId(), policyChain);
104                 String responseMessage = policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
105                 System.out.println(responseMessage);
106             }
107         }
108     }
109
110     private void createUpdateTcaPolicy(String actionCd) throws Exception {
111         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
112         Tca tca = prop.getType(Tca.class);
113         if (tca.isFound()) {
114             String tcaPolicyRequestUuid = UUID.randomUUID().toString();
115             String policyJson = TcaMPolicyReq.formatTca(refProp, prop);
116             String correctValue = ResourceFileUtil.getResourceAsString("expected/tca.json");
117             JSONAssert.assertEquals(policyJson, correctValue, true);
118             String responseMessage = "";
119             try {
120                 responseMessage = policyClient.sendMicroServiceInJson(policyJson, prop, tcaPolicyRequestUuid);
121             } catch (Exception e) {
122                 assertTrue(e.getMessage().contains("Exception while communicating with Policy"));
123             }
124             System.out.println(responseMessage);
125         }
126     }
127
128     private void deleteStringMatchPolicy(String actionCd) throws Exception {
129         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
130
131         StringMatch stringMatch = prop.getType(StringMatch.class);
132         if (stringMatch.isFound()) {
133             prop.setCurrentModelElementId(stringMatch.getId());
134             String responseMessage = "";
135             try {
136                 responseMessage = policyClient.deleteMicrosService(prop);
137             } catch (Exception e) {
138                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
139             }
140             System.out.println(responseMessage);
141         }
142     }
143
144     private void deleteOperationalPolicy(String actionCd) throws Exception {
145         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
146
147         Policy policy = prop.getType(Policy.class);
148         if (policy.isFound()) {
149             prop.setCurrentModelElementId(policy.getId());
150             for (PolicyChain policyChain : policy.getPolicyChains()) {
151                 prop.setPolicyUniqueId(policyChain.getPolicyId());
152                 String responseMessage = policyClient.deleteBrms(prop);
153                 System.out.println(responseMessage);
154             }
155         }
156     }
157
158     private void deleteTcaPolicy(String actionCd) throws Exception {
159         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
160
161         Tca tca = prop.getType(Tca.class);
162         if (tca.isFound()) {
163             prop.setCurrentModelElementId(tca.getId());
164             String responseMessage = "";
165             try {
166                 responseMessage = policyClient.deleteMicrosService(prop);
167             } catch (Exception e) {
168                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
169             }
170
171             System.out.println(responseMessage);
172         }
173     }
174
175     // @Test
176     /**
177      * Temporarily disabled Test.
178      */
179     public void testCreateUpdateDeleteStringMatchPolicy() throws Exception {
180
181         createUpdateStringMatch(CldsEvent.ACTION_SUBMIT);
182
183         TimeUnit.SECONDS.sleep(20);
184
185         deleteStringMatchPolicy(CldsEvent.ACTION_DELETE);
186     }
187
188     // @Test
189     /**
190      * Temporarily disabled Test.
191      */
192     public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
193
194         createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
195
196         TimeUnit.SECONDS.sleep(20);
197
198         deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
199     }
200
201     @Test
202     public void testCreateUpdateDeleteTcaPolicy() throws Exception {
203
204         createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
205
206         TimeUnit.SECONDS.sleep(20);
207
208         deleteTcaPolicy(CldsEvent.ACTION_DELETE);
209     }
210 }