bb566e811a91a1fd26557d0c42aa17e5ad5c8cee
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / configuration / model / CsrConfigurationFactoryTest.java
1 /*
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
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.aaf.certservice.client.configuration.model;
22
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;
31
32 import java.util.Optional;
33 import java.util.function.Predicate;
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
40 public class CsrConfigurationFactoryTest {
41
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";
52
53     private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
54     private CsrConfigurationFactory testedFactory;
55     private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code"){
56         @Override
57         public boolean matches(CsrConfigurationException e) {
58             return e.applicationExitCode() == ExitCode.CSR_CONFIGURATION_EXCEPTION.getValue();
59         }
60     };
61
62     @BeforeEach
63     void setUp() {
64         testedFactory = new CsrConfigurationFactory(envsForCsr);
65     }
66
67     @Test
68     void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
69         // given
70         mockEnvsWithAllValidParameters();
71
72         // when
73         CsrConfiguration configuration = testedFactory.create();
74
75         // then
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);
83     }
84
85     @Test
86     void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
87         // given
88         mockEnvsWithValidRequiredParameters();
89
90         // when
91         CsrConfiguration configuration = testedFactory.create();
92
93         // then
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);
98     }
99
100
101     @Test
102     void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
103         // given
104         mockEnvsWithInvalidCommonName();
105
106         // when/then
107         assertThatExceptionOfType(CsrConfigurationException.class)
108                 .isThrownBy(testedFactory::create)
109                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
110                 .has(expectedExitCodeCondition);
111     }
112
113     @Test
114     void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
115         // given
116         mockEnvsWithInvalidOrganization();
117
118         // when/then
119         assertThatExceptionOfType(CsrConfigurationException.class)
120                 .isThrownBy(testedFactory::create)
121                 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
122                 .has(expectedExitCodeCondition);
123
124     }
125
126     @Test
127     void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
128         // given
129         mockEnvsWithInvalidCountry();
130
131         // when/then
132         assertThatExceptionOfType(CsrConfigurationException.class)
133                 .isThrownBy(testedFactory::create)
134                 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
135                 .has(expectedExitCodeCondition);
136
137     }
138
139     @Test
140     void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
141         // given
142         mockEnvsWithInvalidState();
143         // when/then
144         assertThatExceptionOfType(CsrConfigurationException.class)
145                 .isThrownBy(testedFactory::create)
146                 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
147                 .has(expectedExitCodeCondition);
148     }
149
150     private void mockEnvsWithAllValidParameters() {
151         mockEnvsWithValidRequiredParameters();
152         mockEnvsWithValidOptionalParameters();
153     }
154
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));
159     }
160
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));
166     }
167
168     private void mockEnvsWithInvalidCommonName() {
169         mockEnvsWithAllValidParameters();
170         when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
171     }
172
173     private void mockEnvsWithInvalidCountry() {
174         mockEnvsWithAllValidParameters();
175         when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
176     }
177
178     private void mockEnvsWithInvalidOrganization() {
179         mockEnvsWithAllValidParameters();
180         when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
181     }
182
183     private void mockEnvsWithInvalidState() {
184         mockEnvsWithAllValidParameters();
185         when(envsForCsr.getState()).thenReturn(Optional.empty());
186     }
187 }