Added validation of configuration
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / aaf / certservice / client / configuration / model / 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.model;
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.factory.ClientConfigurationFactory;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33
34 public class ClientConfigurationFactoryTest {
35
36     final String CA_NAME_VALID =  "caaaftest2";
37     final String TIME_OUT_VALID = "30000";
38     final String OUTPUT_PATH_VALID = "/opt/app/osaaf";
39     final String URL_TO_CERT_SERVICE_VALID = "http://cert-service:8080/v1/certificate/";
40     final String CA_NAME_INVALID =  "caaaftest2#$";
41     final String OUTPUT_PATH_INVALID = "/opt//app/osaaf";
42
43     private EnvsForClient envsForClient = mock(EnvsForClient.class);
44
45     @Test
46     void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() {
47         // given
48         when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID);
49         when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
50         when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID);
51         when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID);
52
53         // when
54         ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
55
56         // then
57         assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
58         assertThat(configuration.getRequestTimeout()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
59         assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
60         assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_VALID);
61     }
62
63     @Test
64     void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() {
65         // given
66         when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID);
67         when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
68
69         // when
70         ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
71
72         // then
73         assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
74         assertThat(configuration.getRequestTimeout()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
75         assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
76         assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_VALID);
77     }
78
79     @Test
80     void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() {
81         // given
82         when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
83
84         // when
85         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
86
87         // when/then
88         assertThatExceptionOfType(ClientConfigurationException.class)
89                 .isThrownBy(configurationFactory::create)
90                 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
91     }
92
93     @Test
94     void create_shouldReturnClientExceptionWhenCANameContainsSpecialCharacters() {
95         // given
96         when(envsForClient.getCaName()).thenReturn(CA_NAME_INVALID);
97         when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
98         when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID);
99         when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID);
100
101         // when
102         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
103
104         // when/then
105         assertThatExceptionOfType(ClientConfigurationException.class)
106                 .isThrownBy(configurationFactory::create)
107                 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
108     }
109
110     @Test
111     void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() {
112         // given
113         when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID);
114         when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_INVALID);
115         when(envsForClient.getRequestTimeOut()).thenReturn(TIME_OUT_VALID);
116         when(envsForClient.getUrlToCertService()).thenReturn(URL_TO_CERT_SERVICE_VALID);
117
118         // when
119         ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
120
121         // when/then
122         assertThatExceptionOfType(ClientConfigurationException.class)
123                 .isThrownBy(configurationFactory::create)
124                 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.");
125     }
126 }