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