238b979d75e7a3188d42418896340f730b2fe705
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / oom / certservice / client / configuration / factory / CsrConfigurationFactoryTest.java
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.oom.certservice.client.configuration.factory;
22
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;
31
32 import java.util.Optional;
33 import org.onap.oom.certservice.client.configuration.validation.ValidatorsFactory;
34
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;
40
41 public class CsrConfigurationFactoryTest {
42
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";
54
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") {
59         @Override
60         public boolean matches(CsrConfigurationException exception) {
61             return exception.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION;
62         }
63     };
64
65     @BeforeEach
66     void setUp() {
67         testedFactory = new CsrConfigurationFactory(envsForCsr, validatorsFactory);
68     }
69
70     @Test
71     void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
72         // given
73         mockEnvsWithAllValidParameters();
74
75         // when
76         CsrConfiguration configuration = testedFactory.create();
77
78         // then
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);
86     }
87
88     @Test
89     void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
90         // given
91         mockEnvsWithValidRequiredParameters();
92
93         // when
94         CsrConfiguration configuration = testedFactory.create();
95
96         // then
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);
101     }
102
103
104     @Test
105     void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
106         // given
107         mockEnvsWithInvalidCommonName();
108
109         // when/then
110         assertThatExceptionOfType(CsrConfigurationException.class)
111                 .isThrownBy(testedFactory::create)
112                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
113                 .has(expectedExitCodeCondition);
114     }
115
116     @Test
117     void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
118         // given
119         mockEnvsWithInvalidOrganization();
120
121         // when/then
122         assertThatExceptionOfType(CsrConfigurationException.class)
123                 .isThrownBy(testedFactory::create)
124                 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
125                 .has(expectedExitCodeCondition);
126
127     }
128
129     @Test
130     void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
131         // given
132         mockEnvsWithInvalidCountry();
133
134         // when/then
135         assertThatExceptionOfType(CsrConfigurationException.class)
136                 .isThrownBy(testedFactory::create)
137                 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
138                 .has(expectedExitCodeCondition);
139
140     }
141
142     @Test
143     void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
144         // given
145         mockEnvsWithInvalidState();
146         // when/then
147         assertThatExceptionOfType(CsrConfigurationException.class)
148                 .isThrownBy(testedFactory::create)
149                 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
150                 .has(expectedExitCodeCondition);
151     }
152
153     private void mockEnvsWithAllValidParameters() {
154         mockEnvsWithValidRequiredParameters();
155         mockEnvsWithValidOptionalParameters();
156     }
157
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));
162     }
163
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));
169     }
170
171     private void mockEnvsWithInvalidCommonName() {
172         mockEnvsWithAllValidParameters();
173         when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
174     }
175
176     private void mockEnvsWithInvalidCountry() {
177         mockEnvsWithAllValidParameters();
178         when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
179     }
180
181     private void mockEnvsWithInvalidOrganization() {
182         mockEnvsWithAllValidParameters();
183         when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
184     }
185
186     private void mockEnvsWithInvalidState() {
187         mockEnvsWithAllValidParameters();
188         when(envsForCsr.getState()).thenReturn(Optional.empty());
189     }
190 }