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