317858012e3264b98278ab51adb344b71d9d37de
[clamp.git] / src / test / java / org / onap / clamp / clds / client / req / policy / GuardPolicyAttributesConstructorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 Nokia 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.client.req.policy;
25
26 import java.io.IOException;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.assertj.core.api.Assertions;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.clamp.clds.model.properties.ModelProperties;
34 import org.onap.clamp.clds.model.properties.Policy;
35 import org.onap.clamp.clds.model.properties.PolicyChain;
36 import org.onap.clamp.clds.model.properties.PolicyItem;
37 import org.onap.clamp.clds.util.ResourceFileUtil;
38 import org.onap.policy.api.AttributeType;
39 import org.onap.policy.controlloop.policy.builder.BuilderException;
40
41 public class GuardPolicyAttributesConstructorTest {
42
43     private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
44     private ModelProperties modelProperties;
45     private List<PolicyChain> policyChains;
46
47     /**
48      * @throws Exception thrown if resources not found.
49      */
50     @Before
51     public void setUp() throws Exception {
52         String modelProp = ResourceFileUtil
53             .getResourceAsString("example/model-properties/tca_new/model-properties.json");
54         String modelBpmnJson = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-bpmn.json");
55         modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540", CONTROL_NAME, "PUT", false,
56             modelBpmnJson, modelProp);
57
58         policyChains = modelProperties.getType(Policy.class).getPolicyChains();
59     }
60
61     @Test
62     public void testRequestAttributes() throws IOException, BuilderException {
63         List<PolicyItem> policyItemsList = GuardPolicyAttributesConstructor
64             .getAllPolicyGuardsFromPolicyChain(policyChains.get(0));
65
66         Assertions.assertThat(policyItemsList.size()).isEqualTo(1);
67
68         // Test first entry
69         Map<AttributeType, Map<String, String>> requestAttributes = GuardPolicyAttributesConstructor
70             .formatAttributes(modelProperties, policyItemsList.get(0));
71
72         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING);
73         Map<String, String> ruleParameters = requestAttributes.get(AttributeType.MATCHING);
74         Assertions.assertThat(ruleParameters).contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
75             Assertions.entry(GuardPolicyAttributesConstructor.RECIPE, "restart"),
76             Assertions.entry(GuardPolicyAttributesConstructor.TARGETS, ".*"),
77             Assertions.entry(GuardPolicyAttributesConstructor.CLNAME,
78                 modelProperties.getControlNameAndPolicyUniqueId()),
79             Assertions.entry(GuardPolicyAttributesConstructor.LIMIT, "1"),
80             Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"),
81             Assertions.entry(GuardPolicyAttributesConstructor.TIME_UNITS, "minute"),
82             Assertions.entry(GuardPolicyAttributesConstructor.GUARD_ACTIVE_START, "00:00:01-05:00"),
83             Assertions.entry(GuardPolicyAttributesConstructor.GUARD_ACTIVE_END, "00:00:00-05:00"));
84     }
85 }