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