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