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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.oom.certservice.client.configuration.factory;
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;
29 import java.util.Optional;
30 import org.onap.oom.certservice.client.configuration.validation.client.OutputTypeValidator;
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;
37 public class ClientConfigurationFactoryTest {
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";
50 private EnvsForClient envsForClient = mock(EnvsForClient.class);
51 private OutputTypeValidator outputTypeValidator = new OutputTypeValidator();
55 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws ClientConfigurationException {
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));
64 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
65 System.out.println(configuration.toString());
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);
76 void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException {
78 when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
79 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
82 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient, outputTypeValidator).create();
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);
93 void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() {
95 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
98 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
102 assertThatExceptionOfType(ClientConfigurationException.class)
103 .isThrownBy(configurationFactory::create)
104 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
108 void create_shouldReturnClientExceptionWhenCaNameContainsSpecialCharacters() {
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));
116 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
117 outputTypeValidator);
120 assertThatExceptionOfType(ClientConfigurationException.class)
121 .isThrownBy(configurationFactory::create)
122 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
126 void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() {
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));
134 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
135 outputTypeValidator);
138 assertThatExceptionOfType(ClientConfigurationException.class)
139 .isThrownBy(configurationFactory::create)
140 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.");
144 void create_shouldReturnClientExceptionWhenOutputTypeIsInvalid() {
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));
153 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient,
154 outputTypeValidator);
157 assertThatExceptionOfType(ClientConfigurationException.class)
158 .isThrownBy(configurationFactory::create)
159 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_TYPE + " is invalid.");