2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.AbstractItCase;
39 import org.onap.clamp.clds.client.req.OperationalPolicyReq;
40 import org.onap.clamp.clds.model.CldsEvent;
41 import org.onap.clamp.clds.model.prop.ModelProperties;
42 import org.onap.clamp.clds.model.prop.Policy;
43 import org.onap.clamp.clds.model.prop.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.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.TestPropertySource;
49 import org.springframework.test.context.junit4.SpringRunner;
51 @RunWith(SpringRunner.class)
53 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
54 public class OperationPolicyReqItCase extends AbstractItCase {
57 public void formatAttributesTest() throws IOException, BuilderException {
58 String modelProp = ResourceFileUtil.getResourceAsString("example/modelProp.json");
59 String modelBpmnProp = ResourceFileUtil.getResourceAsString("example/modelBpmnProp.json");
60 ModelProperties prop = new ModelProperties("testModel", "controlNameTest", CldsEvent.ACTION_SUBMIT, true,
61 modelBpmnProp, modelProp);
62 List<Map<AttributeType, Map<String, String>>> attributes = new ArrayList<>();
63 if (prop.getType(Policy.class).isFound()) {
64 for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) {
66 attributes.add(OperationalPolicyReq.formatAttributes(refProp, prop, prop.getType(Policy.class).getId(),
70 assertFalse(attributes.isEmpty());
71 assertTrue(attributes.size() == 2);
72 // now validate the Yaml, to do so we replace the dynamic ID by a known
73 // key so that we can compare it
74 String yaml = URLDecoder.decode(attributes.get(0).get(AttributeType.RULE).get("ControlLoopYaml"), "UTF-8");
75 yaml = yaml.replaceAll("trigger_policy: (.*)", "trigger_policy: <generatedId>");
76 yaml = yaml.replaceAll("id: (.*)", "id: <generatedId>");
77 yaml = yaml.replaceAll("success: (.*)", "success: <generatedId>");
78 // Remove this field as not always present (depends of policy api)
79 yaml = yaml.replaceAll(" pnf: null", "");
80 yaml = yaml.substring(yaml.indexOf("controlLoop:"), yaml.length());
82 assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-1.yaml"), yaml);
84 yaml = URLDecoder.decode(attributes.get(1).get(AttributeType.RULE).get("ControlLoopYaml"), "UTF-8");
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", "");
90 yaml = yaml.substring(yaml.indexOf("controlLoop:"), yaml.length());
92 assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-2.yaml"), yaml);