71cd9f6b8a378e1a3fcf037c86a9aec9247e1dbc
[clamp.git] / src / test / java / org / onap / clamp / clds / it / PolicyClientItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.client.req.policy.OperationalPolicyReq;
37 import org.onap.clamp.clds.client.req.policy.PolicyClient;
38 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
39 import org.onap.clamp.clds.config.ClampProperties;
40 import org.onap.clamp.clds.model.CldsEvent;
41 import org.onap.clamp.clds.model.properties.ModelProperties;
42 import org.onap.clamp.clds.model.properties.Policy;
43 import org.onap.clamp.clds.model.properties.PolicyChain;
44 import org.onap.clamp.clds.model.properties.Tca;
45 import org.onap.clamp.clds.util.ResourceFileUtil;
46 import org.onap.policy.api.AttributeType;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.boot.test.context.SpringBootTest;
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 public class PolicyClientItCase {
58
59     @Autowired
60     private ClampProperties refProp;
61     @Autowired
62     protected PolicyClient policyClient;
63     String modelProp;
64     String modelBpmnProp;
65     String modelName;
66     String controlName;
67
68     /**
69      * Initialize Test.
70      */
71     @Before
72     public void setUp() throws IOException {
73         modelProp = ResourceFileUtil.getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
74         modelBpmnProp = ResourceFileUtil.getResourceAsString("example/model-properties/policy/modelBpmn.json");
75         modelName = "example-model06";
76         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
77     }
78
79     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
80         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
81         Policy policy = prop.getType(Policy.class);
82         if (policy.isFound()) {
83             for (PolicyChain policyChain : policy.getPolicyChains()) {
84                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
85                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
86                         prop, policy.getId(), policyChain);
87                 policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
88             }
89         }
90     }
91
92     private void createUpdateTcaPolicy(String actionCd) throws Exception {
93         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
94         Tca tca = prop.getType(Tca.class);
95         if (tca.isFound()) {
96             String tcaPolicyRequestUuid = UUID.randomUUID().toString();
97             String policyJson = TcaRequestFormatter.createPolicyJson(refProp, prop);
98             try {
99                 policyClient.sendMicroServiceInJson(policyJson, prop, tcaPolicyRequestUuid);
100             } catch (Exception e) {
101                 assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
102             }
103         }
104     }
105
106     private void deleteOperationalPolicy(String actionCd) throws Exception {
107         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
108         Policy policy = prop.getType(Policy.class);
109         if (policy.isFound()) {
110             prop.setCurrentModelElementId(policy.getId());
111             for (PolicyChain policyChain : policy.getPolicyChains()) {
112                 prop.setPolicyUniqueId(policyChain.getPolicyId());
113                 policyClient.deleteBrms(prop);
114             }
115         }
116     }
117
118     private void deleteTcaPolicy(String actionCd) throws Exception {
119         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp);
120         Tca tca = prop.getType(Tca.class);
121         if (tca.isFound()) {
122             prop.setCurrentModelElementId(tca.getId());
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         createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT);
137         TimeUnit.SECONDS.sleep(20);
138         deleteOperationalPolicy(CldsEvent.ACTION_DELETE);
139     }
140
141     @Test
142     public void testCreateUpdateDeleteTcaPolicy() throws Exception {
143         createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT);
144         TimeUnit.SECONDS.sleep(20);
145         deleteTcaPolicy(CldsEvent.ACTION_DELETE);
146     }
147 }