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.CsrConfigurationEnvs;
25 import org.onap.aaf.certservice.client.configuration.EnvsForCsr;
26 import org.onap.aaf.certservice.client.configuration.exception.CsrConfigurationException;
27 import org.onap.aaf.certservice.client.configuration.factory.CsrConfigurationFactory;
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 CsrConfigurationFactoryTest {
36 final String COMMON_NAME_VALID = "onap.org";
37 final String SANS_VALID = "test-name";
38 final String COUNTRY_VALID = "US";
39 final String LOCATION_VALID = "San-Francisco";
40 final String ORGANIZATION_VALID = "Linux-Foundation";
41 final String ORGANIZATION_UNIT_VALID = "ONAP";
42 final String STATE_VALID = "California";
43 final String COMMON_NAME_INVALID = "onap.org*&";
45 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
49 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
51 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
52 when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID);
53 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
54 when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID);
55 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
56 when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID);
57 when(envsForCsr.getState()).thenReturn(STATE_VALID);
60 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
63 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
64 assertThat(configuration.getSubjectAlternativeNames()).isEqualTo(SANS_VALID);
65 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
66 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
67 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
68 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
69 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
73 void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
75 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
76 when(envsForCsr.getState()).thenReturn(STATE_VALID);
77 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
78 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
81 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
84 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
85 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
86 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
87 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
92 void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
94 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_INVALID);
95 when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID);
96 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
97 when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID);
98 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
99 when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID);
100 when(envsForCsr.getState()).thenReturn(SANS_VALID);
103 CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
106 assertThatExceptionOfType(CsrConfigurationException.class)
107 .isThrownBy(configurationFactory::create)
108 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");