d224552142d136ef24cb493fcd924856c4e1b396
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.starter.parameters;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Map;
28
29 import org.junit.Test;
30 import org.onap.policy.common.parameters.GroupValidationResult;
31
32 /**
33  * Class to perform unit test of {@link ApexStarterParameterGroup}.
34  *
35  * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
36  */
37 public class TestApexStarterParameterGroup {
38     CommonTestData commonTestData = new CommonTestData();
39
40     @Test
41     public void testApexStarterParameterGroup_Named() {
42         final ApexStarterParameterGroup apexStarterParameters = new ApexStarterParameterGroup("my-name");
43         assertEquals("my-name", apexStarterParameters.getName());
44     }
45
46     @Test
47     public void testApexStarterParameterGroup() {
48         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
49                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
50                 ApexStarterParameterGroup.class);
51         final GroupValidationResult validationResult = apexStarterParameters.validate();
52         assertTrue(validationResult.isValid());
53         assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.getName());
54         assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval());
55     }
56
57     @Test
58     public void testApexStarterParameterGroup_NullName() {
59         final ApexStarterParameterGroup apexStarterParameters = commonTestData
60                 .toObject(commonTestData.getApexStarterParameterGroupMap(null), ApexStarterParameterGroup.class);
61         final GroupValidationResult validationResult = apexStarterParameters.validate();
62         assertFalse(validationResult.isValid());
63         assertEquals(null, apexStarterParameters.getName());
64         assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval());
65         assertTrue(validationResult.getResult().contains("is null"));
66     }
67
68     @Test
69     public void testApexStarterParameterGroup_EmptyName() {
70         final ApexStarterParameterGroup apexStarterParameters = commonTestData
71                 .toObject(commonTestData.getApexStarterParameterGroupMap(""), ApexStarterParameterGroup.class);
72         final GroupValidationResult validationResult = apexStarterParameters.validate();
73         assertFalse(validationResult.isValid());
74         assertEquals("", apexStarterParameters.getName());
75         assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval());
76         assertTrue(validationResult.getResult().contains(
77                 "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string"));
78     }
79
80     @Test
81     public void testApexStarterParameterGroup_SetName() {
82         final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(
83                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME),
84                 ApexStarterParameterGroup.class);
85         apexStarterParameters.setName("ApexStarterNewGroup");
86         final GroupValidationResult validationResult = apexStarterParameters.validate();
87         assertTrue(validationResult.isValid());
88         assertEquals("ApexStarterNewGroup", apexStarterParameters.getName());
89     }
90
91     @Test
92     public void testApexStarterParameterGroup_EmptyTimeInterval() {
93         final Map<String, Object> map =
94                 commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME);
95         map.put("timeInterval", commonTestData.getTimeInterval(true));
96         final ApexStarterParameterGroup apexStarterParameters =
97                 commonTestData.toObject(map, ApexStarterParameterGroup.class);
98         final GroupValidationResult validationResult = apexStarterParameters.validate();
99         assertFalse(validationResult.isValid());
100         assertTrue(validationResult.getResult()
101                 .contains("field \"timeInterval\" type \"int\" value \"0\" INVALID, must be >= 1")
102                 && validationResult.getResult().contains("parameter group has status INVALID"));
103     }
104 }