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
10 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.oom.truststoremerger.certification.file.model;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.onap.oom.truststoremerger.api.CertificateConstants.X_509_CERTIFICATE;
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;
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;
41 void jksTruststoreShouldReadCertificatesFromFile() throws ExitableException {
44 JavaTruststore jksTruststoreFile = TestCertificateProvider.getSampleJksTruststoreFile();
47 List<CertificateWithAlias> certificates = jksTruststoreFile.getCertificates();
48 Certificate certificate = certificates.get(FIRST_ELEMENT).getCertificate();
51 assertThat(certificates).hasSize(EXPECTED_ONE);
52 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
56 void jksTruststoreShouldAddDifferentCertificates() throws Exception {
59 JavaTruststore jksTruststore = TestCertificateProvider.createTmpJksTruststoreFileWithUniqAlias();
60 List<CertificateWithAlias> p12certificates = TestCertificateProvider.getSampleP12Truststore()
62 List<CertificateWithAlias> pemCertificates = TestCertificateProvider.getSamplePemTruststoreFile()
66 jksTruststore.addCertificate(p12certificates);
67 jksTruststore.addCertificate(pemCertificates);
70 assertThat(jksTruststore.getCertificates()).hasSize(EXPECTED_THREE);
75 void p12TruststoreShouldReadCertificatesFromFile() throws ExitableException {
77 JavaTruststore p12Truststore = TestCertificateProvider.getSampleP12Truststore();
80 List<CertificateWithAlias> certificatesWithAliases = p12Truststore.getCertificates();
81 Certificate certificate = certificatesWithAliases.get(FIRST_ELEMENT).getCertificate();
84 assertThat(certificatesWithAliases).hasSize(EXPECTED_ONE);
85 assertThat(certificate.getType()).isEqualTo(X_509_CERTIFICATE);
90 void p12TruststoreShouldAddDifferentCertificates() throws Exception {
92 JavaTruststore p12Truststore = TestCertificateProvider.createTmpP12TruststoreFile();
93 List<CertificateWithAlias> jksTruststoreCertificates = TestCertificateProvider
94 .getSampleJksTruststoreFileWithUniqueAlias()
96 List<CertificateWithAlias> pemTruststoreCertificates = TestCertificateProvider.getSamplePemTruststoreFile()
100 p12Truststore.addCertificate(jksTruststoreCertificates);
101 p12Truststore.addCertificate(pemTruststoreCertificates);
102 p12Truststore.saveFile();
106 JavaTruststore p12TruststoreSaved = TestCertificateProvider.getTmpP12TruststoreFile();
107 assertThat(p12TruststoreSaved.getCertificates()).hasSize(EXPECTED_THREE);
113 static void removeTemporaryFiles() throws IOException {
114 TestCertificateProvider.removeTemporaryFiles();