Merge "Adding 'name' to yamls and json in model"
[policy/models.git] / models-sim / policy-models-sim-pdp / src / test / java / org / onap / policy / models / sim / pdp / parameters / TestPdpSimulatorParameterGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.sim.pdp.parameters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Map;
28
29 import org.junit.Test;
30 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
31 import org.onap.policy.common.parameters.GroupValidationResult;
32
33 /**
34  * Class to perform unit test of {@link PdpSimulatorParameterGroup}.
35  *
36  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
37  */
38 public class TestPdpSimulatorParameterGroup {
39     CommonTestData commonTestData = new CommonTestData();
40
41     @Test
42     public void testPdpSimulatorParameterGroup_Named() {
43         final PdpSimulatorParameterGroup pdpSimulatorParameters = new PdpSimulatorParameterGroup("my-name");
44         assertEquals("my-name", pdpSimulatorParameters.getName());
45     }
46
47     @Test
48     public void testPdpSimulatorParameterGroup() {
49         final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData.toObject(
50                 commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME),
51                 PdpSimulatorParameterGroup.class);
52         final PdpStatusParameters pdpStatusParameters = pdpSimulatorParameters.getPdpStatusParameters();
53         final TopicParameterGroup topicParameterGroup  = pdpSimulatorParameters.getTopicParameterGroup();
54         final GroupValidationResult validationResult = pdpSimulatorParameters.validate();
55         assertTrue(validationResult.isValid());
56         assertEquals(CommonTestData.PDP_SIMULATOR_GROUP_NAME, pdpSimulatorParameters.getName());
57         assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeIntervalMs());
58         assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType());
59         assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription());
60         assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes());
61         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks());
62         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources());
63     }
64
65     @Test
66     public void testPdpSimulatorParameterGroup_NullName() {
67         final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData
68                 .toObject(commonTestData.getPdpSimulatorParameterGroupMap(null), PdpSimulatorParameterGroup.class);
69         final GroupValidationResult validationResult = pdpSimulatorParameters.validate();
70         assertFalse(validationResult.isValid());
71         assertEquals(null, pdpSimulatorParameters.getName());
72         assertTrue(validationResult.getResult().contains("is null"));
73     }
74
75     @Test
76     public void testPdpSimulatorParameterGroup_EmptyName() {
77         final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData
78                 .toObject(commonTestData.getPdpSimulatorParameterGroupMap(""), PdpSimulatorParameterGroup.class);
79         final GroupValidationResult validationResult = pdpSimulatorParameters.validate();
80         assertFalse(validationResult.isValid());
81         assertEquals("", pdpSimulatorParameters.getName());
82         assertTrue(validationResult.getResult().contains(
83                 "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string"));
84     }
85
86     @Test
87     public void testPdpSimulatorParameterGroup_SetName() {
88         final PdpSimulatorParameterGroup pdpSimulatorParameters = commonTestData.toObject(
89                 commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME),
90                 PdpSimulatorParameterGroup.class);
91         pdpSimulatorParameters.setName("PdpSimulatorNewGroup");
92         final GroupValidationResult validationResult = pdpSimulatorParameters.validate();
93         assertTrue(validationResult.isValid());
94         assertEquals("PdpSimulatorNewGroup", pdpSimulatorParameters.getName());
95     }
96
97     @Test
98     public void testPdpSimulatorParameterGroup_EmptyPdpStatusParameters() {
99         final Map<String, Object> map =
100                 commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME);
101         map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true));
102         final PdpSimulatorParameterGroup pdpSimulatorParameters =
103                 commonTestData.toObject(map, PdpSimulatorParameterGroup.class);
104         final GroupValidationResult validationResult = pdpSimulatorParameters.validate();
105         assertFalse(validationResult.isValid());
106         assertTrue(validationResult.getResult()
107                 .contains("\"org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup\" INVALID, "
108                         + "parameter group has status INVALID"));
109     }
110
111     @Test
112     public void testApexStarterParameterGroupp_EmptyTopicParameters() {
113         final Map<String, Object> map =
114                 commonTestData.getPdpSimulatorParameterGroupMap(CommonTestData.PDP_SIMULATOR_GROUP_NAME);
115         map.put("topicParameterGroup", commonTestData.getTopicParametersMap(true));
116
117         final PdpSimulatorParameterGroup parGroup =
118                 commonTestData.toObject(map, PdpSimulatorParameterGroup.class);
119         final GroupValidationResult validationResult = parGroup.validate();
120         assertFalse(validationResult.isValid());
121         assertTrue(validationResult.getResult()
122                 .contains("\"org.onap.policy.common.endpoints.parameters.TopicParameterGroup\" INVALID, "
123                         + "parameter group has status INVALID"));
124     }
125
126 }