Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / parameters / TestPapParameterGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  *  Modification Copyright 2022. Nordix Foundation.
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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.pap.main.parameters;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertFalse;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38
39 /**
40  * Class to perform unit test of {@link PapParameterGroup}.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43  */
44 class TestPapParameterGroup {
45     private static final Coder coder = new StandardCoder();
46
47     CommonTestData commonTestData = new CommonTestData();
48
49     @Test
50     void testPapParameterGroup_Named() {
51         final PapParameterGroup papParameters = new PapParameterGroup("my-name");
52         assertEquals("my-name", papParameters.getName());
53     }
54
55     @Test
56     void testPapParameterGroup() {
57         final PapParameterGroup papParameters = commonTestData.getPapParameterGroup(1);
58         validateParameters(papParameters);
59     }
60
61     @Test
62     void testPapParameterGroup_Postgres() throws CoderException {
63         String json = commonTestData.getPapPostgresParameterGroupAsString(1);
64         final PapParameterGroup papPostgresParameters = coder.decode(json, PapParameterGroup.class);
65         validateParameters(papPostgresParameters);
66     }
67
68     @Test
69     void testPapParameterGroup_NullName() throws Exception {
70         String json = commonTestData.getPapParameterGroupAsString(1).replace("\"PapGroup\"", "null");
71         final PapParameterGroup papParameters = coder.decode(json, PapParameterGroup.class);
72         final ValidationResult validationResult = papParameters.validate();
73         assertFalse(validationResult.isValid());
74         assertNull(papParameters.getName());
75         assertThat(validationResult.getResult()).contains("is null");
76     }
77
78     @Test
79     void testPapParameterGroup_EmptyName() throws Exception {
80         String json = commonTestData.getPapParameterGroupAsString(1).replace(CommonTestData.PAP_GROUP_NAME, "");
81         final PapParameterGroup papParameters = coder.decode(json, PapParameterGroup.class);
82         final ValidationResult validationResult = papParameters.validate();
83         assertFalse(validationResult.isValid());
84         assertEquals("", papParameters.getName());
85         assertThat(validationResult.getResult()).contains("\"name\" value \"\" INVALID, " + "is blank");
86     }
87
88     @Test
89     void testPapParameterGroup_SetName() {
90         final PapParameterGroup papParameters = commonTestData.getPapParameterGroup(1);
91         papParameters.setName("PapNewGroup");
92         final ValidationResult validationResult = papParameters.validate();
93         assertTrue(validationResult.isValid());
94         assertEquals("PapNewGroup", papParameters.getName());
95     }
96
97     /**
98      * Validates the pap parameters.
99      *
100      * @param parameterGroup parameter group being tested
101      */
102     public void validateParameters(PapParameterGroup parameterGroup) {
103         final TopicParameterGroup topicParameterGroup = parameterGroup.getTopicParameterGroup();
104         final ValidationResult validationResult = parameterGroup.validate();
105         assertTrue(validationResult.isValid());
106         assertEquals(CommonTestData.PAP_GROUP_NAME, parameterGroup.getName());
107         assertEquals(topicParameterGroup.getTopicSinks(), parameterGroup.getTopicParameterGroup().getTopicSinks());
108         assertEquals(topicParameterGroup.getTopicSources(), parameterGroup.getTopicParameterGroup().getTopicSources());
109     }
110 }