69dad53ec9fd4638cb29c5c6db4a088d4d984399
[clamp.git] / src / test / java / org / onap / clamp / clds / it / OperationPolicyReqItCase.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  * 
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.IOException;
31 import java.net.URLDecoder;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq;
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.util.ResourceFileUtil;
45 import org.onap.policy.api.AttributeType;
46 import org.onap.policy.controlloop.policy.builder.BuilderException;
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 @RunWith(SpringRunner.class)
52 @SpringBootTest
53 public class OperationPolicyReqItCase {
54
55     @Autowired
56     private ClampProperties refProp;
57
58     @Test
59     public void formatAttributesTest() throws IOException, BuilderException {
60         String modelBpmnProp = ResourceFileUtil
61                 .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
62         String modelBpmn = ResourceFileUtil.getResourceAsString("example/model-properties/policy/modelBpmn.json");
63         ModelProperties modelProperties = new ModelProperties("testModel", "controlNameTest", CldsEvent.ACTION_SUBMIT,
64                 true, modelBpmn, modelBpmnProp);
65         List<Map<AttributeType, Map<String, String>>> attributes = new ArrayList<>();
66         if (modelProperties.getType(Policy.class).isFound()) {
67             for (PolicyChain policyChain : modelProperties.getType(Policy.class).getPolicyChains()) {
68                 attributes.add(OperationalPolicyReq.formatAttributes(refProp, modelProperties,
69                         modelProperties.getType(Policy.class).getId(), policyChain));
70             }
71         }
72         assertFalse(attributes.isEmpty());
73         assertTrue(attributes.size() == 2);
74         // now validate the Yaml, to do so we replace the dynamic ID by a known
75         // key so that we can compare it
76         String yaml = URLDecoder.decode(attributes.get(0).get(AttributeType.RULE).get("controlLoopYaml"), "UTF-8");
77         yaml = replaceGeneratedValues(yaml);
78         assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-1.yaml"), yaml);
79         yaml = URLDecoder.decode(attributes.get(1).get(AttributeType.RULE).get("controlLoopYaml"), "UTF-8");
80         yaml = replaceGeneratedValues(yaml);
81         assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-2.yaml"), yaml);
82     }
83
84     private String replaceGeneratedValues(String yaml) {
85         yaml = yaml.replaceAll("Policy - created" + System.lineSeparator() + "    by CLDS", "Policy - created by CLDS");
86         yaml = yaml.replaceAll("trigger_policy: (.*)", "trigger_policy: <generatedId>");
87         yaml = yaml.replaceAll("id: (.*)", "id: <generatedId>");
88         yaml = yaml.replaceAll("success: (.*)", "success: <generatedId>");
89         // Remove this field as not always present (depends of policy api)
90         yaml = yaml.replaceAll("  pnf: null" + System.lineSeparator(), "");
91         yaml = yaml.replaceAll("failure: (.*)", "failure: <generatedId>");
92         yaml = yaml.replaceAll("failure_exception: (.*)", "failure_exception: <generatedId>");
93         yaml = yaml.replaceAll("failure_retries: (.*)", "failure_retries: <generatedId>");
94         yaml = yaml.replaceAll("failure_timeout: (.*)", "failure_timeout: <generatedId>");
95         yaml = yaml.substring(yaml.indexOf("controlLoop:"), yaml.length());
96         return yaml;
97     }
98 }