bd063d06a89cd6840d29e3c00bbf15819b4dca88
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2024 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30 import java.util.Map;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
33 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
34 import org.onap.policy.common.parameters.ValidationResult;
35
36 /**
37  * Class to perform unit test of {@link ApexStarterParameterGroup}.
38  *
39  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40  */
41 class TestApexStarterParameterGroup {
42     CommonTestData commonTestData = new CommonTestData();
43
44     @Test
45     void testApexStarterParameterGroup_Named() {
46         final ApexStarterParameterGroup apexStarterParameters = new ApexStarterParameterGroup("my-name");
47         assertEquals("my-name", apexStarterParameters.getName());
48     }
49
50     @Test
51     void testApexStarterParameterGroup() {
52         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
53             commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
54             ApexStarterParameterGroup.class);
55         final RestServerParameters restServerParameters = apexStarterParameters.getRestServerParameters();
56         final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters();
57         final TopicParameterGroup topicParameterGroup = apexStarterParameters.getTopicParameterGroup();
58         final ValidationResult validationResult = apexStarterParameters.validate();
59         assertTrue(validationResult.isValid());
60         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.getName());
61         assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeIntervalMs());
62         assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType());
63         assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription());
64         assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes());
65         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSinks());
66         assertEquals(CommonTestData.TOPIC_PARAMS, topicParameterGroup.getTopicSources());
67         assertEquals(restServerParameters.getHost(), apexStarterParameters.getRestServerParameters().getHost());
68         assertEquals(restServerParameters.getPort(), apexStarterParameters.getRestServerParameters().getPort());
69         assertEquals(restServerParameters.getUserName(), apexStarterParameters.getRestServerParameters().getUserName());
70         assertEquals(restServerParameters.getPassword(), apexStarterParameters.getRestServerParameters().getPassword());
71         assertTrue(apexStarterParameters.getRestServerParameters().isHttps());
72         assertFalse(apexStarterParameters.getRestServerParameters().isAaf());
73     }
74
75     @Test
76     void testApexStarterParameterGroup_NullName() {
77         final ApexStarterParameterGroup apexStarterParameters = commonTestData
78             .toObject(commonTestData.getApexStarterParameterGroupMap(null), ApexStarterParameterGroup.class);
79         final ValidationResult validationResult = apexStarterParameters.validate();
80         assertFalse(validationResult.isValid());
81         assertNull(apexStarterParameters.getName());
82         assertTrue(validationResult.getResult().contains("is null"));
83     }
84
85     @Test
86     void testApexStarterParameterGroup_EmptyName() {
87         final ApexStarterParameterGroup apexStarterParameters = commonTestData
88             .toObject(commonTestData.getApexStarterParameterGroupMap(""), ApexStarterParameterGroup.class);
89         final ValidationResult validationResult = apexStarterParameters.validate();
90         assertThat(validationResult.getResult()).contains("\"name\" value \"\" INVALID, is blank");
91         assertFalse(validationResult.isValid());
92         assertEquals("", apexStarterParameters.getName());
93     }
94
95     @Test
96     void testApexStarterParameterGroup_SetName() {
97         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
98             commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
99             ApexStarterParameterGroup.class);
100         apexStarterParameters.setName("ApexStarterNewGroup");
101         final ValidationResult validationResult = apexStarterParameters.validate();
102         assertTrue(validationResult.isValid());
103         assertEquals("ApexStarterNewGroup", apexStarterParameters.getName());
104     }
105
106     @Test
107     void testApexStarterParameterGroup_EmptyPdpStatusParameters() {
108         final Map<String, Object> map =
109             commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
110         map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true));
111         final ApexStarterParameterGroup apexStarterParameters =
112             commonTestData.toObject(map, ApexStarterParameterGroup.class);
113         final ValidationResult validationResult = apexStarterParameters.validate();
114         assertThat(validationResult.getResult())
115             .contains("\"ApexStarterParameterGroup\" INVALID, item has status INVALID");
116         assertFalse(validationResult.isValid());
117     }
118
119     @Test
120     void testApexStarterParameterGroupp_EmptyRestServerParameters() {
121         final Map<String, Object> map =
122             commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
123         map.put("restServerParameters", commonTestData.getRestServerParametersMap(true));
124
125         final ApexStarterParameterGroup apexStarterParameters =
126             commonTestData.toObject(map, ApexStarterParameterGroup.class);
127         final ValidationResult validationResult = apexStarterParameters.validate();
128         assertThat(validationResult.getResult()).contains("\"RestServerParameters\" INVALID, item has status INVALID");
129         assertFalse(validationResult.isValid());
130     }
131
132
133     @Test
134     void testApexStarterParameterGroup_EmptyTopicParameters() {
135         final Map<String, Object> map =
136             commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
137         map.put("topicParameterGroup", commonTestData.getTopicParametersMap(true));
138
139         final ApexStarterParameterGroup apexStarterParameters =
140             commonTestData.toObject(map, ApexStarterParameterGroup.class);
141         final ValidationResult validationResult = apexStarterParameters.validate();
142         assertThat(validationResult.getResult()).contains("\"TopicParameterGroup\" INVALID, item has status INVALID");
143         assertFalse(validationResult.isValid());
144     }
145 }