2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertTrue;
28 import java.io.IOException;
30 import java.util.UUID;
31 import java.util.concurrent.TimeUnit;
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.test.context.TestPropertySource;
51 import org.springframework.test.context.junit4.SpringRunner;
54 * Test Policy API in org.onap.clamp.ClampDesigner.client package - replicate
55 * Policy Delegates in tests.
57 @RunWith(SpringRunner.class)
59 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
60 public class PolicyClientIT extends AbstractIT {
70 public void setUp() throws IOException {
71 modelProp = ResourceFileUtil.getResourceAsString("example/modelProp.json");
72 modelBpmnProp = ResourceFileUtil.getResourceAsString("example/modelBpmnProp.json");
73 modelName = "example-model06";
74 controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
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();
83 String policyJson = StringMatchPolicyReq.format(refProp, prop);
84 String correctValue = ResourceFileUtil.getResourceAsString("expected/stringmatch.json");
85 JSONAssert.assertEquals(policyJson, correctValue, true);
86 String responseMessage = "";
88 responseMessage = policyClient.sendMicroServiceInJson(policyJson, prop, stringMatchPolicyRequestUuid);
89 } catch (Exception e) {
90 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
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();
102 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
103 prop, policy.getId(), policyChain);
104 String responseMessage = policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
109 private void createUpdateTcaPolicy(String actionCd) throws Exception {
110 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
111 Tca tca = prop.getType(Tca.class);
113 String tcaPolicyRequestUuid = UUID.randomUUID().toString();
114 String policyJson = TcaMPolicyReq.formatTca(refProp, prop);
115 String correctValue = ResourceFileUtil.getResourceAsString("expected/tca.json");
116 JSONAssert.assertEquals(policyJson, correctValue, true);
117 String responseMessage = "";
119 responseMessage = policyClient.sendMicroServiceInJson(policyJson, prop, tcaPolicyRequestUuid);
120 } catch (Exception e) {
121 assertTrue(e.getMessage().contains("Exception while communicating with Policy"));
126 private void deleteStringMatchPolicy(String actionCd) throws Exception {
127 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
129 StringMatch stringMatch = prop.getType(StringMatch.class);
130 if (stringMatch.isFound()) {
131 prop.setCurrentModelElementId(stringMatch.getId());
132 String responseMessage = "";
134 responseMessage = policyClient.deleteMicrosService(prop);
135 } catch (Exception e) {
136 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
141 private void deleteOperationalPolicy(String actionCd) throws Exception {
142 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
144 Policy policy = prop.getType(Policy.class);
145 if (policy.isFound()) {
146 prop.setCurrentModelElementId(policy.getId());
147 for (PolicyChain policyChain : policy.getPolicyChains()) {
148 prop.setPolicyUniqueId(policyChain.getPolicyId());
149 String responseMessage = policyClient.deleteBrms(prop);
154 private void deleteTcaPolicy(String actionCd) throws Exception {
155 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
157 Tca tca = prop.getType(Tca.class);
159 prop.setCurrentModelElementId(tca.getId());
160 String responseMessage = "";
162 responseMessage = policyClient.deleteMicrosService(prop);
163 } catch (Exception e) {
164 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
171 * Temporarily disabled Test.
173 public void testCreateUpdateDeleteStringMatchPolicy() throws Exception {
175 createUpdateStringMatch(CldsEvent.ACTION_SUBMIT);
177 TimeUnit.SECONDS.sleep(20);
179 deleteStringMatchPolicy(CldsEvent.ACTION_DELETE);
184 * Temporarily disabled Test.
186 public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
188 createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
190 TimeUnit.SECONDS.sleep(20);
192 deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
196 public void testCreateUpdateDeleteTcaPolicy() throws Exception {
198 createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
200 TimeUnit.SECONDS.sleep(20);
202 deleteTcaPolicy(CldsEvent.ACTION_DELETE);