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