22abb13d206a75dc1aec53c2dd3dfbe0ed43393c
[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.TcaMPolicyReq;
39 import org.onap.clamp.clds.model.CldsEvent;
40 import org.onap.clamp.clds.model.prop.ModelProperties;
41 import org.onap.clamp.clds.model.prop.Policy;
42 import org.onap.clamp.clds.model.prop.PolicyChain;
43 import org.onap.clamp.clds.model.prop.Tca;
44 import org.onap.clamp.clds.util.ResourceFileUtil;
45 import org.onap.policy.api.AttributeType;
46 import org.skyscreamer.jsonassert.JSONAssert;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.TestPropertySource;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 /**
52  * Test Policy API in org.onap.clamp.ClampDesigner.client package - replicate
53  * Policy Delegates in tests.
54  */
55 @RunWith(SpringRunner.class)
56 @SpringBootTest
57 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
58 public class PolicyClientIT extends AbstractIT {
59     String modelProp;
60     String modelBpmnProp;
61     String modelName;
62     String controlName;
63
64     /**
65      * Initialize Test.
66      */
67     @Before
68     public void setUp() throws IOException {
69         modelProp = ResourceFileUtil.getResourceAsString("example/modelProp.json");
70         modelBpmnProp = ResourceFileUtil.getResourceAsString("example/modelBpmnProp.json");
71         modelName = "example-model06";
72         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
73     }
74
75     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
76         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
77         Policy policy = prop.getType(Policy.class);
78         if (policy.isFound()) {
79             for (PolicyChain policyChain : policy.getPolicyChains()) {
80                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
81
82                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
83                         prop, policy.getId(), policyChain);
84                 String responseMessage = policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
85             }
86         }
87     }
88
89     private void createUpdateTcaPolicy(String actionCd) throws Exception {
90         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
91         Tca tca = prop.getType(Tca.class);
92         if (tca.isFound()) {
93             String tcaPolicyRequestUuid = UUID.randomUUID().toString();
94             String policyJson = TcaMPolicyReq.formatTca(refProp, prop);
95             String correctValue = ResourceFileUtil.getResourceAsString("expected/tca.json");
96             JSONAssert.assertEquals(policyJson, correctValue, true);
97             String responseMessage = "";
98             try {
99                 responseMessage = policyClient.sendMicroServiceInJson(policyJson, prop, tcaPolicyRequestUuid);
100             } catch (Exception e) {
101                 assertTrue(e.getMessage().contains("Exception while communicating with Policy"));
102             }
103         }
104     }
105
106     private void deleteOperationalPolicy(String actionCd) throws Exception {
107         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
108
109         Policy policy = prop.getType(Policy.class);
110         if (policy.isFound()) {
111             prop.setCurrentModelElementId(policy.getId());
112             for (PolicyChain policyChain : policy.getPolicyChains()) {
113                 prop.setPolicyUniqueId(policyChain.getPolicyId());
114                 String responseMessage = policyClient.deleteBrms(prop);
115             }
116         }
117     }
118
119     private void deleteTcaPolicy(String actionCd) throws Exception {
120         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
121
122         Tca tca = prop.getType(Tca.class);
123         if (tca.isFound()) {
124             prop.setCurrentModelElementId(tca.getId());
125             String responseMessage = "";
126             try {
127                 responseMessage = policyClient.deleteMicrosService(prop);
128             } catch (Exception e) {
129                 assertTrue(e.getMessage().contains("Policy delete failed: PE500 "));
130             }
131         }
132     }
133
134     // @Test
135     /**
136      * Temporarily disabled Test.
137      */
138     public void testCreateUpdateDeleteOperationalPolicy() throws Exception {
139
140         createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
141
142         TimeUnit.SECONDS.sleep(20);
143
144         deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
145     }
146
147     @Test
148     public void testCreateUpdateDeleteTcaPolicy() throws Exception {
149
150         createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
151
152         TimeUnit.SECONDS.sleep(20);
153
154         deleteTcaPolicy(CldsEvent.ACTION_DELETE);
155     }
156 }