Add Certification merge logic
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / model / JavaTruststoreTest.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 package org.onap.oom.truststoremerger.certification.file.model;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE;
24
25 import java.io.IOException;
26 import java.security.cert.Certificate;
27 import java.util.List;
28 import org.junit.jupiter.api.AfterAll;
29 import org.junit.jupiter.api.Test;
30 import org.onap.oom.truststoremerger.api.ExitableException;
31 import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
32 import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider;
33
34
35 class JavaTruststoreTest {
36     public static final int FIRST_ELEMENT = 0;
37     private static final int EXPECTED_ONE = 1;
38     public static final int EXPECTED_THREE = 3;
39
40     @Test
41     void jksTruststoreShouldReadCertificatesFromFile() throws ExitableException {
42
43         //given
44         JavaTruststore jksTruststoreFile = TestCertificateProvider.getSampleJksTruststoreFile();
45
46         //when
47         List<CertificateWithAlias> certificates = jksTruststoreFile.getCertificates();
48         Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
49
50         //then
51         assertThat(certificates).hasSize(EXPECTED_ONE);
52         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
53     }
54
55     @Test
56     void jksTruststoreShouldAddDifferentCertificates() throws Exception {
57
58         //given
59         JavaTruststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
60         List<CertificateWithAlias> p12certificates = TestCertificateProvider.getSampleP12Truststore()
61             .getCertificates();
62         List<CertificateWithAlias> pemCertificates = TestCertificateProvider.getSamplePemTruststoreFile()
63             .getCertificates();
64
65         //when
66         jksTruststore.addCertificate(p12certificates);
67         jksTruststore.addCertificate(pemCertificates);
68
69         //then
70         assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
71
72     }
73
74     @Test
75     void p12TruststoreShouldReadCertificatesFromFile() throws ExitableException {
76         //given
77         JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore();
78
79         //when
80         List<CertificateWithAlias> certificatesWithAliases = p12Truststore.getCertificates();
81         Certificate certificate = certificatesWithAliases.get(FIRST_ELEMENT).getCertificate();
82
83         //then
84         assertThat(certificatesWithAliases).hasSize(EXPECTED_ONE);
85         assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
86     }
87
88
89     @Test
90     void p12TruststoreShouldAddDifferentCertificates() throws Exception {
91         //given
92         JavaTruststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
93         List<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider
94             .getSampleJksTruststoreFileWithUniqueAlias()
95             .getCertificates();
96         List<CertificateWithAlias> pemTruststoreCertificates = TestCertificateProvider.getSamplePemTruststoreFile()
97             .getCertificates();
98
99         //when
100         p12Truststore.addCertificate(jksTruststoreCertificates);
101         p12Truststore.addCertificate(pemTruststoreCertificates);
102         p12Truststore.saveFile();
103
104
105         //then
106         JavaTruststore p12TruststoreSaved = TestCertificateProvider.getTmpP12TruststoreFile();
107         assertThat(p12TruststoreSaved.getCertificates()).hasSize(EXPECTED_THREE);
108     }
109
110
111
112     @AfterAll
113     static void removeTemporaryFiles() throws IOException {
114         TestCertificateProvider.removeTemporaryFiles();
115     }
116 }