Fix bug at runtime
[clamp.git] / src / test / java / org / onap / clamp / clds / it / OperationPolicyReqIT.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.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.AbstractIT;
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;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest
53 @TestPropertySource(locations = "classpath:application-no-camunda.properties")
54 public class OperationPolicyReqIT extends AbstractIT {
55
56     @Test
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()) {
65
66                 attributes.add(OperationalPolicyReq.formatAttributes(refProp, prop, prop.getType(Policy.class).getId(),
67                         policyChain));
68             }
69         }
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
79         assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-1.yaml"), yaml);
80
81         yaml = URLDecoder.decode(attributes.get(1).get(AttributeType.RULE).get("ControlLoopYaml"), "UTF-8");
82         yaml = yaml.replaceAll("trigger_policy: (.*)", "trigger_policy: <generatedId>");
83         yaml = yaml.replaceAll("id: (.*)", "id: <generatedId>");
84         yaml = yaml.replaceAll("success: (.*)", "success: <generatedId>");
85
86         assertEquals(ResourceFileUtil.getResourceAsString("example/operational-policy/yaml-policy-chain-2.yaml"), yaml);
87     }
88 }