2 * ============LICENSE_START=======================================================
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.model;
23 import org.junit.jupiter.api.Test;
24 import org.onap.aaf.certservice.client.common.CsrConfigurationEnvs;
25 import org.onap.aaf.certservice.client.common.EnvsForCsr;
26 import org.onap.aaf.certservice.client.exceptions.CsrConfigurationException;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 public class CsrConfigurationFactoryTest {
35 final String COMMON_NAME_VALID = "onap.org";
36 final String SANS_VALID = "test-name";
37 final String COUNTRY_VALID = "US";
38 final String LOCATION_VALID = "San-Francisco";
39 final String ORGANIZATION_VALID = "Linux-Foundation";
40 final String ORGANIZATION_UNIT_VALID = "ONAP";
41 final String STATE_VALID = "California";
42 final String COMMON_NAME_INVALID = "onap.org*&";
44 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
48 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() {
50 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
51 when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID);
52 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
53 when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID);
54 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
55 when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID);
56 when(envsForCsr.getState()).thenReturn(STATE_VALID);
59 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
62 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
63 assertThat(configuration.getSubjectAlternativeNames()).isEqualTo(SANS_VALID);
64 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
65 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
66 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
67 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
68 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
72 void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() {
74 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_VALID);
75 when(envsForCsr.getState()).thenReturn(STATE_VALID);
76 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
77 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
80 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
83 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
84 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
85 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
86 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
91 void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
93 when(envsForCsr.getCommonName()).thenReturn(COMMON_NAME_INVALID);
94 when(envsForCsr.getSubjectAlternativesName()).thenReturn(SANS_VALID);
95 when(envsForCsr.getCountry()).thenReturn(COUNTRY_VALID);
96 when(envsForCsr.getLocation()).thenReturn(LOCATION_VALID);
97 when(envsForCsr.getOrganization()).thenReturn(ORGANIZATION_VALID);
98 when(envsForCsr.getOrganizationUnit()).thenReturn(ORGANIZATION_UNIT_VALID);
99 when(envsForCsr.getState()).thenReturn(SANS_VALID);
102 CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
105 assertThatExceptionOfType(CsrConfigurationException.class)
106 .isThrownBy(configurationFactory::create)
107 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");