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