1c021b437d9cdb6e434a80974359a1ed4ac02099
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / validation / Cmpv2ServerConfigurationValidatorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aaf.certservice.certification.configuration.validation;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import org.bouncycastle.asn1.x500.X500Name;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.onap.aaf.certservice.CertServiceApplication;
31 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
32 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
33 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.test.context.ContextConfiguration;
36 import org.springframework.test.context.junit.jupiter.SpringExtension;
37
38 @ExtendWith(SpringExtension.class)
39 @ContextConfiguration(classes = CertServiceApplication.class)
40 class Cmpv2ServerConfigurationValidatorTest {
41
42     private static final String EMPTY_STRING = "";
43
44     @Autowired
45     private Cmpv2ServerConfigurationValidator validator;
46
47     private Authentication authentication;
48     private Cmpv2Server server;
49
50     @BeforeEach
51     private void init() {
52         setAuthentication();
53         setServerConfiguration();
54     }
55
56     @Test
57     void shouldNotThrowExceptionWhenServerConfigurationIsValid() {
58         // Then
59         assertDoesNotThrow(() -> validator.validate(server));
60     }
61
62     @Test
63     void shouldThrowExceptionWhenWrongProtocolInUrl() {
64         // Given
65         server.setUrl("https://test.test.test:60000/");
66
67         // Then
68         assertExceptionIsThrown();
69     }
70
71     @Test
72     void shouldThrowExceptionWhenWrongPortInUrl() {
73         // Given
74         server.setUrl("http://test.test.test:70000/");
75
76         // Then
77         assertExceptionIsThrown();
78     }
79
80     @Test
81     void shouldThrowExceptionWhenWrongCaNameLength() {
82         // Given
83         server.setCaName(EMPTY_STRING);
84
85         // Then
86         assertExceptionIsThrown();
87     }
88
89     @Test
90     void shouldThrowExceptionWhenWrongRvLength() {
91         // Given
92         authentication.setRv(EMPTY_STRING);
93
94         // Then
95         assertExceptionIsThrown();
96     }
97
98
99     @Test
100     void shouldThrowExceptionWhenWrongIakLength() {
101         // Given
102         authentication.setIak(EMPTY_STRING);
103
104         // Then
105         assertExceptionIsThrown();
106     }
107
108     @Test
109     void shouldThrowExceptionWhenCaNameIsNull() {
110         // Given
111         server.setCaName(null);
112
113         // Then
114         assertExceptionIsThrown();
115     }
116
117     @Test
118     void shouldThrowExceptionWhenIssuerDnIsNull() {
119         // Given
120         server.setIssuerDN(null);
121
122         // Then
123         assertExceptionIsThrown();
124     }
125
126     @Test
127     void shouldThrowExceptionWhenCaModeIsNull() {
128         // Given
129         server.setCaMode(null);
130
131         // Then
132         assertExceptionIsThrown();
133     }
134
135     @Test
136     void shouldThrowExceptionWhenUrlIsNull() {
137         // Given
138         server.setUrl(null);
139
140         // Then
141         assertExceptionIsThrown();
142     }
143
144     @Test
145     void shouldThrowExceptionWhenAuthenticationIsNull() {
146         // Given
147         server.setAuthentication(null);
148
149         // Then
150         assertExceptionIsThrown();
151     }
152
153     @Test
154     void shouldThrowExceptionWhenIakIsNull() {
155         // Given
156         authentication.setIak(null);
157
158         // Then
159         assertExceptionIsThrown();
160     }
161
162     @Test
163     void shouldThrowExceptionWhenRvIsNull() {
164         // Given
165         authentication.setRv(null);
166
167         // Then
168         assertExceptionIsThrown();
169     }
170
171     private void assertExceptionIsThrown() {
172         assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
173     }
174
175     private void setServerConfiguration() {
176         server = new Cmpv2Server();
177         server.setCaMode(CaMode.CLIENT);
178         server.setCaName("TEST");
179         server.setIssuerDN(new X500Name("CN=ManagementCA"));
180         server.setUrl("http://127.0.0.1/ejbca/publicweb/cmp/cmp");
181         server.setAuthentication(authentication);
182     }
183
184     private void setAuthentication() {
185         authentication = new Authentication();
186         authentication.setRv("testRV");
187         authentication.setIak("testIAK");
188     }
189 }