[OOM cert-service-client] Add validation of email, ip and domain name
[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.bouncycastle.asn1.x509.GeneralName;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.onap.oom.certservice.client.configuration.CsrConfigurationEnvs;
29 import org.onap.oom.certservice.client.configuration.EnvsForCsr;
30 import org.onap.oom.certservice.client.configuration.exception.CsrConfigurationException;
31 import org.onap.oom.certservice.client.configuration.model.CsrConfiguration;
32
33 import java.util.Optional;
34 import org.onap.oom.certservice.client.configuration.model.San;
35 import org.onap.oom.certservice.client.configuration.validation.csr.CommonNameValidator;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.onap.oom.certservice.client.api.ExitStatus.CSR_CONFIGURATION_EXCEPTION;
42
43 public class CsrConfigurationFactoryTest {
44
45     private static final String COMMON_NAME_VALID = "onap.org";
46     private static final String RAW_SAN1 = "ves-collector";
47     private static final String RAW_SAN2 = "ves";
48     private static final String RAW_SANS_VALID = String.format("%s,%s", RAW_SAN1, RAW_SAN2);
49     private static final String COUNTRY_VALID = "US";
50     private static final String LOCATION_VALID = "San-Francisco";
51     private static final String ORGANIZATION_VALID = "Linux-Foundation";
52     private static final String ORGANIZATION_UNIT_VALID = "ONAP";
53     private static final String STATE_VALID = "California";
54     private static final String COMMON_NAME_INVALID = "onap.org*&";
55     private static final String COUNTRY_INVALID = "PLA";
56     private static final String ORGANIZATION_INVALID = "Linux?Foundation";
57     private static final String INVALID_SANS = "192.168.1.";
58
59     private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
60     private CommonNameValidator commonNameValidator = new CommonNameValidator();
61     private SanMapper sanMapper = new SanMapper();
62     private CsrConfigurationFactory testedFactory;
63     private Condition<CsrConfigurationException> expectedExitCodeCondition = new Condition<>("Correct exit code") {
64         @Override
65         public boolean matches(CsrConfigurationException exception) {
66             return exception.applicationExitStatus() == CSR_CONFIGURATION_EXCEPTION;
67         }
68     };
69
70     @BeforeEach
71     void setUp() {
72         testedFactory = new CsrConfigurationFactory(envsForCsr, commonNameValidator, sanMapper);
73     }
74
75     @Test
76     void shouldReturnCorrectConfiguration_WhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
77         // given
78         mockEnvsWithAllValidParameters();
79         San san1 = new San(RAW_SAN1, GeneralName.dNSName);
80         San san2 = new San(RAW_SAN2, GeneralName.dNSName);
81         List<San> sans = List.of(san1, san2);
82
83         // when
84         CsrConfiguration configuration = testedFactory.create();
85
86         // then
87         assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
88         assertThat(configuration.getSans()).isEqualTo(sans);
89         assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
90         assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
91         assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
92         assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
93         assertThat(configuration.getState()).isEqualTo(STATE_VALID);
94     }
95
96     @Test
97     void shouldReturnCorrectConfiguration_WhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
98         // given
99         mockEnvsWithValidRequiredParameters();
100
101         // when
102         CsrConfiguration configuration = testedFactory.create();
103
104         // then
105         assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
106         assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
107         assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
108         assertThat(configuration.getState()).isEqualTo(STATE_VALID);
109     }
110
111
112     @Test
113     void shouldThrowCsrConfigurationException_WhenCommonNameInvalid() {
114         // given
115         mockEnvsWithInvalidCommonName();
116
117         // when/then
118         assertThatExceptionOfType(CsrConfigurationException.class)
119                 .isThrownBy(testedFactory::create)
120                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.")
121                 .has(expectedExitCodeCondition);
122     }
123
124     @Test
125     void shouldThrowCsrConfigurationException_WhenOrganizationInvalid() {
126         // given
127         mockEnvsWithInvalidOrganization();
128
129         // when/then
130         assertThatExceptionOfType(CsrConfigurationException.class)
131                 .isThrownBy(testedFactory::create)
132                 .withMessageContaining(CsrConfigurationEnvs.ORGANIZATION + " is invalid.")
133                 .has(expectedExitCodeCondition);
134
135     }
136
137     @Test
138     void shouldThrowCsrConfigurationException_WhenCountryInvalid() {
139         // given
140         mockEnvsWithInvalidCountry();
141
142         // when/then
143         assertThatExceptionOfType(CsrConfigurationException.class)
144                 .isThrownBy(testedFactory::create)
145                 .withMessageContaining(CsrConfigurationEnvs.COUNTRY + " is invalid.")
146                 .has(expectedExitCodeCondition);
147
148     }
149
150     @Test
151     void shouldThrowCsrConfigurationExceptionWhenStateInvalid() {
152         // given
153         mockEnvsWithInvalidState();
154         // when/then
155         assertThatExceptionOfType(CsrConfigurationException.class)
156                 .isThrownBy(testedFactory::create)
157                 .withMessageContaining(CsrConfigurationEnvs.STATE + " is invalid.")
158                 .has(expectedExitCodeCondition);
159     }
160
161     @Test
162     void shouldThrowCsrConfigurationExceptionWhenSansInvalid() {
163         // given
164         mockEnvsWithInvalidSans();
165         // when/then
166         assertThatExceptionOfType(CsrConfigurationException.class)
167                 .isThrownBy(testedFactory::create)
168                 .withMessageContaining("SAN :" + INVALID_SANS + " does not match any requirements")
169                 .has(expectedExitCodeCondition);
170     }
171
172     private void mockEnvsWithAllValidParameters() {
173         mockEnvsWithValidRequiredParameters();
174         mockEnvsWithValidOptionalParameters();
175     }
176
177     private void mockEnvsWithValidOptionalParameters() {
178         when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
179         when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
180         when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(RAW_SANS_VALID));
181     }
182
183     private void mockEnvsWithValidRequiredParameters() {
184         when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
185         when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
186         when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
187         when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
188     }
189
190     private void mockEnvsWithInvalidCommonName() {
191         mockEnvsWithAllValidParameters();
192         when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
193     }
194
195     private void mockEnvsWithInvalidCountry() {
196         mockEnvsWithAllValidParameters();
197         when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_INVALID));
198     }
199
200     private void mockEnvsWithInvalidOrganization() {
201         mockEnvsWithAllValidParameters();
202         when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_INVALID));
203     }
204
205     private void mockEnvsWithInvalidState() {
206         mockEnvsWithAllValidParameters();
207         when(envsForCsr.getState()).thenReturn(Optional.empty());
208     }
209
210     private void mockEnvsWithInvalidSans() {
211         mockEnvsWithAllValidParameters();
212         when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(INVALID_SANS));
213     }
214 }