4195f907c2454cfe5d06fae071a95be4d8c9d7e1
[clamp.git] / src / test / java / org / onap / clamp / clds / model / prop / ModelPropertiesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
22  */
23
24 package org.onap.clamp.clds.model.prop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.IOException;
32 import java.util.Arrays;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.clamp.clds.model.CldsModel;
37 import org.onap.clamp.clds.model.properties.Holmes;
38 import org.onap.clamp.clds.model.properties.ModelProperties;
39 import org.onap.clamp.clds.model.properties.Policy;
40 import org.onap.clamp.clds.model.properties.PolicyItem;
41 import org.onap.clamp.clds.model.properties.Tca;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43
44 /**
45  * Test org.onap.clamp.ClampDesigner.model.prop package using ModelProperties.
46  */
47 public class ModelPropertiesTest {
48
49     @Before
50     public void registerNewClasses() {
51         ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
52     }
53
54     @Test
55     public void testTcaModelDecoding() throws IOException {
56         String modelBpmnProp = ResourceFileUtil
57             .getResourceAsString("example/model-properties/tca/modelBpmnProperties.json");
58         String modelBpmn = ResourceFileUtil.getResourceAsString("example/model-properties/tca/modelBpmn.json");
59         ModelProperties prop = new ModelProperties("example-model-name", "example-control-name", null, true, modelBpmn,
60             modelBpmnProp);
61         Policy policy = prop.getType(Policy.class);
62         assertTrue(policy.isFound());
63         assertEquals(1, policy.getPolicyChains().size());
64         assertEquals("0", policy.getPolicyChains().get(0).getPolicyId());
65         assertEquals(1, policy.getPolicyChains().get(0).getPolicyItems().size());
66         PolicyItem firstPolicyItem = policy.getPolicyChains().get(0).getPolicyItems().get(0);
67         assertEquals("resourceid", firstPolicyItem.getTargetResourceId());
68         assertEquals(180, firstPolicyItem.getRetryTimeLimit());
69         assertEquals(3, firstPolicyItem.getMaxRetries());
70         assertEquals("", firstPolicyItem.getParentPolicy());
71         assertThat(firstPolicyItem.getParentPolicyConditions()).isEmpty();
72         Tca tca = prop.getType(Tca.class);
73         assertNotNull(tca);
74         assertTrue(tca.isFound());
75         assertEquals("vFirewallBroadcastPackets", tca.getTcaItem().getEventName());
76         assertEquals("VNF", tca.getTcaItem().getControlLoopSchemaType());
77         assertEquals("policy1", tca.getTcaItem().getPolicyId());
78         assertEquals("f734f031-10aa-t8fb-330f-04dde2886325", tca.getTcaItem().getTcaUuId());
79         assertEquals(2, tca.getTcaItem().getTcaThresholds().size());
80         assertEquals("ABATED", tca.getTcaItem().getTcaThresholds().get(0).getClosedLoopEventStatus());
81         assertEquals("$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
82             tca.getTcaItem().getTcaThresholds().get(0).getFieldPath());
83         assertEquals("LESS_OR_EQUAL", tca.getTcaItem().getTcaThresholds().get(0).getOperator());
84         assertEquals(Integer.valueOf(123), tca.getTcaItem().getTcaThresholds().get(0).getThreshold());
85         assertEquals("ONSET", tca.getTcaItem().getTcaThresholds().get(1).getClosedLoopEventStatus());
86         assertEquals("$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
87             tca.getTcaItem().getTcaThresholds().get(1).getFieldPath());
88         assertEquals("GREATER_OR_EQUAL", tca.getTcaItem().getTcaThresholds().get(1).getOperator());
89         assertEquals(Integer.valueOf(123), tca.getTcaItem().getTcaThresholds().get(1).getThreshold());
90         // Test global prop
91         assertEquals("vnfRecipe", prop.getGlobal().getActionSet());
92         assertEquals("4cc5b45a-1f63-4194-8100-cd8e14248c92", prop.getGlobal().getService());
93         assertTrue(Arrays.equals(new String[] { "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad" },
94             prop.getGlobal().getResourceVf().toArray()));
95         assertTrue(Arrays.equals(new String[] { "SNDGCA64", "ALPRGAED", "LSLEILAA", "MDTWNJC1" },
96             prop.getGlobal().getLocation().toArray()));
97         assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").getAsString());
98         assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").getAsString());
99     }
100
101     @Test
102     public void testHolmesModelDecoding() throws IOException {
103         String modelBpmnProp = ResourceFileUtil
104             .getResourceAsString("example/model-properties/holmes/modelBpmnProperties.json");
105         String modelBpmn = ResourceFileUtil.getResourceAsString("example/model-properties/holmes/modelBpmn.json");
106         ModelProperties prop = new ModelProperties("example-model-name", "example-control-name", null, true, modelBpmn,
107             modelBpmnProp);
108         Policy policy = prop.getType(Policy.class);
109         assertTrue(policy.isFound());
110         assertEquals(1, policy.getPolicyChains().size());
111         assertEquals("0", policy.getPolicyChains().get(0).getPolicyId());
112         assertEquals(1, policy.getPolicyChains().get(0).getPolicyItems().size());
113         PolicyItem firstPolicyItem = policy.getPolicyChains().get(0).getPolicyItems().get(0);
114         assertEquals("resourceid", firstPolicyItem.getTargetResourceId());
115         assertEquals(180, firstPolicyItem.getRetryTimeLimit());
116         assertEquals(3, firstPolicyItem.getMaxRetries());
117         assertEquals("", firstPolicyItem.getParentPolicy());
118         assertThat(firstPolicyItem.getParentPolicyConditions()).isEmpty();
119         Holmes holmes = prop.getType(Holmes.class);
120         assertNotNull(holmes);
121         assertTrue(holmes.isFound());
122         assertEquals("configPolicy1", holmes.getConfigPolicyName());
123         assertEquals("blabla", holmes.getCorrelationLogic());
124         // Test global prop
125         assertEquals("vnfRecipe", prop.getGlobal().getActionSet());
126         assertEquals("4cc5b45a-1f63-4194-8100-cd8e14248c92", prop.getGlobal().getService());
127         assertTrue(Arrays.equals(new String[] { "f5213e3a-9191-4362-93b5-b67f8d770e44" },
128             prop.getGlobal().getResourceVf().toArray()));
129         assertTrue(Arrays.equals(new String[] { "SNDGCA64", "ALPRGAED", "LSLEILAA", "MDTWNJC1" },
130             prop.getGlobal().getLocation().toArray()));
131         assertEquals("value1", prop.getGlobal().getDeployParameters().get("input1").getAsString());
132         assertEquals("value2", prop.getGlobal().getDeployParameters().get("input2").getAsString());
133     }
134
135     @Test
136     public void testGetVf() throws IOException {
137         CldsModel cldsModel = new CldsModel();
138         cldsModel
139         .setPropText(ResourceFileUtil.getResourceAsString("example/model-properties/tca/modelBpmnProperties.json"));
140         assertEquals("023a3f0d-1161-45ff-b4cf-8918a8ccf3ad", ModelProperties.getVf(cldsModel));
141     }
142 }