Merge "Update License.txt"
[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.transform.TransformUtil;
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     /**
67     * Initialize Test.
68     */
69     @Before
70     public void setUp() throws IOException {
71         modelProp = TransformUtil.getResourceAsString("example/modelProp.json");
72         modelBpmnProp = TransformUtil.getResourceAsString("example/modelBpmnProp.json");
73         modelName = "example-model06";
74         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
75     }
76
77     private void createUpdateStringMatch(String actionCd) throws Exception {
78         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
79         StringMatch stringMatch = prop.getType(StringMatch.class);
80         if (stringMatch.isFound()) {
81             String stringMatchPolicyRequestUuid = UUID.randomUUID().toString();
82
83             String policyJson = StringMatchPolicyReq.format(refProp, prop);
84             String correctValue = TransformUtil.getResourceAsString("expected/stringmatch.json");
85             JSONAssert.assertEquals(policyJson, correctValue, true);
86             String responseMessage = "";
87             try {
88                 responseMessage = policyClient.sendMicroService(policyJson, prop, stringMatchPolicyRequestUuid);
89             } catch (Exception e) {
90                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
91             }
92             System.out.println(responseMessage);
93         }
94     }
95
96     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
97         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
98         Policy policy = prop.getType(Policy.class);
99         if (policy.isFound()) {
100             for (PolicyChain policyChain : policy.getPolicyChains()) {
101                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
102
103                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
104                         prop, policy.getId(), policyChain);
105                 String responseMessage = policyClient.sendBrms(attributes, prop, operationalPolicyRequestUuid);
106                 System.out.println(responseMessage);
107             }
108         }
109     }
110
111     private void createUpdateTcaPolicy(String actionCd) throws Exception {
112         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
113         Tca tca = prop.getTca();
114         if (tca.isFound()) {
115             String tcaPolicyRequestUuid = UUID.randomUUID().toString();
116             String policyJson = TcaMPolicyReq.formatTca(refProp, prop);
117             String correctValue = TransformUtil.getResourceAsString("expected/tca.json");
118             JSONAssert.assertEquals(policyJson, correctValue, true);
119             String responseMessage = "";
120             try {
121                 responseMessage = policyClient.sendMicroService(policyJson, prop, tcaPolicyRequestUuid);
122             } catch (Exception e) {
123                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
124             }
125             System.out.println(responseMessage);
126         }
127     }
128
129     private void deleteStringMatchPolicy(String actionCd) throws Exception {
130         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
131
132         StringMatch stringMatch = prop.getType(StringMatch.class);
133         if (stringMatch.isFound()) {
134             prop.setCurrentModelElementId(stringMatch.getId());
135             String responseMessage = "";
136             try {
137                 responseMessage = policyClient.deleteMicrosService(prop);
138             } catch (Exception e) {
139                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
140             }
141             System.out.println(responseMessage);
142         }
143     }
144
145     private void deleteOperationalPolicy(String actionCd) throws Exception {
146         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
147
148         Policy policy = prop.getType(Policy.class);
149         if (policy.isFound()) {
150             prop.setCurrentModelElementId(policy.getId());
151             for (PolicyChain policyChain : policy.getPolicyChains()) {
152                 prop.setPolicyUniqueId(policyChain.getPolicyId());
153                 String responseMessage = policyClient.deleteBrms(prop);
154                 System.out.println(responseMessage);
155             }
156         }
157     }
158
159     private void deleteTcaPolicy(String actionCd) throws Exception {
160         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
161
162         Tca tca = prop.getTca();
163         if (tca.isFound()) {
164             prop.setCurrentModelElementId(tca.getId());
165             String responseMessage = "";
166             try {
167                 responseMessage = policyClient.deleteMicrosService(prop);
168             } catch (Exception e) {
169                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
170             }
171
172             System.out.println(responseMessage);
173         }
174     }
175
176     // @Test
177     /**
178      * Temporarily disabled Test.
179      */
180     public void testCreateUpdateDeleteStringMatchPolicy() throws Exception {
181
182         createUpdateStringMatch(CldsEvent.ACTION_SUBMIT);
183
184         TimeUnit.SECONDS.sleep(20);
185
186         deleteStringMatchPolicy(CldsEvent.ACTION_DELETE);
187     }
188
189     // @Test
190     /**
191      * Temporarily disabled Test.
192      */
193     public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
194
195         createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
196
197         TimeUnit.SECONDS.sleep(20);
198
199         deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
200     }
201
202     @Test
203     public void testCreateUpdateDeleteTcaPolicy() throws Exception {
204
205         createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
206
207         TimeUnit.SECONDS.sleep(20);
208
209         deleteTcaPolicy(CldsEvent.ACTION_DELETE);
210     }
211 }