Merge "Removed usage of RegTokenPwd when generating cert request"
[oom/platform/cert-service.git] / certService / src / test / java / org / onap / aaf / certservice / certification / configuration / validation / Cmpv2ServersConfigurationValidatorTest.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
24 import org.bouncycastle.asn1.x500.X500Name;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.onap.aaf.certservice.CertServiceApplication;
29 import org.onap.aaf.certservice.certification.configuration.model.Authentication;
30 import org.onap.aaf.certservice.certification.configuration.model.CaMode;
31 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit.jupiter.SpringExtension;
35
36 import java.security.InvalidParameterException;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
42 import static org.junit.jupiter.api.Assertions.assertThrows;
43
44 @ExtendWith(SpringExtension.class)
45 @ContextConfiguration(classes = CertServiceApplication.class)
46 class Cmpv2ServersConfigurationValidatorTest {
47
48     private static final String EMPTY_STRING = "";
49
50     @Autowired
51     private Cmpv2ServersConfigurationValidator validator;
52
53     private Authentication authentication;
54     private Cmpv2Server server;
55     private List<Cmpv2Server> servers;
56
57     @BeforeEach
58     private void init() {
59         setAuthentication();
60         setServerConfiguration();
61         servers = new ArrayList<>();
62         servers.add(server);
63     }
64
65     @Test
66     void shouldThrowExceptionWhenCaNamesAreNotUnique() {
67         // Given
68         servers.add(server);
69
70         // When
71         Exception exception = assertThrows(
72                 InvalidParameterException.class,
73                 () -> validator.validate(servers));
74
75         // Then
76         assertThat(exception.getMessage()).contains("CA names are not unique within given CMPv2 servers");
77     }
78
79     @Test
80     void shouldThrowExceptionWhenWrongProtocolInUrl() {
81         // Given
82         server.setUrl("https://test.test.test:60000/");
83
84         // Then
85         assertExceptionIsThrown();
86     }
87
88     @Test
89     void shouldThrowExceptionWhenWrongPortInUrl() {
90         // Given
91         server.setUrl("http://test.test.test:70000/");
92
93         // Then
94         assertExceptionIsThrown();
95     }
96
97     @Test
98     void shouldThrowExceptionWhenWrongCaNameLength() {
99         // Given
100         server.setCaName(EMPTY_STRING);
101
102         // Then
103         assertExceptionIsThrown();
104     }
105
106     @Test
107     void shouldThrowExceptionWhenWrongRvLength() {
108         // Given
109         authentication.setRv(EMPTY_STRING);
110
111         // Then
112         assertExceptionIsThrown();
113     }
114
115
116     @Test
117     void shouldThrowExceptionWhenWrongIakLength() {
118         // Given
119         authentication.setIak(EMPTY_STRING);
120
121         // Then
122         assertExceptionIsThrown();
123     }
124
125     @Test
126     void shouldThrowExceptionWhenCaNameIsNull() {
127         // Given
128         server.setCaName(null);
129
130         // Then
131         assertExceptionIsThrown();
132     }
133
134     @Test
135     void shouldThrowExceptionWhenIssuerDnIsNull() {
136         // Given
137         server.setIssuerDN(null);
138
139         // Then
140         assertExceptionIsThrown();
141     }
142
143     @Test
144     void shouldThrowExceptionWhenCaModeIsNull() {
145         // Given
146         server.setCaMode(null);
147
148         // Then
149         assertExceptionIsThrown();
150     }
151
152     @Test
153     void shouldThrowExceptionWhenUrlIsNull() {
154         // Given
155         server.setUrl(null);
156
157         // Then
158         assertExceptionIsThrown();
159     }
160
161     @Test
162     void shouldThrowExceptionWhenAuthenticationIsNull() {
163         // Given
164         server.setAuthentication(null);
165
166         // Then
167         assertExceptionIsThrown();
168     }
169
170     @Test
171     void shouldThrowExceptionWhenIakIsNull() {
172         // Given
173         authentication.setIak(null);
174
175         // Then
176         assertExceptionIsThrown();
177     }
178
179     @Test
180     void shouldThrowExceptionWhenRvIsNull() {
181         // Given
182         authentication.setRv(null);
183
184         // Then
185         assertExceptionIsThrown();
186     }
187
188     @Test
189     void shouldNotThrowExceptionWhenServerConfigurationIsValid() {
190         // Then
191         assertDoesNotThrow(() -> validator.validate(servers));
192     }
193
194     private void assertExceptionIsThrown() {
195         assertThrows(IllegalArgumentException.class, () -> validator.validate(servers));
196     }
197
198     private void setServerConfiguration() {
199         server = new Cmpv2Server();
200         server.setCaMode(CaMode.CLIENT);
201         server.setCaName("TEST");
202         server.setIssuerDN(new X500Name("CN=ManagementCA"));
203         server.setUrl("http://127.0.0.1/ejbca/publicweb/cmp/cmp");
204         server.setAuthentication(authentication);
205     }
206
207     private void setAuthentication() {
208         authentication = new Authentication();
209         authentication.setRv("testRV");
210         authentication.setIak("testIAK");
211     }
212
213 }