39d4459216d221a9c163a259e08f6491d3490893
[oom/platform/cert-service.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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.model;
22
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;
27
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;
32
33 public class CsrConfigurationFactoryTest {
34
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*&";
43
44     private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
45
46
47     @Test
48     void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() {
49         // given
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);
57
58         // when
59         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
60
61         // then
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);
69     }
70
71     @Test
72     void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() {
73         // given
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);
78
79         // when
80         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
81
82         // then
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);
87     }
88
89
90     @Test
91     void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
92         // given
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);
100
101         // when
102         CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
103
104         // when/then
105         assertThatExceptionOfType(CsrConfigurationException.class)
106                 .isThrownBy(configurationFactory::create)
107                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");
108     }
109 }