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
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.aaf.certservice.client.configuration.model;
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;
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;
34 public class ClientConfigurationFactoryTest {
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";
43 private EnvsForClient envsForClient = mock(EnvsForClient.class);
46 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() {
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);
54 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
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);
64 void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() {
66 when(envsForClient.getCaName()).thenReturn(CA_NAME_VALID);
67 when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
70 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
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);
80 void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() {
82 when(envsForClient.getOutputPath()).thenReturn(OUTPUT_PATH_VALID);
85 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
88 assertThatExceptionOfType(ClientConfigurationException.class)
89 .isThrownBy(configurationFactory::create)
90 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
94 void create_shouldReturnClientExceptionWhenCANameContainsSpecialCharacters() {
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);
102 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
105 assertThatExceptionOfType(ClientConfigurationException.class)
106 .isThrownBy(configurationFactory::create)
107 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
111 void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() {
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);
119 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
122 assertThatExceptionOfType(ClientConfigurationException.class)
123 .isThrownBy(configurationFactory::create)
124 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.");