Fix for env names
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / certservice / postprocessor / merger / model / TruststoreFactoryTest.java
1 /*============LICENSE_START=======================================================
2  * oom-truststore-merger
3  * ================================================================================
4  * Copyright (C) 2020 Nokia. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20
21 package org.onap.oom.certservice.postprocessor.merger.model;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.onap.oom.certservice.postprocessor.merger.exception.KeystoreInstanceException;
30 import org.onap.oom.certservice.postprocessor.merger.exception.LoadTruststoreException;
31 import org.onap.oom.certservice.postprocessor.merger.exception.PasswordReaderException;
32 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreFileFactoryException;
33
34 @ExtendWith(MockitoExtension.class)
35 class TruststoreFactoryTest {
36
37     private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
38     private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
39     private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
40     private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
41     private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
42     private static final String EMPTY_PASS_PATH = "";
43     private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown";
44     private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks";
45
46     @Test
47     void shouldReturnCorrectJksTruststoreForJksFile()
48         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
49         //given, when
50         Truststore truststore = TruststoreFactory
51             .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH);
52
53         //then
54         assertThat(truststore).isInstanceOf(Truststore.class);
55     }
56
57     @Test
58     void shouldReturnCorrectP12TruststoreForP12File()
59         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
60         //given, when
61         Truststore truststore = TruststoreFactory
62             .create(TRUSTSTORE_P12_PATH, TRUSTSTORE_P12_PASS_PATH);
63
64         //then
65         assertThat(truststore).isInstanceOf(Truststore.class);
66     }
67
68     @Test
69     void shouldReturnCorrectPemTruststoreForPemFile()
70         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
71         //given, when
72         Truststore truststore = TruststoreFactory
73             .create(TRUSTSTORE_PEM_PATH,
74                 EMPTY_PASS_PATH);
75
76         //then
77         assertThat(truststore).isInstanceOf(Truststore.class);
78     }
79
80     @Test
81     void shouldThrowExceptionForInvalidP12PassPath() {
82         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
83             () -> TruststoreFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH)
84         );
85     }
86
87     @Test
88     void shouldThrowExceptionForInvalidJksPassPath() {
89         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
90             () -> TruststoreFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH)
91         );
92     }
93
94     @Test
95     void shouldThrowExceptionForUnknownTruststoreExtension() {
96         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
97             () -> TruststoreFactory
98                 .create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH)
99         );
100     }
101
102     @Test
103     void shouldThrowExceptionForNonExistingTruststoreFile() {
104         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
105             () -> TruststoreFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH)
106         );
107     }
108
109 }