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;
 
  22 import org.junit.jupiter.api.BeforeEach;
 
  23 import org.junit.jupiter.api.Test;
 
  24 import org.junit.jupiter.api.extension.ExtendWith;
 
  25 import org.mockito.Mock;
 
  26 import org.mockito.junit.jupiter.MockitoExtension;
 
  28 import java.util.Optional;
 
  30 import static org.assertj.core.api.Assertions.assertThat;
 
  31 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 
  32 import static org.mockito.Mockito.when;
 
  33 import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_ENV;
 
  34 import static org.onap.oom.truststoremerger.api.ConfigurationEnvs.TRUSTSTORES_PASSWORDS_ENV;
 
  37 @ExtendWith(MockitoExtension.class)
 
  38 class TruststoresPathsProviderTest {
 
  40     private static final String VALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.pem";
 
  41     private static final String VALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:";
 
  42     private static final String INVALID_TRUSTSTORES = "/opt/app/certificates/truststore.jks:/opt/app/certificates/truststore.invalid";
 
  43     private static final String INVALID_TRUSTSTORES_PASSWORDS = "/opt/app/certificates/truststore.pass:/.pass";
 
  46     private EnvProvider envProvider;
 
  47     private TruststoresPathsProvider truststoresPathsProvider;
 
  51         truststoresPathsProvider = new TruststoresPathsProvider(envProvider, new PathValidator());
 
  55     void shouldReturnCorrectListWhenTruststoresValid() throws TruststoresPathsProviderException {
 
  56         mockTruststoresEnv(VALID_TRUSTSTORES);
 
  58         assertThat(truststoresPathsProvider.getTruststores())
 
  59                 .contains("/opt/app/certificates/truststore.jks",
 
  60                         "/opt/app/certificates/truststore.pem");
 
  64     void shouldReturnCorrectListWhenTruststoresPasswordsValid() throws TruststoresPathsProviderException {
 
  65         mockTruststoresPasswordsEnv(VALID_TRUSTSTORES_PASSWORDS);
 
  67         assertThat(truststoresPathsProvider.getTruststoresPasswords())
 
  68                 .contains("/opt/app/certificates/truststore.pass",
 
  73     void shouldThrowExceptionWhenTruststoresEmpty() {
 
  74         mockTruststoresEnv("");
 
  76         assertThatExceptionOfType(TruststoresPathsProviderException.class)
 
  77                 .isThrownBy(truststoresPathsProvider::getTruststores);
 
  81     void shouldThrowExceptionWhenOneOfTruststoresPathsInvalid() {
 
  82         mockTruststoresEnv(INVALID_TRUSTSTORES);
 
  84         assertThatExceptionOfType(TruststoresPathsProviderException.class)
 
  85                 .isThrownBy(truststoresPathsProvider::getTruststores);
 
  89     void shouldThrowExceptionWhenOneOfTruststorePasswordPathsInvalid() {
 
  90         mockTruststoresPasswordsEnv(INVALID_TRUSTSTORES_PASSWORDS);
 
  92         assertThatExceptionOfType(TruststoresPathsProviderException.class)
 
  93                 .isThrownBy(truststoresPathsProvider::getTruststoresPasswords);
 
  96     private void mockTruststoresEnv(String truststores) {
 
  97         mockEnv(truststores, TRUSTSTORES_ENV);
 
 100     private void mockTruststoresPasswordsEnv(String truststoresPasswords) {
 
 101         mockEnv(truststoresPasswords, TRUSTSTORES_PASSWORDS_ENV);
 
 104     private void mockEnv(String envValue, String envName) {
 
 105         when(envProvider.getEnv(envName))
 
 106                 .thenReturn(Optional.of(envValue));