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;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
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;
38 @ExtendWith(SpringExtension.class)
39 @ContextConfiguration(classes = CertServiceApplication.class)
40 class Cmpv2ServerConfigurationValidatorTest {
42 private static final String EMPTY_STRING = "";
45 private Cmpv2ServerConfigurationValidator validator;
47 private Authentication authentication;
48 private Cmpv2Server server;
53 setServerConfiguration();
57 void shouldNotThrowExceptionWhenServerConfigurationIsValid() {
59 assertDoesNotThrow(() -> validator.validate(server));
63 void shouldThrowExceptionWhenWrongProtocolInUrl() {
65 server.setUrl("https://test.test.test:60000/");
68 assertExceptionIsThrown();
72 void shouldThrowExceptionWhenWrongPortInUrl() {
74 server.setUrl("http://test.test.test:70000/");
77 assertExceptionIsThrown();
81 void shouldThrowExceptionWhenWrongCaNameLength() {
83 server.setCaName(EMPTY_STRING);
86 assertExceptionIsThrown();
90 void shouldThrowExceptionWhenWrongRvLength() {
92 authentication.setRv(EMPTY_STRING);
95 assertExceptionIsThrown();
100 void shouldThrowExceptionWhenWrongIakLength() {
102 authentication.setIak(EMPTY_STRING);
105 assertExceptionIsThrown();
109 void shouldThrowExceptionWhenCaNameIsNull() {
111 server.setCaName(null);
114 assertExceptionIsThrown();
118 void shouldThrowExceptionWhenIssuerDnIsNull() {
120 server.setIssuerDN(null);
123 assertExceptionIsThrown();
127 void shouldThrowExceptionWhenCaModeIsNull() {
129 server.setCaMode(null);
132 assertExceptionIsThrown();
136 void shouldThrowExceptionWhenUrlIsNull() {
141 assertExceptionIsThrown();
145 void shouldThrowExceptionWhenAuthenticationIsNull() {
147 server.setAuthentication(null);
150 assertExceptionIsThrown();
154 void shouldThrowExceptionWhenIakIsNull() {
156 authentication.setIak(null);
159 assertExceptionIsThrown();
163 void shouldThrowExceptionWhenRvIsNull() {
165 authentication.setRv(null);
168 assertExceptionIsThrown();
171 private void assertExceptionIsThrown() {
172 assertThrows(IllegalArgumentException.class, () -> validator.validate(server));
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);
184 private void setAuthentication() {
185 authentication = new Authentication();
186 authentication.setRv("testRV");
187 authentication.setIak("testIAK");