707094c0fdb3852cb3ee627a28e64d895398f87b
[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 java.util.Optional;
30
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;
35
36 public class CsrConfigurationFactoryTest {
37
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*&";
46
47     private EnvsForCsr envsForCsr = mock(EnvsForCsr.class);
48
49
50     @Test
51     void create_shouldReturnSuccessWhenAllVariablesAreSetAndValid() throws CsrConfigurationException {
52         // given
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));
60
61         // when
62         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
63
64         // then
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);
72     }
73
74     @Test
75     void create_shouldReturnSuccessWhenNotRequiredVariablesAreNotSet() throws CsrConfigurationException {
76         // given
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));
81
82         // when
83         CsrConfiguration configuration = new CsrConfigurationFactory(envsForCsr).create();
84
85         // then
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);
90     }
91
92
93     @Test
94     void create_shouldReturnCsrConfigurationExceptionWhenCommonNameContainsSpecialCharacters() {
95         // given
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));
103
104         // when
105         CsrConfigurationFactory configurationFactory = new CsrConfigurationFactory(envsForCsr);
106
107         // when/then
108         assertThatExceptionOfType(CsrConfigurationException.class)
109                 .isThrownBy(configurationFactory::create)
110                 .withMessageContaining(CsrConfigurationEnvs.COMMON_NAME + " is invalid.");
111     }
112 }