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.provider;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import java.io.IOException;
27 import java.security.cert.Certificate;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import org.junit.jupiter.api.Test;
31 import org.onap.oom.truststoremerger.api.ExitableException;
32 import org.onap.oom.truststoremerger.certification.file.provider.entry.CertificateWithAlias;
33 import org.onap.oom.truststoremerger.certification.file.TestCertificateProvider;
34 import org.onap.oom.truststoremerger.certification.file.exception.MissingTruststoreException;
35 import org.onap.oom.truststoremerger.certification.file.exception.TruststoreDataOperationException;
36 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
38 class PemCertificateControllerTest {
41 void getNotEmptyCertificateListShouldThrowExceptionWhenFileNotContainsCertificate() {
43 File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile();
44 PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile);
46 assertThatExceptionOfType(MissingTruststoreException.class)
47 .isThrownBy(pemCertificateController::getNotEmptyCertificateList);
51 void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException {
53 PemTruststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile();
54 List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates();
55 File notEmptyPemFile = pemTruststore.getFile();
56 List<Certificate> certificateList = unWrapCertificate(wrappedCertificates);
57 PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile);
58 String expected = TestCertificateProvider.getExpectedPemCertificateAsString();
61 String certificateTransformed = pemCertificateController.transformToStringInPemFormat(certificateList);
64 assertThat(certificateTransformed).isEqualTo(expected);
68 void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate()
69 throws TruststoreDataOperationException {
71 File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile();
72 PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile);
74 assertThat(pemCertificateController.isFileWithoutPemCertificate()).isTrue();
78 void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate()
79 throws TruststoreDataOperationException {
81 File notEmptyPemFile = TestCertificateProvider.getSamplePemTruststoreFile().getFile();
82 PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile);
85 assertThat(pemCertificateController.isFileWithoutPemCertificate()).isFalse();
88 private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) {
89 return certificateWithAliases
91 .map(CertificateWithAlias::getCertificate)
92 .collect(Collectors.toList());