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.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs;
28 import org.onap.oom.certservice.client.configuration.EnvsForCsr;
29 import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException;
30 import org.onap.oom.certservice.client.configuration.model.CsrConfiguration;
32 import java.util.Optional;
33 import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39 import static org.onap.oom.certservice.client.api.ExitStatus.CSR_CONFIGURATION_EXCEPTION;
41 public class CsrConfigurationFactoryTest {
43 private static final String COMMON_NAME_VALID = "onap.org";
44 private static final List<String> SANS_SPLITTED_VALID = List.of("test-name");
45 private static final String SANS_VALID = "test-name";
46 private static final String COUNTRY_VALID = "US";
47 private static final String LOCATION_VALID = "San-Francisco";
48 private static final String ORGANIZATION_VALID = "Linux-Foundation";
49 private static final String ORGANIZATION_UNIT_VALID = "ONAP";
50 private static final String STATE_VALID = "California";
51 private static final String COMMON_NAME_INVALID = "onap.org*&";
52 private static final String COUNTRY_INVALID = "PLA";
53 private static final String ORGANIZATION_INVALID = "Linux?Foundation";
55 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
56 private ValidatorsFactory validatorsFactory = new ValidatorsFactory();
57 private CsrConfigurationFactory testedFactory;
58 private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code") {
60 public boolean matches(CsrConfigurationException exception) {
61 return exception.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION;
67 testedFactory = new CsrConfigurationFactory(envsForCsr, validatorsFactory);
71 void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
73 mockEnvsWithAllValidParameters();
76 CsrConfiguration configuration = testedFactory.create();
79 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
80 assertThat(configuration.getSans()).isEqualTo(SANS_SPLITTED_VALID);
81 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
82 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
83 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
84 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
85 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
89 void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
91 mockEnvsWithValidRequiredParameters();
94 CsrConfiguration configuration = testedFactory.create();
97 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
98 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
99 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
100 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
105 void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
107 mockEnvsWithInvalidCommonName();
110 assertThatExceptionOfType(CsrConfigurationException.class)
111 .isThrownBy(testedFactory::create)
112 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
113 .has(expectedExitCodeCondition);
117 void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
119 mockEnvsWithInvalidOrganization();
122 assertThatExceptionOfType(CsrConfigurationException.class)
123 .isThrownBy(testedFactory::create)
124 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
125 .has(expectedExitCodeCondition);
130 void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
132 mockEnvsWithInvalidCountry();
135 assertThatExceptionOfType(CsrConfigurationException.class)
136 .isThrownBy(testedFactory::create)
137 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
138 .has(expectedExitCodeCondition);
143 void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
145 mockEnvsWithInvalidState();
147 assertThatExceptionOfType(CsrConfigurationException.class)
148 .isThrownBy(testedFactory::create)
149 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
150 .has(expectedExitCodeCondition);
153 private void mockEnvsWithAllValidParameters() {
154 mockEnvsWithValidRequiredParameters();
155 mockEnvsWithValidOptionalParameters();
158 private void mockEnvsWithValidOptionalParameters() {
159 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
160 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
161 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
164 private void mockEnvsWithValidRequiredParameters() {
165 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
166 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
167 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
168 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
171 private void mockEnvsWithInvalidCommonName() {
172 mockEnvsWithAllValidParameters();
173 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
176 private void mockEnvsWithInvalidCountry() {
177 mockEnvsWithAllValidParameters();
178 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
181 private void mockEnvsWithInvalidOrganization() {
182 mockEnvsWithAllValidParameters();
183 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
186 private void mockEnvsWithInvalidState() {
187 mockEnvsWithAllValidParameters();
188 when(envsForCsr.getState()).thenReturn(Optional.empty());