Merge "Add TruststoreFile provider"
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / provider / 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.truststoremerger.certification.file.provider;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.onap.oom.truststoremerger.certification.file.TruststoreFileFactory;
28 import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore;
29 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
30 import org.onap.oom.truststoremerger.certification.file.model.Truststore;
31 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
32 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
33
34 import java.io.File;
35 import org.onap.oom.truststoremerger.certification.file.exception.PasswordReaderException;
36 import org.onap.oom.truststoremerger.certification.file.exception.TruststoreFileFactoryException;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
40
41 @ExtendWith(MockitoExtension.class)
42 class TruststoreFactoryTest {
43
44     private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
45     private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
46     private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
47     private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
48     private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
49     private static final String EMPTY_PASS_PATH = "";
50     private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown";
51     private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks";
52
53     private TruststoreFileFactory truststoreFileFactory;
54
55     @BeforeEach
56     void setUp() {
57         truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader());
58     }
59
60     @Test
61     void shouldReturnCorrectJksTruststoreForJksFile()
62         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
63         Truststore truststore = truststoreFileFactory
64                 .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH);
65         assertThat(truststore).isInstanceOf(JavaTruststore.class);
66         JavaTruststore jksTruststore = (JavaTruststore) truststore;
67         assertThat(jksTruststore.getFile()).isEqualTo(new File(TRUSTSTORE_JKS_PATH));
68     }
69
70     @Test
71     void shouldReturnCorrectP12TruststoreForP12File()
72         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
73         Truststore truststore = truststoreFileFactory
74                 .create(TRUSTSTORE_P12_PATH,
75                         TRUSTSTORE_P12_PASS_PATH);
76         assertThat(truststore).isInstanceOf(JavaTruststore.class);
77     }
78
79     @Test
80     void shouldReturnCorrectPemTruststoreForPemFile()
81         throws LoadTruststoreException, PasswordReaderException, TruststoreFileFactoryException, KeystoreInstanceException {
82         Truststore truststore = truststoreFileFactory
83                 .create(TRUSTSTORE_PEM_PATH,
84                         EMPTY_PASS_PATH);
85         assertThat(truststore).isInstanceOf(PemTruststore.class);
86     }
87
88     @Test
89     void shouldThrowExceptionForInvalidP12PassPath() {
90         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
91                 () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH)
92         );
93     }
94
95     @Test
96     void shouldThrowExceptionForInvalidJksPassPath() {
97         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
98                 () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH)
99         );
100     }
101
102     @Test
103     void shouldThrowExceptionForUnknownTruststoreExtension() {
104         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
105                 () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH)
106         );
107     }
108
109     @Test
110     void shouldThrowExceptionForNonExistingTruststoreFile() {
111         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
112                 () -> truststoreFileFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH)
113         );
114     }
115
116 }