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 java.util.Optional;
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;
36 public class ClientConfigurationFactoryTest {
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 = "https://cert-service:8443/v1/certificate/";
42 private final String URL_TO_CERT_SERVICE_DEFAULT = "https://aaf-cert-service:8443/v1/certificate/";
43 private final String CA_NAME_INVALID = "caaaftest2#$";
44 private final String OUTPUT_PATH_INVALID = "/opt//app/osaaf";
46 private EnvsForClient envsForClient = mock(EnvsForClient.class);
49 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws ClientConfigurationException {
51 when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
52 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
53 when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
54 when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
57 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
60 assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
61 assertThat(configuration.getRequestTimeout()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
62 assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
63 assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_VALID);
67 void create_shouldReturnSuccessWhenDefaultVariablesAreNotSet() throws ClientConfigurationException {
69 when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
70 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
73 ClientConfiguration configuration = new ClientConfigurationFactory(envsForClient).create();
76 assertThat(configuration.getCaName()).isEqualTo(CA_NAME_VALID);
77 assertThat(configuration.getRequestTimeout()).isEqualTo(Integer.valueOf(TIME_OUT_VALID));
78 assertThat(configuration.getCertsOutputPath()).isEqualTo(OUTPUT_PATH_VALID);
79 assertThat(configuration.getUrlToCertService()).isEqualTo(URL_TO_CERT_SERVICE_DEFAULT);
83 void create_shouldReturnClientExceptionWhenRequiredVariableIsNotSet() {
85 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
88 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
91 assertThatExceptionOfType(ClientConfigurationException.class)
92 .isThrownBy(configurationFactory::create)
93 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
97 void create_shouldReturnClientExceptionWhenCANameContainsSpecialCharacters() {
99 when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_INVALID));
100 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_VALID));
101 when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
102 when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
105 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
108 assertThatExceptionOfType(ClientConfigurationException.class)
109 .isThrownBy(configurationFactory::create)
110 .withMessageContaining(ClientConfigurationEnvs.CA_NAME + " is invalid.");
114 void create_shouldReturnClientExceptionWhenOutputPathContainsSpecialCharacters() {
116 when(envsForClient.getCaName()).thenReturn(Optional.of(CA_NAME_VALID));
117 when(envsForClient.getOutputPath()).thenReturn(Optional.of(OUTPUT_PATH_INVALID));
118 when(envsForClient.getRequestTimeOut()).thenReturn(Optional.of(TIME_OUT_VALID));
119 when(envsForClient.getUrlToCertService()).thenReturn(Optional.of(URL_TO_CERT_SERVICE_VALID));
122 ClientConfigurationFactory configurationFactory = new ClientConfigurationFactory(envsForClient);
125 assertThatExceptionOfType(ClientConfigurationException.class)
126 .isThrownBy(configurationFactory::create)
127 .withMessageContaining(ClientConfigurationEnvs.OUTPUT_PATH + " is invalid.");