6013ff2d0b4b76e4f153f4ee646498da0005f7c2
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2024 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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.common.endpoints.parameters;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.onap.policy.common.parameters.BeanValidationResult;
28 import org.onap.policy.common.parameters.ValidationStatus;
29
30 class RestClientParametersTest {
31
32     private RestClientParameters params;
33
34     @BeforeEach
35     void setUp() {
36         params = new RestClientParameters();
37     }
38
39     @Test
40     void testValidate_ValidParameters() {
41         params.setHostname("localhost");
42         params.setClientName("testClient");
43         params.setPort(8080);
44
45         BeanValidationResult result = params.validate();
46
47         assertEquals(ValidationStatus.CLEAN, result.getStatus(), "Expected the parameters to be valid");
48         assertNull(result.getResult(), "Expected no validation errors");
49     }
50
51     @Test
52     void testValidate_InvalidHostname() {
53         params.setHostname("");
54         params.setClientName("testClient");
55         params.setPort(8080);
56
57         BeanValidationResult result = params.validate();
58
59         assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
60         assertTrue(result.getResult().contains("hostname") && result.getResult().contains("is blank"),
61             "Expected invalid hostname error message");
62     }
63
64     @Test
65     void testValidate_InvalidClientName() {
66         params.setHostname("localhost");
67         params.setClientName("");
68         params.setPort(8080);
69
70         BeanValidationResult result = params.validate();
71
72         assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
73         assertTrue(result.getResult().contains("clientName") && result.getResult().contains("is blank"),
74             "Expected invalid clientName error message");
75     }
76
77     @Test
78     void testValidate_InvalidPort() {
79         params.setHostname("localhost");
80         params.setClientName("testClient");
81         params.setPort(-1);
82
83         BeanValidationResult result = params.validate();
84
85         assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
86         assertTrue(result.getResult().contains("port") && result.getResult().contains("is not valid"),
87             "Expected invalid port error message");
88     }
89
90     @Test
91     void testValidate_MultipleInvalidParameters() {
92         params.setHostname("");
93         params.setClientName("");
94         params.setPort(-1);
95
96         BeanValidationResult result = params.validate();
97
98         assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
99
100         assertTrue(result.getResult().contains("hostname") && result.getResult().contains("is blank"),
101             "Expected invalid hostname error message");
102
103         assertTrue(result.getResult().contains("clientName") && result.getResult().contains("is blank"),
104             "Expected invalid clientName error message");
105
106         assertTrue(result.getResult().contains("port") && result.getResult().contains("is not valid"),
107             "Expected invalid port error message");
108     }
109
110     @Test
111     void testGetAndSetName() {
112         String name = "testClient";
113         params.setName(name);
114         assertEquals(name, params.getName(), "Expected the client name to be set and retrieved correctly");
115     }
116 }