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 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 CsrConfigurationFactoryTest {
38 private final String COMMON_NAME_VALID = "onap.org";
39 private final String SANS_VALID = "test-name";
40 private final String COUNTRY_VALID = "US";
41 private final String LOCATION_VALID = "San-Francisco";
42 private final String ORGANIZATION_VALID = "Linux-Foundation";
43 private final String ORGANIZATION_UNIT_VALID = "ONAP";
44 private final String STATE_VALID = "California";
45 private final String COMMON_NAME_INVALID = "onap.org*&";
47 private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
51 void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
53 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
54 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
55 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
56 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
57 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
58 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
59 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
62 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
65 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
66 assertThat(configuration.getSans()).isEqualTo(SANS_VALID);
67 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
68 assertThat(configuration.getLocation()).isEqualTo(LOCATION_VALID);
69 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
70 assertThat(configuration.getOrganizationUnit()).isEqualTo(ORGANIZATION_UNIT_VALID);
71 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
75 void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
77 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_VALID));
78 when(envsForCsr.getState()).thenReturn(Optional.of(STATE_VALID));
79 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
80 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
83 CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
86 assertThat(configuration.getCommonName()).isEqualTo(COMMON_NAME_VALID);
87 assertThat(configuration.getCountry()).isEqualTo(COUNTRY_VALID);
88 assertThat(configuration.getOrganization()).isEqualTo(ORGANIZATION_VALID);
89 assertThat(configuration.getState()).isEqualTo(STATE_VALID);
94 void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
96 when(envsForCsr.getCommonName()).thenReturn(Optional.of(COMMON_NAME_INVALID));
97 when(envsForCsr.getSubjectAlternativesName()).thenReturn(Optional.of(SANS_VALID));
98 when(envsForCsr.getCountry()).thenReturn(Optional.of(COUNTRY_VALID));
99 when(envsForCsr.getLocation()).thenReturn(Optional.of(LOCATION_VALID));
100 when(envsForCsr.getOrganization()).thenReturn(Optional.of(ORGANIZATION_VALID));
101 when(envsForCsr.getOrganizationUnit()).thenReturn(Optional.of(ORGANIZATION_UNIT_VALID));
102 when(envsForCsr.getState()).thenReturn(Optional.of(SANS_VALID));
105 CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
108 assertThatExceptionOfType(CsrConfigurationException.class)
109 .isThrownBy(configurationFactory::create)
110 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");