From 974ca909fd004351024e36d6e91169d90b3419e7 Mon Sep 17 00:00:00 2001 From: Tomek Kaminski Date: Thu, 11 Apr 2019 10:31:12 +0200 Subject: [PATCH] Fix for loading cadi properties Change-Id: I5b6115b8bf7dc0b9cde67e2983f2aad6781bd475 Issue-ID: DMAAP-1158 Signed-off-by: Tomek Kaminski --- .../dbcapi/resources/AAFAuthenticationFilter.java | 16 +++++++++++++++- .../resources/AAFAuthenticationFilterTest.java | 21 ++++++++++++++++++--- src/test/resources/cadi.properties | 0 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/cadi.properties diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java index 8739511..c5c29fa 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java @@ -21,7 +21,9 @@ package org.onap.dmaap.dbcapi.resources; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.FileInputStream; import java.io.IOException; +import java.util.Properties; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -98,7 +100,7 @@ public class AAFAuthenticationFilter implements Filter { try { String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES); if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) { - cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile)); + cadiFilter = new CadiFilter(loadCadiProperties(cadiPropertiesFile)); } else { throw new ServletException("Cannot initialize CADI filter.CADI properties not available."); } @@ -109,6 +111,18 @@ public class AAFAuthenticationFilter implements Filter { } } + private PropAccess loadCadiProperties(String propertiesFilePath) throws ServletException { + try { + Properties props = new Properties(); + props.load(new FileInputStream(propertiesFilePath)); + return new PropAccess(props); + } catch (IOException e) { + String msg = "Could not load CADI properties file: " + propertiesFilePath; + LOGGER.error(msg, e); + throw new ServletException(msg); + } + } + DmaapConfig getConfig() { return (DmaapConfig) DmaapConfig.getConfig(); } diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java index d5ae5fd..53c8021 100644 --- a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java +++ b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java @@ -118,10 +118,25 @@ public class AAFAuthenticationFilterTest { } @Test - public void init_shouldInitializeCADI_whenAafIsUsed_andCadiPropertiesSet() throws Exception { + public void init_shouldFail_whenAafIsUsed_andInvalidCadiPropertiesSet() throws Exception { //given + String invalidFilePath = "src/test/resources/notExisting.properties"; doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); - doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + doReturn(invalidFilePath).when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + + //then + thrown.expect(ServletException.class); + thrown.expectMessage("Could not load CADI properties file: "+invalidFilePath); + + //when + filter.init(filterConfig); + } + + @Test + public void init_shouldInitializeCADI_whenAafIsUsed_andValidCadiPropertiesSet() throws Exception { + //given + doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); + doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); //when filter.init(filterConfig); @@ -170,7 +185,7 @@ public class AAFAuthenticationFilterTest { private void initCADIFilter() throws Exception{ doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); - doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); filter.init(filterConfig); filter.setCadiFilter(cadiFilterMock); } diff --git a/src/test/resources/cadi.properties b/src/test/resources/cadi.properties new file mode 100644 index 0000000..e69de29 -- 2.16.6