2 * ============LICENSE_START=======================================================
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
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.aaf.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.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;
36 import java.security.InvalidParameterException;
37 import java.util.ArrayList;
38 import java.util.List;
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;
44 @ExtendWith(SpringExtension.class)
45 @ContextConfiguration(classes = CertServiceApplication.class)
46 class Cmpv2ServersConfigurationValidatorTest {
48 private static final String EMPTY_STRING = "";
51 private Cmpv2ServersConfigurationValidator validator;
53 private Authentication authentication;
54 private Cmpv2Server server;
55 private List<Cmpv2Server> servers;
60 setServerConfiguration();
61 servers = new ArrayList<>();
66 void shouldThrowExceptionWhenCaNamesAreNotUnique() {
71 Exception exception = assertThrows(
72 InvalidParameterException.class,
73 () -> validator.validate(servers));
76 assertThat(exception.getMessage()).contains("CA names are not unique within given CMPv2 servers");
80 void shouldThrowExceptionWhenWrongProtocolInUrl() {
82 server.setUrl("https://test.test.test:60000/");
85 assertExceptionIsThrown();
89 void shouldThrowExceptionWhenWrongPortInUrl() {
91 server.setUrl("http://test.test.test:70000/");
94 assertExceptionIsThrown();
98 void shouldThrowExceptionWhenWrongCaNameLength() {
100 server.setCaName(EMPTY_STRING);
103 assertExceptionIsThrown();
107 void shouldThrowExceptionWhenWrongRvLength() {
109 authentication.setRv(EMPTY_STRING);
112 assertExceptionIsThrown();
117 void shouldThrowExceptionWhenWrongIakLength() {
119 authentication.setIak(EMPTY_STRING);
122 assertExceptionIsThrown();
126 void shouldThrowExceptionWhenCaNameIsNull() {
128 server.setCaName(null);
131 assertExceptionIsThrown();
135 void shouldThrowExceptionWhenIssuerDnIsNull() {
137 server.setIssuerDN(null);
140 assertExceptionIsThrown();
144 void shouldThrowExceptionWhenCaModeIsNull() {
146 server.setCaMode(null);
149 assertExceptionIsThrown();
153 void shouldThrowExceptionWhenUrlIsNull() {
158 assertExceptionIsThrown();
162 void shouldThrowExceptionWhenAuthenticationIsNull() {
164 server.setAuthentication(null);
167 assertExceptionIsThrown();
171 void shouldThrowExceptionWhenIakIsNull() {
173 authentication.setIak(null);
176 assertExceptionIsThrown();
180 void shouldThrowExceptionWhenRvIsNull() {
182 authentication.setRv(null);
185 assertExceptionIsThrown();
189 void shouldNotThrowExceptionWhenServerConfigurationIsValid() {
191 assertDoesNotThrow(() -> validator.validate(servers));
194 private void assertExceptionIsThrown() {
195 assertThrows(IllegalArgumentException.class, () -> validator.validate(servers));
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);
207 private void setAuthentication() {
208 authentication = new Authentication();
209 authentication.setRv("testRV");
210 authentication.setIak("testIAK");