Merge "Add of config for copier"
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / certification / file / provider / PemCertificateControllerTest.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.provider;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24
25 import java.io.File;
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;
37
38 class PemCertificateControllerTest {
39
40     @Test
41     void getNotEmptyCertificateListShouldThrowExceptionWhenFileNotContainsCertificate() {
42         //given
43         File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile();
44         PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile);
45         //when//then
46         assertThatExceptionOfType(MissingTruststoreException.class)
47             .isThrownBy(pemCertificateController::getNotEmptyCertificateList);
48     }
49
50     @Test
51     void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException {
52         //given
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();
59
60         //when
61         String certificateTransformed = pemCertificateController.transformToStringInPemFormat(certificateList);
62
63         //then
64         assertThat(certificateTransformed).isEqualTo(expected);
65     }
66
67     @Test
68     void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate()
69         throws TruststoreDataOperationException {
70         //given
71         File emptyPemFile = TestCertificateProvider.getEmptyPemTruststoreFile().getFile();
72         PemCertificateController pemCertificateController = new PemCertificateController(emptyPemFile);
73         //when//then
74         assertThat(pemCertificateController.isFileWithoutPemCertificate()).isTrue();
75     }
76
77     @Test
78     void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate()
79         throws TruststoreDataOperationException {
80         //given
81         File notEmptyPemFile = TestCertificateProvider.getSamplePemTruststoreFile().getFile();
82         PemCertificateController pemCertificateController = new PemCertificateController(notEmptyPemFile);
83
84         //when//then
85         assertThat(pemCertificateController.isFileWithoutPemCertificate()).isFalse();
86     }
87
88     private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) {
89         return certificateWithAliases
90             .stream()
91             .map(CertificateWithAlias::getCertificate)
92             .collect(Collectors.toList());
93     }
94
95 }