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