Allow full-path to certificate and remove implicit Optional::toString 96/91796/1
authorIttay Stern <ittay.stern@att.com>
Mon, 22 Jul 2019 08:21:05 +0000 (11:21 +0300)
committerIttay Stern <ittay.stern@att.com>
Mon, 22 Jul 2019 08:36:00 +0000 (11:36 +0300)
1) If getAAITruststoreFilename or getAAIKeystoreFilename has a file
   separator -- don't append certFilePath to filename.

2) getKeystorePath() is issuing Optional::orElse, as the code
   `+ getAAIKeystoreFilename()` is implicitly calling
   `getAAIKeystoreFilename().toString()` which yields a default toString
   e.g. "Optional[configValue]".

Issue-ID: VID-229

Change-Id: I9c135cc3dfd72cdc203b59d78162a568a6dbd688
Signed-off-by: Ittay Stern <ittay.stern@att.com>
vid-app-common/src/main/java/org/onap/vid/aai/util/HttpsAuthClient.java
vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java

index e2a6d0e..af181eb 100644 (file)
@@ -116,13 +116,13 @@ public class HttpsAuthClient {
         return new NoopHostnameVerifier();
     }
 
-    private String getKeystorePath() {
-        return getCertificatesPath() + FileSystems.getDefault().getSeparator() + systemPropertyHelper.getAAIKeystoreFilename();
+    protected String getKeystorePath() {
+        return systemPropertyHelper.getAAIKeystoreFilename().map(this::getCertificatesPathOf).orElse("");
     }
 
     private void setSystemProperties() {
-        System.setProperty(SSL_TRUST_STORE, getCertificatesPath() + FileSystems.getDefault().getSeparator() +
-                systemPropertyHelper.getAAITruststoreFilename().orElse(""));
+        System.setProperty(SSL_TRUST_STORE,
+            systemPropertyHelper.getAAITruststoreFilename().map(this::getCertificatesPathOf).orElse(""));
         System.setProperty(SSL_TRUST_STORE_PASS_WORD, systemPropertyHelper.getDecryptedTruststorePassword());
     }
 
@@ -135,8 +135,11 @@ public class HttpsAuthClient {
         return config;
     }
 
-    private String getCertificatesPath() {
-        return certFilePath;
+    private String getCertificatesPathOf(String fileName) {
+        if (fileName.contains("/") || fileName.contains("\\")) {
+            return fileName;
+        }
+        return certFilePath + FileSystems.getDefault().getSeparator() + fileName;
     }
 
 }
index 3336a8a..b48efd6 100644 (file)
@@ -22,6 +22,7 @@
 package org.onap.vid.aai.util;
 
 
+import java.nio.file.FileSystems;
 import org.mockito.Mock;
 
 import org.onap.vid.aai.exceptions.HttpClientBuilderException;
@@ -33,6 +34,7 @@ import org.togglz.core.manager.FeatureManager;
 import javax.net.ssl.SSLContext;
 import java.util.Optional;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.*;
 import static org.mockito.MockitoAnnotations.initMocks;
@@ -55,6 +57,7 @@ public class HttpsAuthClientTest {
     public void setUp() throws Exception {
         initMocks(this);
         when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename"));
+        when(systemPropertyHelper.getAAIKeystoreFilename()).thenReturn(Optional.of("keystorefilename"));
         when(systemPropertyHelper.getDecryptedKeystorePassword()).thenReturn("password");
         when(systemPropertyHelper.getDecryptedTruststorePassword()).thenReturn("password");
     }
@@ -78,6 +81,34 @@ public class HttpsAuthClientTest {
         verify(sslContextProvider).getSslContext(anyString(), anyString(), any());
     }
 
+    @Test
+    public void getKeystorePath_whenNotConfigured_yieldEmptyString() {
+        // when
+        when(sslContextProvider.getSslContext(anyString(), anyString(), any())).thenReturn(sslContext);
+
+        //then
+        assertThat(createTestSubject().getKeystorePath()).isEqualTo(CERT_FILE_PATH + FileSystems.getDefault().getSeparator() + "keystorefilename");
+    }
+
+    @Test
+    public void getKeystorePath_whenConfigured_yieldPathAndFile() {
+        // when
+        when(systemPropertyHelper.getAAIKeystoreFilename()).thenReturn(Optional.empty());
+
+        //then
+        assertThat(createTestSubject().getKeystorePath()).isEqualTo("");
+    }
+
+    @Test
+    public void getKeystorePath_whenConfiguredWithSlash_yieldFilenameWithoutPath() {
+        // when
+        final String filenameWithSlash = "/path/to/keystorefilename";
+        when(systemPropertyHelper.getAAIKeystoreFilename()).thenReturn(Optional.of(filenameWithSlash));
+
+        //then
+        assertThat(createTestSubject().getKeystorePath()).isEqualTo(filenameWithSlash);
+    }
+
     @Test
     public void testGetUnsecuredClient() throws Exception {
         // when