Add unit tests
[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     @Before
48     public void setUp() throws Exception {
49         String modelProp = ResourceFileUtil
50             .getResourceAsString("example/model-properties/tca_new/model-properties.json");
51         String modelBpmnJson = ResourceFileUtil.getResourceAsString("example/model-properties/tca_new/model-bpmn.json");
52         modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540", CONTROL_NAME, "PUT", false,
53             modelBpmnJson, modelProp);
54
55         policyChains = modelProperties.getType(Policy.class).getPolicyChains();
56     }
57
58     @Test
59     public void testRequestAttributes() throws IOException, BuilderException {
60         List<PolicyItem> policyItemsList = GuardPolicyAttributesConstructor
61             .getAllPolicyGuardsFromPolicyChain(policyChains.get(0));
62
63         Assertions.assertThat(policyItemsList.size()).isEqualTo(1);
64
65         // Test first entry
66         Map<AttributeType, Map<String, String>> requestAttributes = GuardPolicyAttributesConstructor
67             .formatAttributes(modelProperties, policyItemsList.get(0));
68
69         Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING);
70         Map<String, String> ruleParameters = requestAttributes.get(AttributeType.MATCHING);
71         Assertions.assertThat(ruleParameters).contains(Assertions.entry(GuardPolicyAttributesConstructor.ACTOR, "APPC"),
72             Assertions.entry(GuardPolicyAttributesConstructor.RECIPE, "restart"),
73             Assertions.entry(GuardPolicyAttributesConstructor.TARGETS, ".*"),
74             Assertions.entry(GuardPolicyAttributesConstructor.CLNAME,
75                 modelProperties.getControlNameAndPolicyUniqueId()),
76             Assertions.entry(GuardPolicyAttributesConstructor.LIMIT, "1"),
77             Assertions.entry(GuardPolicyAttributesConstructor.TIME_WINDOW, "10"),
78             Assertions.entry(GuardPolicyAttributesConstructor.TIME_UNITS, "minute"),
79             Assertions.entry(GuardPolicyAttributesConstructor.GUARD_ACTIVE_START, "00:00:01-05:00"),
80             Assertions.entry(GuardPolicyAttributesConstructor.GUARD_ACTIVE_END, "00:00:00-05:00"));
81     }
82 }