[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 / ClientConfigurationFactoryTest.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 org.junit.jupiter.api.Test;
24 import org.onap.oom.certservice.client.configuration.ClientConfigurationEnvs;
25 import org.onap.oom.certservice.client.configuration.EnvsForClient;
26 import org.onap.oom.certservice.client.configuration.exception.ClientConfigurationException;
27 import org.onap.oom.certservice.client.configuration.model.ClientConfiguration;
28
29 import java.util.Optional;
30 import org.onap.oom.certservice.client.configuration.validation.client.OutputTypeValidator;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36
37 public class ClientConfigurationFactoryTest {
38
39     private static final String CA_NAME_VALID = "catest2";
40     private static final String TIME_OUT_VALID = "30000";
41     private static final String OUTPUT_PATH_VALID = "/opt/app/oom";
42     private static final String URL_TO_CERT_SERVICE_VALID = "https://cert-service:8443/v1/certificate/";
43     private static final String URL_TO_CERT_SERVICE_DEFAULT = "https://oom-cert-service:8443/v1/certificate/";
44     private static final String CA_NAME_INVALID = "catest2#$";
45     private static final String OUTPUT_PATH_INVALID = "/opt//app/oom";
46     private static final String OUTPUT_TYPE_VALID = "JKS";
47     private static final String OUTPUT_TYPE_INVALID = "JKSS";
48     private static final String OUTPUT_TYPE_DEFAULT = "P12";
49
50     private EnvsForClient envsForClient = mock(EnvsForClient.class);
51     private OutputTypeValidator outputTypeValidator = new OutputTypeValidator();
52
53
54     @Test
55     void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws ClientConfigurationException {
56         // given
57         when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
58         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
59         when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
60         when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
61         when(envsForClient.getOutputType()).thenReturn(Optional.of(OUTPUT_TYPE_VALID));
62
63         // when
64         ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
65         System.out.println(configuration.toString());
66
67         // then
68         assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
69         assertThat(configuration.getRequestTimeoutInMs()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
70         assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
71         assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_VALID);
72         assertThat(configuration.getOutputType()).isEqualTo(OUTPUT_TYPE_VALID);
73     }
74
75     @Test
76     void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException {
77         // given
78         when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
79         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
80
81         // when
82         ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
83
84         // then
85         assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
86         assertThat(configuration.getRequestTimeoutInMs()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
87         assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
88         assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_DEFAULT);
89         assertThat(configuration.getOutputType()).isEqualTo(OUTPUT_TYPE_DEFAULT);
90     }
91
92     @Test
93     void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() {
94         // given
95         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
96
97         // when
98         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
99             outputTypeValidator);
100
101         // then
102         assertThatExceptionOfType(ClientConfigurationException.class)
103                 .isThrownBy(configurationFactory::create)
104                 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
105     }
106
107     @Test
108     void create_shouldReturnClientExceptionWhenCaNameContainsSpecialCharacters() {
109         // given
110         when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_INVALID));
111         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
112         when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
113         when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
114
115         // when
116         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
117             outputTypeValidator);
118
119         // when/then
120         assertThatExceptionOfType(ClientConfigurationException.class)
121                 .isThrownBy(configurationFactory::create)
122                 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
123     }
124
125     @Test
126     void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() {
127         // given
128         when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
129         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_INVALID));
130         when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
131         when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
132
133         // when
134         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
135             outputTypeValidator);
136
137         //then
138         assertThatExceptionOfType(ClientConfigurationException.class)
139                 .isThrownBy(configurationFactory::create)
140                 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.");
141     }
142
143     @Test
144     void create_shouldReturnClientExceptionWhenOutputTypeIsInvalid() {
145         // given
146         when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
147         when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
148         when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
149         when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
150         when(envsForClient.getOutputType()).thenReturn(Optional.of(OUTPUT_TYPE_INVALID));
151
152         // when
153         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
154             outputTypeValidator);
155
156         //then
157         assertThatExceptionOfType(ClientConfigurationException.class)
158                 .isThrownBy(configurationFactory::create)
159                 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_TYPE + " is invalid.");
160     }
161 }