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.certservice.postprocessor.merger.model;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24 import static org.mockito.Mockito.mock;
27 import java.io.IOException;
28 import java.security.KeyStore;
29 import java.security.KeyStoreSpi;
30 import java.security.cert.Certificate;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import org.junit.jupiter.api.Test;
34 import org.onap.oom.certservice.postprocessor.api.ExitableException;
35 import org.onap.oom.certservice.postprocessor.merger.exception.MissingTruststoreException;
36 import org.onap.oom.certservice.postprocessor.merger.exception.TruststoreDataOperationException;
37 import org.onap.oom.certservice.postprocessor.merger.exception.WriteTruststoreFileException;
38 import org.onap.oom.certservice.postprocessor.merger.model.certificate.CertificateWithAlias;
40 class PemTruststoreTest {
42 private static final int EXPECTED_ONE = 1;
45 void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() {
47 File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
48 PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
50 assertThatExceptionOfType(MissingTruststoreException.class)
51 .isThrownBy(pemCertificate::getCertificates);
55 void shouldThrowExceptionWhenCannotSaveFile() {
57 KeyStoreSpi keyStoreSpi = mock(KeyStoreSpi.class);
58 KeyStore keyStore = new KeyStore(keyStoreSpi, null, "") {
60 File pemFile = TestCertificateProvider.getEmptyPemFile();
61 pemFile.setWritable(false);
62 PemTruststore pem = new PemTruststore(pemFile);
65 assertThatExceptionOfType(WriteTruststoreFileException.class)
66 .isThrownBy(pem::saveFile);
70 void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException {
72 Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile();
74 List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates();
75 List<Certificate> certificateList = unWrapCertificate(wrappedCertificates);
76 File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
77 PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
80 String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList);
83 String expected = TestCertificateProvider.getExpectedPemCertificateAsString();
84 assertThat(certificateTransformed).isEqualTo(expected);
88 void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate()
89 throws TruststoreDataOperationException {
91 File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
92 PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
94 assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue();
98 void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate()
99 throws TruststoreDataOperationException {
101 File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
102 PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
105 assertThat(pemCertificate.isFileWithoutPemCertificate()).isFalse();
109 void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException {
111 File pemTruststoreFile = TestCertificateProvider.getPemWithPrivateKeyFile();
112 PemTruststore pemCertificate = new PemTruststore(pemTruststoreFile);
115 List<CertificateWithAlias> certificate = pemCertificate.getCertificates();
118 assertThat(certificate).hasSize(EXPECTED_ONE);
121 private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) {
122 return certificateWithAliases
124 .map(CertificateWithAlias::getCertificate)
125 .collect(Collectors.toList());