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.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;
49 import static org.junit.Assert.assertEquals;
52 * Test Policy API in org.onap.clamp.ClampDesigner.client package - replicate
53 * Policy Delegates in tests.
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
57 public class PolicyClientIT extends AbstractIT {
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";
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();
77 String policyJson = StringMatchPolicyReq.format(refProp, prop);
78 String correctValue = TransformUtil.getResourceAsString("expected/stringmatch.json");
79 JSONAssert.assertEquals(policyJson, correctValue, true);
80 String responseMessage = "";
82 responseMessage = policyClient.sendMicroService(policyJson, prop, stringMatchPolicyRequestUuid);
83 } catch (Exception e) {
84 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
86 System.out.println(responseMessage);
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();
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);
105 private void createUpdateTcaPolicy(String actionCd) throws Exception {
106 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
107 Tca tca = prop.getTca();
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 = "";
115 responseMessage = policyClient.sendMicroService(policyJson, prop, tcaPolicyRequestUuid);
116 } catch (Exception e) {
117 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
119 System.out.println(responseMessage);
123 private void deleteStringMatchPolicy(String actionCd) throws Exception {
124 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
126 StringMatch stringMatch = prop.getType(StringMatch.class);
127 if (stringMatch.isFound()) {
128 prop.setCurrentModelElementId(stringMatch.getId());
129 String responseMessage = "";
131 responseMessage = policyClient.deleteMicrosService(prop);
132 } catch (Exception e) {
133 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
135 System.out.println(responseMessage);
139 private void deleteOperationalPolicy(String actionCd) throws Exception {
140 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
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);
153 private void deleteTcaPolicy(String actionCd) throws Exception {
154 ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
156 Tca tca = prop.getTca();
158 prop.setCurrentModelElementId(tca.getId());
159 String responseMessage = "";
161 responseMessage = policyClient.deleteMicrosService(prop);
162 } catch (Exception e) {
163 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
166 System.out.println(responseMessage);
171 public void testCreateUpdateDeleteStringMatchPolicy() throws Exception {
173 createUpdateStringMatch(CldsEvent.ACTION_SUBMIT);
175 TimeUnit.SECONDS.sleep(20);
177 deleteStringMatchPolicy(CldsEvent.ACTION_DELETE);
181 public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
183 createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
185 TimeUnit.SECONDS.sleep(20);
187 deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
191 public void testCreateUpdateDeleteTcaPolicy() throws Exception {
193 createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
195 TimeUnit.SECONDS.sleep(20);
197 deleteTcaPolicy(CldsEvent.ACTION_DELETE);