d6bf431ba119831c0ba5f418490364f0916fb1b2
[oom/platform/cert-service.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aaf.certservice.client.configuration.model;
22
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;
28
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;
33
34 public class CsrConfigurationFactoryTest {
35
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*&";
44
45     private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
46
47
48     @Test
49     void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
50         // given
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);
58
59         // when
60         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
61
62         // then
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);
70     }
71
72     @Test
73     void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
74         // given
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);
79
80         // when
81         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
82
83         // then
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);
88     }
89
90
91     @Test
92     void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
93         // given
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);
101
102         // when
103         CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
104
105         // when/then
106         assertThatExceptionOfType(CsrConfigurationException.class)
107                 .isThrownBy(configurationFactory::create)
108                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");
109     }
110 }