2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.oom.certservice.certification.configuration.validation;
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;
35 import java.security.InvalidParameterException;
36 import java.util.ArrayList;
37 import java.util.List;
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;
43 @ExtendWith(SpringExtension.class)
44 @ContextConfiguration(classes = CertServiceApplication.class)
45 class Cmpv2ServersConfigurationValidatorTest {
47 private static final String EMPTY_STRING = "";
50 private Cmpv2ServersConfigurationValidator validator;
52 private Authentication authentication;
53 private Cmpv2Server server;
54 private List<Cmpv2Server> servers;
59 setServerConfiguration();
60 servers = new ArrayList<>();
65 void shouldThrowExceptionWhenCaNamesAreNotUnique() {
70 Exception exception = assertThrows(
71 InvalidParameterException.class,
72 () -> validator.validate(servers));
75 assertThat(exception.getMessage()).contains("CA names are not unique within given CMPv2 servers");
79 void shouldThrowExceptionWhenWrongProtocolInUrl() {
81 server.setUrl("https://test.test.test:60000/");
84 assertExceptionIsThrown();
88 void shouldThrowExceptionWhenWrongPortInUrl() {
90 server.setUrl("http://test.test.test:70000/");
93 assertExceptionIsThrown();
97 void shouldThrowExceptionWhenWrongCaNameLength() {
99 server.setCaName(EMPTY_STRING);
102 assertExceptionIsThrown();
106 void shouldThrowExceptionWhenWrongRvLength() {
108 authentication.setRv(EMPTY_STRING);
111 assertExceptionIsThrown();
116 void shouldThrowExceptionWhenWrongIakLength() {
118 authentication.setIak(EMPTY_STRING);
121 assertExceptionIsThrown();
125 void shouldThrowExceptionWhenCaNameIsNull() {
127 server.setCaName(null);
130 assertExceptionIsThrown();
134 void shouldThrowExceptionWhenIssuerDnIsNull() {
136 server.setIssuerDN(null);
139 assertExceptionIsThrown();
143 void shouldThrowExceptionWhenUrlIsNull() {
148 assertExceptionIsThrown();
152 void shouldThrowExceptionWhenAuthenticationIsNull() {
154 server.setAuthentication(null);
157 assertExceptionIsThrown();
161 void shouldThrowExceptionWhenIakIsNull() {
163 authentication.setIak(null);
166 assertExceptionIsThrown();
170 void shouldThrowExceptionWhenRvIsNull() {
172 authentication.setRv(null);
175 assertExceptionIsThrown();
179 void shouldNotThrowExceptionWhenServerConfigurationIsValid() {
181 assertDoesNotThrow(() -> validator.validate(servers));
184 private void assertExceptionIsThrown() {
185 assertThrows(IllegalArgumentException.class, () -> validator.validate(servers));
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);
196 private void setAuthentication() {
197 authentication = new Authentication();
198 authentication.setRv("testRV");
199 authentication.setIak("testIAK");