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=========================================================
 
  21 package org.onap.oom.truststoremerger.certification.file.provider;
 
  23 import org.junit.jupiter.api.BeforeEach;
 
  24 import org.junit.jupiter.api.Test;
 
  25 import org.junit.jupiter.api.extension.ExtendWith;
 
  26 import org.mockito.junit.jupiter.MockitoExtension;
 
  27 import org.onap.oom.truststoremerger.certification.file.JksTruststore;
 
  28 import org.onap.oom.truststoremerger.certification.file.P12Truststore;
 
  29 import org.onap.oom.truststoremerger.certification.file.PemTruststore;
 
  30 import org.onap.oom.truststoremerger.certification.file.TruststoreFile;
 
  34 import static org.assertj.core.api.Assertions.assertThat;
 
  35 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 
  37 @ExtendWith(MockitoExtension.class)
 
  38 class TruststoreFileFactoryTest {
 
  40     private static final String TRUSTSTORE_JKS_PATH = "src/test/resources/truststore-jks.jks";
 
  41     private static final String TRUSTSTORE_JKS_PASS_PATH = "src/test/resources/truststore-jks.pass";
 
  42     private static final String TRUSTSTORE_JKS_PASS = "EOyuFbuYDyq_EhpboM72RHua";
 
  43     private static final String TRUSTSTORE_P12_PATH = "src/test/resources/truststore-p12.p12";
 
  44     private static final String TRUSTSTORE_P12_PASS_PATH = "src/test/resources/truststore-p12.pass";
 
  45     private static final String TRUSTSTORE_P12_PASS = "88y9v5D8H3SG6bZWRVHDfOAo";
 
  46     private static final String TRUSTSTORE_PEM_PATH = "src/test/resources/truststore.pem";
 
  47     private static final String EMPTY_PASS_PATH = "";
 
  48     private static final String TRUSTSTORE_UNKNOWN_EXTENSION_PATH = "src/test/resources/truststore-jks.unknown";
 
  49     private static final String NON_EXISTING_TRUSTSTORE_PATH = "src/test/resources/non-existing-truststore.jks";
 
  51     private TruststoreFileFactory truststoreFileFactory;
 
  55         truststoreFileFactory = new TruststoreFileFactory(new FileManager(), new PasswordReader());
 
  59     void shouldReturnCorrectJksTruststoreForJksFile() throws TruststoreFileFactoryException, PasswordReaderException {
 
  60         TruststoreFile truststore = truststoreFileFactory
 
  61                 .create(TRUSTSTORE_JKS_PATH, TRUSTSTORE_JKS_PASS_PATH);
 
  62         assertThat(truststore).isInstanceOf(JksTruststore.class);
 
  63         JksTruststore jksTruststore = (JksTruststore) truststore;
 
  64         assertThat(jksTruststore.getPassword()).isEqualTo(TRUSTSTORE_JKS_PASS);
 
  65         assertThat(jksTruststore.getTruststoreFile()).isEqualTo(new File(TRUSTSTORE_JKS_PATH));
 
  69     void shouldReturnCorrectP12TruststoreForP12File() throws TruststoreFileFactoryException, PasswordReaderException {
 
  70         TruststoreFile truststore = truststoreFileFactory
 
  71                 .create(TRUSTSTORE_P12_PATH,
 
  72                         TRUSTSTORE_P12_PASS_PATH);
 
  73         assertThat(truststore).isInstanceOf(P12Truststore.class);
 
  74         P12Truststore jksTruststore = (P12Truststore) truststore;
 
  75         assertThat(jksTruststore.getPassword()).isEqualTo(TRUSTSTORE_P12_PASS);
 
  79     void shouldReturnCorrectPemTruststoreForPemFile() throws TruststoreFileFactoryException, PasswordReaderException {
 
  80         TruststoreFile truststore = truststoreFileFactory
 
  81                 .create(TRUSTSTORE_PEM_PATH,
 
  83         assertThat(truststore).isInstanceOf(PemTruststore.class);
 
  87     void shouldThrowExceptionForInvalidP12PassPath() {
 
  88         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
 
  89                 () -> truststoreFileFactory.create(TRUSTSTORE_P12_PATH, EMPTY_PASS_PATH)
 
  94     void shouldThrowExceptionForInvalidJksPassPath() {
 
  95         assertThatExceptionOfType(PasswordReaderException.class).isThrownBy(
 
  96                 () -> truststoreFileFactory.create(TRUSTSTORE_JKS_PATH, EMPTY_PASS_PATH)
 
 101     void shouldThrowExceptionForUnknownTruststoreExtension() {
 
 102         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
 
 103                 () -> truststoreFileFactory.create(TRUSTSTORE_UNKNOWN_EXTENSION_PATH, TRUSTSTORE_JKS_PASS_PATH)
 
 108     void shouldThrowExceptionForNonExistingTruststoreFile() {
 
 109         assertThatExceptionOfType(TruststoreFileFactoryException.class).isThrownBy(
 
 110                 () -> truststoreFileFactory.create(NON_EXISTING_TRUSTSTORE_PATH, TRUSTSTORE_JKS_PASS_PATH)