2 * ============LICENSE_START=======================================================
3 * oom-certservice-client
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.oom.certservice.client.configuration.factory;
23 import java.util.List;
24 import org.assertj.core.api.Condition;
25 import org.bouncycastle.asn1.x509.GeneralName;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs;
29 import org.onap.oom.certservice.client.configuration.EnvsForCsr;
30 import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException;
31 import org.onap.oom.certservice.client.configuration.model.CsrConfiguration;
33 import java.util.Optional;
34 import org.onap.oom.certservice.client.configuration.model.San;
35 import org.onap.oom.certservice.client.configuration.validation.csr.CommonNameValidator;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.onap.oom.certservice.client.api.ExitStatus.CSR_CONFIGURATION_EXCEPTION;
43 public class CsrConfigurationFactoryTest {
45 private static final String COMMON_NAME_VALID = "onap.org";
46 private static final String RAW_SAN1 = "ves-collector";
47 private static final String RAW_SAN2 = "ves";
48 private static final String RAW_SANS_VALID = String.format("%s,%s", RAW_SAN1, RAW_SAN2);
49 private static final String COUNTRY_VALID = "US";
50 private static final String LOCATION_VALID = "San-Francisco";
51 private static final String ORGANIZATION_VALID = "Linux-Foundation";
52 private static final String ORGANIZATION_UNIT_VALID = "ONAP";
53 private static final String STATE_VALID = "California";
54 private static final String COMMON_NAME_INVALID = "onap.org*&";
55 private static final String COUNTRY_INVALID = "PLA";
56 private static final String ORGANIZATION_INVALID = "Linux?Foundation";
57 private static final String INVALID_SANS = "192.168.1.";
59 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
60 private CommonNameValidator commonNameValidator = new CommonNameValidator();
61 private SanMapper sanMapper = new SanMapper();
62 private CsrConfigurationFactory testedFactory;
63 private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code") {
65 public boolean matches(CsrConfigurationException exception) {
66 return exception.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION;
72 testedFactory = new CsrConfigurationFactory(envsForCsr, commonNameValidator, sanMapper);
76 void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
78 mockEnvsWithAllValidParameters();
79 San san1 = new San(RAW_SAN1, GeneralName.dNSName);
80 San san2 = new San(RAW_SAN2, GeneralName.dNSName);
81 List<San> sans = List.of(san1, san2);
84 CsrConfiguration configuration = testedFactory.create();
87 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
88 assertThat(configuration.getSans()).isEqualTo(sans);
89 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
90 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
91 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
92 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
93 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
97 void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
99 mockEnvsWithValidRequiredParameters();
102 CsrConfiguration configuration = testedFactory.create();
105 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
106 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
107 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
108 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
113 void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
115 mockEnvsWithInvalidCommonName();
118 assertThatExceptionOfType(CsrConfigurationException.class)
119 .isThrownBy(testedFactory::create)
120 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
121 .has(expectedExitCodeCondition);
125 void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
127 mockEnvsWithInvalidOrganization();
130 assertThatExceptionOfType(CsrConfigurationException.class)
131 .isThrownBy(testedFactory::create)
132 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
133 .has(expectedExitCodeCondition);
138 void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
140 mockEnvsWithInvalidCountry();
143 assertThatExceptionOfType(CsrConfigurationException.class)
144 .isThrownBy(testedFactory::create)
145 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
146 .has(expectedExitCodeCondition);
151 void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
153 mockEnvsWithInvalidState();
155 assertThatExceptionOfType(CsrConfigurationException.class)
156 .isThrownBy(testedFactory::create)
157 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
158 .has(expectedExitCodeCondition);
162 void shouldThrowCsrConfigurationExceptionWhenSansInvalid() {
164 mockEnvsWithInvalidSans();
166 assertThatExceptionOfType(CsrConfigurationException.class)
167 .isThrownBy(testedFactory::create)
168 .withMessageContaining("SAN :" + INVALID_SANS + " does not match any requirements")
169 .has(expectedExitCodeCondition);
172 private void mockEnvsWithAllValidParameters() {
173 mockEnvsWithValidRequiredParameters();
174 mockEnvsWithValidOptionalParameters();
177 private void mockEnvsWithValidOptionalParameters() {
178 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
179 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
180 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(RAW_SANS_VALID));
183 private void mockEnvsWithValidRequiredParameters() {
184 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
185 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
186 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
187 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
190 private void mockEnvsWithInvalidCommonName() {
191 mockEnvsWithAllValidParameters();
192 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
195 private void mockEnvsWithInvalidCountry() {
196 mockEnvsWithAllValidParameters();
197 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
200 private void mockEnvsWithInvalidOrganization() {
201 mockEnvsWithAllValidParameters();
202 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
205 private void mockEnvsWithInvalidState() {
206 mockEnvsWithAllValidParameters();
207 when(envsForCsr.getState()).thenReturn(Optional.empty());
210 private void mockEnvsWithInvalidSans() {
211 mockEnvsWithAllValidParameters();
212 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(INVALID_SANS));