2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.it;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
31 import java.net.URLDecoder;
32 import java.util.ArrayList;
33 import java.util.List;
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;
51 @RunWith(SpringRunner.class)
53 public class OperationPolicyReqItCase {
56 private ClampProperties refProp;
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));
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);
84 private String replaceGeneratedValues(String yaml) {
85 yaml = yaml.replaceAll("trigger_policy: (.*)", "trigger_policy: <generatedId>");
86 yaml = yaml.replaceAll("id: (.*)", "id: <generatedId>");
87 yaml = yaml.replaceAll("success: (.*)", "success: <generatedId>");
88 // Remove this field as not always present (depends of policy api)
89 yaml = yaml.replaceAll(" pnf: null" + System.lineSeparator(), "");
90 yaml = yaml.replaceAll("failure: (.*)", "failure: <generatedId>");
91 yaml = yaml.replaceAll("failure_exception: (.*)", "failure_exception: <generatedId>");
92 yaml = yaml.replaceAll("failure_retries: (.*)", "failure_retries: <generatedId>");
93 yaml = yaml.replaceAll("failure_timeout: (.*)", "failure_timeout: <generatedId>");
94 yaml = yaml.substring(yaml.indexOf("controlLoop:"), yaml.length());