c44964921c8ac32514e57eaa7b4fbb1cb7a80cf8
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.services.onappf.parameters;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Map;
30 import org.junit.Test;
31 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
32 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
33 import org.onap.policy.common.parameters.ValidationResult;
34
35 /**
36  * Class to perform unit test of {@link ApexStarterParameterGroup}.
37  *
38  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39  */
40 public class TestApexStarterParameterGroup {
41     CommonTestData commonTestData = new CommonTestData();
42
43     @Test
44     public void testApexStarterParameterGroup_Named() {
45         final ApexStarterParameterGroup apexStarterParameters = new ApexStarterParameterGroup("my-name");
46         assertEquals("my-name", apexStarterParameters.getName());
47     }
48
49     @Test
50     public void testApexStarterParameterGroup() {
51         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
52                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
53                 ApexStarterParameterGroup.class);
54         final RestServerParameters restServerParameters = apexStarterParameters.getRestServerParameters();
55         final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters();
56         final TopicParameterGroup topicParameterGroup  = apexStarterParameters.getTopicParameterGroup();
57         final ValidationResult validationResult = apexStarterParameters.validate();
58         assertTrue(validationResult.isValid());
59         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.getName());
60         assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeIntervalMs());
61         assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType());
62         assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription());
63         assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes());
64         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks());
65         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources());
66         assertEquals(restServerParameters.getHost(), apexStarterParameters.getRestServerParameters().getHost());
67         assertEquals(restServerParameters.getPort(), apexStarterParameters.getRestServerParameters().getPort());
68         assertEquals(restServerParameters.getUserName(), apexStarterParameters.getRestServerParameters().getUserName());
69         assertEquals(restServerParameters.getPassword(), apexStarterParameters.getRestServerParameters().getPassword());
70         assertTrue(apexStarterParameters.getRestServerParameters().isHttps());
71         assertFalse(apexStarterParameters.getRestServerParameters().isAaf());
72     }
73
74     @Test
75     public void testApexStarterParameterGroup_NullName() {
76         final ApexStarterParameterGroup apexStarterParameters = commonTestData
77                 .toObject(commonTestData.getApexStarterParameterGroupMap(null), ApexStarterParameterGroup.class);
78         final ValidationResult validationResult = apexStarterParameters.validate();
79         assertFalse(validationResult.isValid());
80         assertEquals(null, apexStarterParameters.getName());
81         assertTrue(validationResult.getResult().contains("is null"));
82     }
83
84     @Test
85     public void testApexStarterParameterGroup_EmptyName() {
86         final ApexStarterParameterGroup apexStarterParameters = commonTestData
87                 .toObject(commonTestData.getApexStarterParameterGroupMap(""), ApexStarterParameterGroup.class);
88         final ValidationResult validationResult = apexStarterParameters.validate();
89         assertThat(validationResult.getResult()).contains("\"name\" value \"\" INVALID, is blank");
90         assertFalse(validationResult.isValid());
91         assertEquals("", apexStarterParameters.getName());
92     }
93
94     @Test
95     public void testApexStarterParameterGroup_SetName() {
96         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
97                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
98                 ApexStarterParameterGroup.class);
99         apexStarterParameters.setName("ApexStarterNewGroup");
100         final ValidationResult validationResult = apexStarterParameters.validate();
101         assertTrue(validationResult.isValid());
102         assertEquals("ApexStarterNewGroup", apexStarterParameters.getName());
103     }
104
105     @Test
106     public void testApexStarterParameterGroup_EmptyPdpStatusParameters() {
107         final Map<String, Object> map =
108                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
109         map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true));
110         final ApexStarterParameterGroup apexStarterParameters =
111                 commonTestData.toObject(map, ApexStarterParameterGroup.class);
112         final ValidationResult validationResult = apexStarterParameters.validate();
113         assertThat(validationResult.getResult())
114                 .contains("\"ApexStarterParameterGroup\" INVALID, item has status INVALID");
115         assertFalse(validationResult.isValid());
116     }
117
118     @Test
119     public void testApexStarterParameterGroupp_EmptyRestServerParameters() {
120         final Map<String, Object> map =
121                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
122         map.put("restServerParameters", commonTestData.getRestServerParametersMap(true));
123
124         final ApexStarterParameterGroup apexStarterParameters =
125                 commonTestData.toObject(map, ApexStarterParameterGroup.class);
126         final ValidationResult validationResult = apexStarterParameters.validate();
127         assertThat(validationResult.getResult()).contains("\"RestServerParameters\" INVALID, item has status INVALID");
128         assertFalse(validationResult.isValid());
129     }
130
131
132     @Test
133     public void testApexStarterParameterGroupp_EmptyTopicParameters() {
134         final Map<String, Object> map =
135                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
136         map.put("topicParameterGroup", commonTestData.getTopicParametersMap(true));
137
138         final ApexStarterParameterGroup apexStarterParameters =
139                 commonTestData.toObject(map, ApexStarterParameterGroup.class);
140         final ValidationResult validationResult = apexStarterParameters.validate();
141         assertThat(validationResult.getResult()).contains("\"TopicParameterGroup\" INVALID, item has status INVALID");
142         assertFalse(validationResult.isValid());
143     }
144 }