[CLAMP-1] Initial ONAP CLAMP seed code commit
[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.model.CldsEvent;
38 import org.onap.clamp.clds.model.prop.ModelProperties;
39 import org.openecomp.policy.api.AttributeType;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
42 import org.springframework.test.context.junit4.SpringRunner;
43 import org.onap.clamp.clds.client.req.OperationalPolicyReq;
44 import org.onap.clamp.clds.client.req.StringMatchPolicyReq;
45 import org.onap.clamp.clds.model.prop.Policy;
46 import org.onap.clamp.clds.model.prop.StringMatch;
47 import org.onap.clamp.clds.transform.TransformUtil;
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(webEnvironment = WebEnvironment.RANDOM_PORT)
55 public class PolicyClientIT extends AbstractIT {
56     String modelProp;
57     String modelBpmnProp;
58     String modelName;
59     String controlName;
60
61     @Before
62     public void setUp() throws IOException {
63         modelProp = TransformUtil.getResourceAsString("example/modelProp.json");
64         modelBpmnProp = TransformUtil.getResourceAsString("example/modelBpmnProp.json");
65         modelName = "example-model06";
66         controlName = "ClosedLoop_FRWL_SIG_fad4dcae_e498_11e6_852e_0050568c4ccf";
67     }
68
69     private void createUpdateStringMatch(String actionCd) throws Exception {
70         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
71         String stringMatchPolicyRequestUuid = UUID.randomUUID().toString();
72         String policyJson = StringMatchPolicyReq.format(refProp, prop);
73         System.out.println("String Match policyJson=" + policyJson);
74         String responseMessage = policyClient.sendMicroService(policyJson, prop, stringMatchPolicyRequestUuid);
75         System.out.println(responseMessage);
76     }
77
78     private void createUpdateOperationalPolicy(String actionCd) throws Exception {
79         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
80         String operationalPolicyRequestUuid = UUID.randomUUID().toString();
81         Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp, prop);
82         String responseMessage = policyClient.sendBrms(attributes, prop, operationalPolicyRequestUuid);
83         System.out.println(responseMessage);
84     }
85
86     private void createUpdatePolicies(String actionCd) throws Exception {
87         createUpdateStringMatch(actionCd);
88         createUpdateOperationalPolicy(actionCd);
89     }
90
91     private void deleteStringMatchPolicy(String actionCd) throws Exception {
92         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
93         StringMatch stringMatch = prop.getStringMatch();
94         prop.setCurrentModelElementId(stringMatch.getId());
95         String responseMessage = policyClient.deleteMicrosService(prop);
96         System.out.println(responseMessage);
97     }
98
99     private void deleteOperationalPolicy(String actionCd) throws Exception {
100         ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
101         Policy policy = prop.getPolicy();
102         prop.setCurrentModelElementId(policy.getId());
103         String responseMessage = policyClient.deleteBrms(prop);
104         System.out.println(responseMessage);
105     }
106
107     private void deletePolicies(String actionCd) throws Exception {
108         deleteStringMatchPolicy(actionCd);
109         deleteOperationalPolicy(actionCd);
110     }
111
112     /**
113      * Delete policies so we can start with a clean state. But this is just a
114      * precaution - the policies might not already exists. So ignore errors in
115      * attempting to do this.
116      * 
117      * @param actionCd
118      */
119     private void cleanUpPolicies(String actionCd) {
120         try {
121             deleteStringMatchPolicy(actionCd);
122         } catch (Exception e) {
123             System.err.println(
124                     "TestPolicyClient: The following error is ok - attempting delete in case the policy exists - the goal is to start with clean slate");
125         }
126         try {
127             deleteOperationalPolicy(actionCd);
128         } catch (Exception e) {
129             System.err.println(
130                     "TestPolicyClient: The following error is ok - attempting delete in case the policy exists - the goal is to start with clean slate");
131         }
132     }
133
134     @Test
135     public void testCreateUpdateDeletePolicy() throws Exception {
136
137         cleanUpPolicies(CldsEvent.ACTION_DELETE);
138         TimeUnit.SECONDS.sleep(5);
139         System.out.println("entered into update");
140         String actionCd;
141
142         try {
143             actionCd = CldsEvent.ACTION_SUBMIT;
144             createUpdatePolicies(actionCd);
145         } catch (Exception e) {
146             assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
147         }
148
149         try {
150             actionCd = CldsEvent.ACTION_RESUBMIT;
151             createUpdatePolicies(actionCd);
152         } catch (Exception e) {
153             assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
154         }
155
156         try {
157             actionCd = CldsEvent.ACTION_RESUBMIT;
158             createUpdatePolicies(actionCd);
159         } catch (Exception e) {
160             assertTrue(e.getMessage().contains("Policy send failed: PE500 "));
161         }
162
163         try {
164             TimeUnit.SECONDS.sleep(20);
165             deletePolicies(CldsEvent.ACTION_DELETE);
166         } catch (Exception e) {
167             assertTrue(e.getMessage().contains("Unable to get valid Response from  PDP"));
168         }
169
170     }
171 }