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.model;
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.api.ExitCode;
27 import org.onap.aaf.certservice.client.configuration.CsrConfigurationEnvs;
28 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
29 import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException;
30 import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory;
32 import java.util.Optional;
33 import java.util.function.Predicate;
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;
40 public class CsrConfigurationFactoryTest {
42 private final String COMMON_NAME_VALID = "onap.org";
43 private final String SANS_VALID = "test-name";
44 private final String COUNTRY_VALID = "US";
45 private final String LOCATION_VALID = "San-Francisco";
46 private final String ORGANIZATION_VALID = "Linux-Foundation";
47 private final String ORGANIZATION_UNIT_VALID = "ONAP";
48 private final String STATE_VALID = "California";
49 private final String COMMON_NAME_INVALID = "onap.org*&";
50 private final String COUNTRY_INVALID = "PLA";
51 private final String ORGANIZATION_INVALID = "Linux?Foundation";
53 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
54 private CsrConfigurationFactory testedFactory;
55 private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code"){
57 public boolean matches(CsrConfigurationException e) {
58 return e.applicationExitCode() == ExitCode.CSR_CONFIGURATION_EXCEPTION.getValue();
64 testedFactory = new CsrConfigurationFactory(envsForCsr);
68 void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
70 mockEnvsWithAllValidParameters();
73 CsrConfiguration configuration = testedFactory.create();
76 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
77 assertThat(configuration.getSans()).isEqualTo(SANS_VALID);
78 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
79 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
80 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
81 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
82 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
86 void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
88 mockEnvsWithValidRequiredParameters();
91 CsrConfiguration configuration = testedFactory.create();
94 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
95 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
96 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
97 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
102 void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
104 mockEnvsWithInvalidCommonName();
107 assertThatExceptionOfType(CsrConfigurationException.class)
108 .isThrownBy(testedFactory::create)
109 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
110 .has(expectedExitCodeCondition);
114 void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
116 mockEnvsWithInvalidOrganization();
119 assertThatExceptionOfType(CsrConfigurationException.class)
120 .isThrownBy(testedFactory::create)
121 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
122 .has(expectedExitCodeCondition);
127 void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
129 mockEnvsWithInvalidCountry();
132 assertThatExceptionOfType(CsrConfigurationException.class)
133 .isThrownBy(testedFactory::create)
134 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
135 .has(expectedExitCodeCondition);
140 void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
142 mockEnvsWithInvalidState();
144 assertThatExceptionOfType(CsrConfigurationException.class)
145 .isThrownBy(testedFactory::create)
146 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
147 .has(expectedExitCodeCondition);
150 private void mockEnvsWithAllValidParameters() {
151 mockEnvsWithValidRequiredParameters();
152 mockEnvsWithValidOptionalParameters();
155 private void mockEnvsWithValidOptionalParameters() {
156 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
157 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
158 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
161 private void mockEnvsWithValidRequiredParameters() {
162 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
163 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
164 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
165 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
168 private void mockEnvsWithInvalidCommonName() {
169 mockEnvsWithAllValidParameters();
170 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
173 private void mockEnvsWithInvalidCountry() {
174 mockEnvsWithAllValidParameters();
175 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
178 private void mockEnvsWithInvalidOrganization() {
179 mockEnvsWithAllValidParameters();
180 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
183 private void mockEnvsWithInvalidState() {
184 mockEnvsWithAllValidParameters();
185 when(envsForCsr.getState()).thenReturn(Optional.empty());