Rework of the unit tests (mainly IT)
[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.TcaMPolicyReq;
38 import org.onap.clamp.clds.model.CldsEvent;
39 import org.onap.clamp.clds.model.prop.*;
40 import org.onap.policy.api.AttributeType;
41 import org.skyscreamer.jsonassert.JSONAssert;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.onap.clamp.clds.client.req.OperationalPolicyReq;
46 import org.onap.clamp.clds.client.req.StringMatchPolicyReq;
47 import org.onap.clamp.clds.transform.TransformUtil;
48
49 import static org.junit.Assert.assertEquals;
50
51 /**
52  * Test Policy API in org.onap.clamp.ClampDesigner.client package - replicate
53  * Policy Delegates in tests.
54  */
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 public class PolicyClientIT extends AbstractIT {
58     String modelProp;
59     String modelBpmnProp;
60     String modelName;
61     String controlName;
62
63     @Before
64     public void setUp() throws IOException {
65         modelProp = TransformUtil.getResourceAsString("example/modelProp.json");
66         modelBpmnProp = TransformUtil.getResourceAsString("example/modelBpmnProp.json");
67         modelName = "example-model06";
68         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
69     }
70
71     private void createUpdateStringMatch(String actionCd) throws Exception {
72         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
73         StringMatch stringMatch = prop.getType(StringMatch.class);
74         if (stringMatch.isFound()) {
75             String stringMatchPolicyRequestUuid = UUID.randomUUID().toString();
76
77             String policyJson = StringMatchPolicyReq.format(refProp, prop);
78             String correctValue = TransformUtil.getResourceAsString("expected/stringmatch.json");
79             JSONAssert.assertEquals(policyJson, correctValue, true);
80             String responseMessage = "";
81             try {
82                 responseMessage = policyClient.sendMicroService(policyJson, prop, stringMatchPolicyRequestUuid);
83             } catch (Exception e) {
84                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
85             }
86             System.out.println(responseMessage);
87         }
88     }
89
90     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
91         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
92         Policy policy = prop.getType(Policy.class);
93         if (policy.isFound()) {
94             for (PolicyChain policyChain : policy.getPolicyChains()) {
95                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
96
97                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
98                         prop, policy.getId(), policyChain);
99                 String responseMessage = policyClient.sendBrms(attributes, prop, operationalPolicyRequestUuid);
100                 System.out.println(responseMessage);
101             }
102         }
103     }
104
105     private void createUpdateTcaPolicy(String actionCd) throws Exception {
106         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
107         Tca tca = prop.getTca();
108         if (tca.isFound()) {
109             String tcaPolicyRequestUuid = UUID.randomUUID().toString();
110             String policyJson = TcaMPolicyReq.formatTca(refProp, prop);
111             String correctValue = TransformUtil.getResourceAsString("expected/tca.json");
112             JSONAssert.assertEquals(policyJson, correctValue, true);
113             String responseMessage = "";
114             try {
115                 responseMessage = policyClient.sendMicroService(policyJson, prop, tcaPolicyRequestUuid);
116             } catch (Exception e) {
117                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
118             }
119             System.out.println(responseMessage);
120         }
121     }
122
123     private void deleteStringMatchPolicy(String actionCd) throws Exception {
124         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
125
126         StringMatch stringMatch = prop.getType(StringMatch.class);
127         if (stringMatch.isFound()) {
128             prop.setCurrentModelElementId(stringMatch.getId());
129             String responseMessage = "";
130             try {
131                 responseMessage = policyClient.deleteMicrosService(prop);
132             } catch (Exception e) {
133                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
134             }
135             System.out.println(responseMessage);
136         }
137     }
138
139     private void deleteOperationalPolicy(String actionCd) throws Exception {
140         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
141
142         Policy policy = prop.getType(Policy.class);
143         if (policy.isFound()) {
144             prop.setCurrentModelElementId(policy.getId());
145             for (PolicyChain policyChain : policy.getPolicyChains()) {
146                 prop.setPolicyUniqueId(policyChain.getPolicyId());
147                 String responseMessage = policyClient.deleteBrms(prop);
148                 System.out.println(responseMessage);
149             }
150         }
151     }
152
153     private void deleteTcaPolicy(String actionCd) throws Exception {
154         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
155
156         Tca tca = prop.getTca();
157         if (tca.isFound()) {
158             prop.setCurrentModelElementId(tca.getId());
159             String responseMessage = "";
160             try {
161                 responseMessage = policyClient.deleteMicrosService(prop);
162             } catch (Exception e) {
163                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
164             }
165
166             System.out.println(responseMessage);
167         }
168     }
169
170     // @Test
171     public void testCreateUpdateDeleteStringMatchPolicy() throws Exception {
172
173         createUpdateStringMatch(CldsEvent.ACTION_SUBMIT);
174
175         TimeUnit.SECONDS.sleep(20);
176
177         deleteStringMatchPolicy(CldsEvent.ACTION_DELETE);
178     }
179
180     // @Test
181     public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
182
183         createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
184
185         TimeUnit.SECONDS.sleep(20);
186
187         deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
188     }
189
190     @Test
191     public void testCreateUpdateDeleteTcaPolicy() throws Exception {
192
193         createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
194
195         TimeUnit.SECONDS.sleep(20);
196
197         deleteTcaPolicy(CldsEvent.ACTION_DELETE);
198     }
199 }