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