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