Fix for env names
[oom/platform/cert-service.git] / trustStoreMerger / src / test / java / org / onap / oom / truststoremerger / merger / model / PemTruststoreTest.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.merger.model;
21
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;
25
26 import java.io.File;
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.truststoremerger.api.ExitableException;
35 import org.onap.oom.truststoremerger.merger.exception.MissingTruststoreException;
36 import org.onap.oom.truststoremerger.merger.exception.TruststoreDataOperationException;
37 import org.onap.oom.truststoremerger.merger.exception.WriteTruststoreFileException;
38 import org.onap.oom.truststoremerger.merger.model.certificate.CertificateWithAlias;
39
40 class PemTruststoreTest {
41
42     private static final int EXPECTED_ONE = 1;
43
44     @Test
45     void getCertificatesShouldThrowExceptionWhenFileNotContainsCertificate() {
46         //given
47         File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
48         PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
49         //when//then
50         assertThatExceptionOfType(MissingTruststoreException.class)
51             .isThrownBy(pemCertificate::getCertificates);
52     }
53
54     @Test
55     void shouldThrowExceptionWhenCannotSaveFile() {
56         //given
57         KeyStoreSpi keyStoreSpi = mock(KeyStoreSpi.class);
58         KeyStore keyStore = new KeyStore(keyStoreSpi, null, "") {
59         };
60         File pemFile = TestCertificateProvider.getEmptyPemFile();
61         pemFile.setWritable(false);
62         PemTruststore pem = new PemTruststore(pemFile);
63
64         //when. then
65         assertThatExceptionOfType(WriteTruststoreFileException.class)
66             .isThrownBy(pem::saveFile);
67     }
68
69     @Test
70     void transformToStringInPemFormatShouldCorrectlyTransform() throws ExitableException, IOException {
71         //given
72         Truststore pemTruststore = TestCertificateProvider.getSamplePemTruststoreFile();
73
74         List<CertificateWithAlias> wrappedCertificates = pemTruststore.getCertificates();
75         List<Certificate> certificateList = unWrapCertificate(wrappedCertificates);
76         File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
77         PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
78
79         //when
80         String certificateTransformed = pemCertificate.transformToStringInPemFormat(certificateList);
81
82         //then
83         String expected = TestCertificateProvider.getExpectedPemCertificateAsString();
84         assertThat(certificateTransformed).isEqualTo(expected);
85     }
86
87     @Test
88     void fileNotContainsPemCertificateShouldReturnTrueIfFileNotContainsCertificate()
89         throws TruststoreDataOperationException {
90         //given
91         File emptyPemFile = TestCertificateProvider.getEmptyPemFile();
92         PemTruststore pemCertificate = new PemTruststore(emptyPemFile);
93         //when//then
94         assertThat(pemCertificate.isFileWithoutPemCertificate()).isTrue();
95     }
96
97     @Test
98     void fileNotContainsPemCertificateShouldReturnFalseIfFileContainsCertificate()
99         throws TruststoreDataOperationException {
100         //given
101         File notEmptyPemFile = TestCertificateProvider.getNotEmptyPemFile();
102         PemTruststore pemCertificate = new PemTruststore(notEmptyPemFile);
103
104         //when//then
105         assertThat(pemCertificate.isFileWithoutPemCertificate()).isFalse();
106     }
107
108     @Test
109     void privateKeyIsSkippedWhileReadingCertificates() throws ExitableException {
110         //given
111         File pemTruststoreFile = TestCertificateProvider.getPemWithPrivateKeyFile();
112         PemTruststore pemCertificate = new PemTruststore(pemTruststoreFile);
113
114         //when
115         List<CertificateWithAlias> certificate = pemCertificate.getCertificates();
116
117         //then
118         assertThat(certificate).hasSize(EXPECTED_ONE);
119     }
120
121     private List<Certificate> unWrapCertificate(List<CertificateWithAlias> certificateWithAliases) {
122         return certificateWithAliases
123             .stream()
124             .map(CertificateWithAlias::getCertificate)
125             .collect(Collectors.toList());
126     }
127
128 }