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