c22f5e1f290ec8371cc1320ef4e74b9639195342
[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 AT&T Intellectual Property.
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.pap.main.parameters;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Test;
29 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
30 import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
31 import org.onap.policy.common.parameters.GroupValidationResult;
32 import org.onap.policy.common.utils.coder.Coder;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34
35 /**
36  * Class to perform unit test of {@link PapParameterGroup}.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class TestPapParameterGroup {
41     private static final Coder coder = new StandardCoder();
42
43     CommonTestData commonTestData = new CommonTestData();
44
45     @Test
46     public void testPapParameterGroup_Named() {
47         final PapParameterGroup papParameters = new PapParameterGroup("my-name");
48         assertEquals("my-name", papParameters.getName());
49     }
50
51     @Test
52     public void testPapParameterGroup() {
53         final PapParameterGroup papParameters = commonTestData.getPapParameterGroup(1);
54         final RestServerParameters restServerParameters = papParameters.getRestServerParameters();
55         final TopicParameterGroup topicParameterGroup = papParameters.getTopicParameterGroup();
56         final GroupValidationResult validationResult = papParameters.validate();
57         assertTrue(validationResult.isValid());
58         assertEquals(CommonTestData.PAP_GROUP_NAME, papParameters.getName());
59         assertEquals(restServerParameters.getHost(), papParameters.getRestServerParameters().getHost());
60         assertEquals(restServerParameters.getPort(), papParameters.getRestServerParameters().getPort());
61         assertEquals(restServerParameters.getUserName(), papParameters.getRestServerParameters().getUserName());
62         assertEquals(restServerParameters.getPassword(), papParameters.getRestServerParameters().getPassword());
63         assertTrue(papParameters.getRestServerParameters().isHttps());
64         assertFalse(papParameters.getRestServerParameters().isAaf());
65         assertEquals(topicParameterGroup.getTopicSinks(), papParameters.getTopicParameterGroup().getTopicSinks());
66         assertEquals(topicParameterGroup.getTopicSources(), papParameters.getTopicParameterGroup().getTopicSources());
67     }
68
69     @Test
70     public void testPapParameterGroup_NullName() throws Exception {
71         String json = commonTestData.getPapParameterGroupAsString(1).replace("\"PapGroup\"", "null");
72         final PapParameterGroup papParameters = coder.decode(json, PapParameterGroup.class);
73         final GroupValidationResult validationResult = papParameters.validate();
74         assertFalse(validationResult.isValid());
75         assertEquals(null, papParameters.getName());
76         assertTrue(validationResult.getResult().contains("is null"));
77     }
78
79     @Test
80     public void testPapParameterGroup_EmptyName() throws Exception {
81         String json = commonTestData.getPapParameterGroupAsString(1).replace(CommonTestData.PAP_GROUP_NAME, "");
82         final PapParameterGroup papParameters = coder.decode(json, PapParameterGroup.class);
83         final GroupValidationResult validationResult = papParameters.validate();
84         assertFalse(validationResult.isValid());
85         assertEquals("", papParameters.getName());
86         assertTrue(validationResult.getResult().contains(
87                 "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string"));
88     }
89
90     @Test
91     public void testPapParameterGroup_SetName() {
92         final PapParameterGroup papParameters = commonTestData.getPapParameterGroup(1);
93         papParameters.setName("PapNewGroup");
94         final GroupValidationResult validationResult = papParameters.validate();
95         assertTrue(validationResult.isValid());
96         assertEquals("PapNewGroup", papParameters.getName());
97     }
98
99     @Test
100     public void testApiParameterGroup_EmptyRestServerParameters() throws Exception {
101         String json = commonTestData.getPapParameterGroupAsString(1);
102         json = commonTestData.nullifyField(json, "restServerParameters");
103         final PapParameterGroup papParameters = commonTestData.getPapParameterGroup(0);
104         final GroupValidationResult validationResult = papParameters.validate();
105         assertFalse(validationResult.isValid());
106         assertTrue(validationResult.getResult()
107                 .contains("\"org.onap.policy.common.endpoints.parameters.RestServerParameters\" INVALID, "
108                         + "parameter group has status INVALID"));
109     }
110 }