2 * ============LICENSE_START=======================================================
3 * aaf-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.aaf.certservice.client.configuration.factory;
23 import org.assertj.core.api.Condition;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.onap.aaf.certservice.client.configuration.CsrConfigurationEnvs;
27 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
28 import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException;
29 import org.onap.aaf.certservice.client.configuration.model.CsrConfiguration;
31 import java.util.Optional;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37 import static org.onap.aaf.certservice.client.api.ExitStatus.CSR_CONFIGURATION_EXCEPTION;
39 public class CsrConfigurationFactoryTest {
41 private final String COMMON_NAME_VALID = "onap.org";
42 private final String SANS_VALID = "test-name";
43 private final String COUNTRY_VALID = "US";
44 private final String LOCATION_VALID = "San-Francisco";
45 private final String ORGANIZATION_VALID = "Linux-Foundation";
46 private final String ORGANIZATION_UNIT_VALID = "ONAP";
47 private final String STATE_VALID = "California";
48 private final String COMMON_NAME_INVALID = "onap.org*&";
49 private final String COUNTRY_INVALID = "PLA";
50 private final String ORGANIZATION_INVALID = "Linux?Foundation";
52 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
53 private CsrConfigurationFactory testedFactory;
54 private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code"){
56 public boolean matches(CsrConfigurationException e) {
57 return e.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION;
63 testedFactory = new CsrConfigurationFactory(envsForCsr);
67 void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
69 mockEnvsWithAllValidParameters();
72 CsrConfiguration configuration = testedFactory.create();
75 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
76 assertThat(configuration.getSans()).isEqualTo(SANS_VALID);
77 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
78 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
79 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
80 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
81 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
85 void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
87 mockEnvsWithValidRequiredParameters();
90 CsrConfiguration configuration = testedFactory.create();
93 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
94 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
95 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
96 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
101 void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
103 mockEnvsWithInvalidCommonName();
106 assertThatExceptionOfType(CsrConfigurationException.class)
107 .isThrownBy(testedFactory::create)
108 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
109 .has(expectedExitCodeCondition);
113 void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
115 mockEnvsWithInvalidOrganization();
118 assertThatExceptionOfType(CsrConfigurationException.class)
119 .isThrownBy(testedFactory::create)
120 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
121 .has(expectedExitCodeCondition);
126 void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
128 mockEnvsWithInvalidCountry();
131 assertThatExceptionOfType(CsrConfigurationException.class)
132 .isThrownBy(testedFactory::create)
133 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
134 .has(expectedExitCodeCondition);
139 void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
141 mockEnvsWithInvalidState();
143 assertThatExceptionOfType(CsrConfigurationException.class)
144 .isThrownBy(testedFactory::create)
145 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
146 .has(expectedExitCodeCondition);
149 private void mockEnvsWithAllValidParameters() {
150 mockEnvsWithValidRequiredParameters();
151 mockEnvsWithValidOptionalParameters();
154 private void mockEnvsWithValidOptionalParameters() {
155 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
156 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
157 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
160 private void mockEnvsWithValidRequiredParameters() {
161 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
162 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
163 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
164 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
167 private void mockEnvsWithInvalidCommonName() {
168 mockEnvsWithAllValidParameters();
169 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
172 private void mockEnvsWithInvalidCountry() {
173 mockEnvsWithAllValidParameters();
174 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
177 private void mockEnvsWithInvalidOrganization() {
178 mockEnvsWithAllValidParameters();
179 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
182 private void mockEnvsWithInvalidState() {
183 mockEnvsWithAllValidParameters();
184 when(envsForCsr.getState()).thenReturn(Optional.empty());