Merge "Doc security after implementation update"
authorvarun gudisena <varuneshwar.gudisena@att.com>
Tue, 9 Apr 2019 20:21:24 +0000 (20:21 +0000)
committerGerrit Code Review <gerrit@onap.org>
Tue, 9 Apr 2019 20:21:24 +0000 (20:21 +0000)
38 files changed:
pom.xml
src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java
src/main/java/org/onap/dmaap/dbcapi/client/MrProvConnection.java
src/main/java/org/onap/dmaap/dbcapi/client/MrTopicConnection.java
src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java
src/main/java/org/onap/dmaap/dbcapi/model/DR_Pub.java
src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java [new file with mode: 0644]
src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java [new file with mode: 0644]
src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java
src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java
src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java
src/main/java/org/onap/dmaap/dbcapi/server/JettyServer.java
src/main/java/org/onap/dmaap/dbcapi/server/Main.java
src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java
src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java
src/main/java/org/onap/dmaap/dbcapi/service/MirrorMakerService.java
src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java [new file with mode: 0644]
src/main/resources/schema_12.sql
src/test/java/org/onap/dmaap/dbcapi/database/DBFieldHandlerTest.java
src/test/java/org/onap/dmaap/dbcapi/model/DRSubTest.java
src/test/java/org/onap/dmaap/dbcapi/model/TopicTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilterTest.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/resources/DR_NodeResourceTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/resources/RequiredFieldExceptionTest.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/resources/TestFeedCreator.java [new file with mode: 0644]
src/test/java/org/onap/dmaap/dbcapi/util/PermissionBuilderTest.java [new file with mode: 0644]
version.properties

diff --git a/pom.xml b/pom.xml
index fdc0cc5..6517cd1 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <jettyVersion>9.4.12.RC2</jettyVersion> 
                <eelf.version>1.0.0</eelf.version>
-               <artifact.version>1.0.24-SNAPSHOT</artifact.version>
+               <artifact.version>1.0.26-SNAPSHOT</artifact.version>
                <!-- SONAR -->
                <jacoco.version>0.7.7.201606060606</jacoco.version>
                <sonar-jacoco-listeners.version>3.2</sonar-jacoco-listeners.version>
index e22290a..934e541 100644 (file)
@@ -37,8 +37,11 @@ import java.net.UnknownHostException;
 import java.net.ConnectException;
 
 import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLHandshakeException;
 
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
 import org.apache.commons.codec.binary.Base64;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
@@ -129,6 +132,10 @@ public class AafConnection extends BaseLoggingClass {
                        uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
+
+                       SSLContext sc = SSLContext.getInstance("SSL");
+                       sc.init(null, trustAllCerts, new java.security.SecureRandom());
+                       uc.setSSLSocketFactory(sc.getSocketFactory());
                        OutputStream os = null;
 
                        
@@ -296,6 +303,27 @@ public class AafConnection extends BaseLoggingClass {
                return rc;
                
        }
+
+       private TrustManager[] trustAllCerts = new TrustManager[]{
+               new X509TrustManager() {
+
+                       @Override
+                       public java.security.cert.X509Certificate[] getAcceptedIssuers()
+                       {
+                               return null;
+                       }
+                       @Override
+                       public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
+                       {
+                               //No need to implement.
+                       }
+                       @Override
+                       public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
+                       {
+                               //No need to implement.
+                       }
+               }
+       };
        
 
 }
index 51bad4f..6e692fa 100644 (file)
@@ -36,131 +36,131 @@ import java.net.*;
 import java.util.Arrays;
 
 public class MrProvConnection extends BaseLoggingClass{
-           
-       private String provURL;
-       
-       private HttpURLConnection uc;
-
-       
-       private String topicMgrCred;
-       private boolean useAAF;
-       private String  user;
-       private String  encPwd;
-       
-       public MrProvConnection() {
-               String mechIdProperty = "aaf.TopicMgrUser";
-               String pwdProperty = "aaf.TopicMgrPassword";
-               DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
-               user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
-               encPwd = p.getProperty( pwdProperty, "notSet" );
-               useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
-               topicMgrCred =  getCred();
-               
-       }
-       
-       private String getCred( ) {
-
-
-               String pwd = "";
-               AafDecrypt decryptor = new AafDecrypt();        
-               pwd = decryptor.decrypt(encPwd);
-               return user + ":" + pwd;        
-       }
-       
-       
-       public boolean makeTopicConnection( MR_Cluster cluster ) {
-               logger.info( "connect to cluster: " + cluster.getDcaeLocationName());
-       
-
-               provURL = cluster.getTopicProtocol() + "://" + cluster.getFqdn() + ":" + cluster.getTopicPort() + "/topics/create";
-
-               if ( cluster.getTopicProtocol().equals( "https" ) ) {
-                       return makeSecureConnection( provURL );
-               }
-               return makeConnection( provURL );
-       }
-
-       private boolean makeSecureConnection( String pURL ) {
-               logger.info( "makeConnection to " + pURL );
-       
-               try {
-                       URL u = new URL( pURL );
-                       uc = (HttpsURLConnection) u.openConnection();
-                       uc.setInstanceFollowRedirects(false);
-                       logger.info( "open connect to " + pURL );
-                       return(true);
-               } catch( UnknownHostException uhe ){
-               logger.error( "Caught UnknownHostException for " + pURL);
-               return(false);
+        
+    private String provURL;
+    
+    private HttpURLConnection uc;
+
+    
+    private String topicMgrCred;
+    private boolean useAAF;
+    private    String    user;
+    private    String    encPwd;
+    
+    public MrProvConnection() {
+        String mechIdProperty = "aaf.TopicMgrUser";
+        String pwdProperty = "aaf.TopicMgrPassword";
+        DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
+        user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" );
+        encPwd = p.getProperty( pwdProperty, "notSet" );
+        useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
+        topicMgrCred =  getCred();
+        
+    }
+    
+    private String getCred( ) {
+
+
+        String pwd = "";
+        AafDecrypt decryptor = new AafDecrypt();    
+        pwd = decryptor.decrypt(encPwd);
+        return user + ":" + pwd;    
+    }
+    
+    
+    public boolean makeTopicConnection( MR_Cluster cluster ) {
+        logger.info( "connect to cluster: " + cluster.getDcaeLocationName());
+    
+
+        provURL = cluster.getTopicProtocol() + "://" + cluster.getFqdn() + ":" + cluster.getTopicPort() + "/topics/create";
+
+        if ( cluster.getTopicProtocol().equals( "https" ) ) {
+            return makeSecureConnection( provURL );
+        }
+        return makeConnection( provURL );
+    }
+
+    private boolean makeSecureConnection( String pURL ) {
+        logger.info( "makeConnection to " + pURL );
+    
+        try {
+            URL u = new URL( pURL );
+            uc = (HttpsURLConnection) u.openConnection();
+            uc.setInstanceFollowRedirects(false);
+            logger.info( "open connect to " + pURL );
+            return(true);
+        } catch( UnknownHostException uhe ){
+            logger.error( "Caught UnknownHostException for " + pURL);
+            return(false);
         } catch (Exception e) {
             logger.error("Unexpected error during openConnection of " + pURL );
-            e.printStackTrace();
+            logger.error("Unexpected error during openConnection of ",e );
             return(false);
         } 
 
-       }
-       private boolean makeConnection( String pURL ) {
-               logger.info( "makeConnection to " + pURL );
-       
-               try {
-                       URL u = new URL( pURL );
-                       uc = (HttpURLConnection) u.openConnection();
-                       uc.setInstanceFollowRedirects(false);
-                       logger.info( "open connect to " + pURL );
-                       return(true);
-               } catch( UnknownHostException uhe ){
-               logger.error( "Caught UnknownHostException for " + pURL);
-               return(false);
+    }
+    private boolean makeConnection( String pURL ) {
+        logger.info( "makeConnection to " + pURL );
+    
+        try {
+            URL u = new URL( pURL );
+            uc = (HttpURLConnection) u.openConnection();
+            uc.setInstanceFollowRedirects(false);
+            logger.info( "open connect to " + pURL );
+            return(true);
+        } catch( UnknownHostException uhe ){
+            logger.error( "Caught UnknownHostException for " + pURL);
+            return(false);
         } catch (Exception e) {
             logger.error("Unexpected error during openConnection of " + pURL );
-            e.printStackTrace();
+            logger.error("Unexpected error during openConnection of ",e );
             return(false);
         } 
 
-       }
-       
-       static String bodyToString( InputStream is ) {
-               StringBuilder sb = new StringBuilder();
-               BufferedReader br = new BufferedReader( new InputStreamReader(is));
-               String line;
-               try {
-                       while ((line = br.readLine()) != null ) {
-                               sb.append( line );
-                       }
-               } catch (IOException ex ) {
-                       errorLogger.error( "IOexception:" + ex);
-               }
-                       
-               return sb.toString();
-       }
-       
-       public String doPostTopic( Topic postTopic, ApiError err ) {
-               String auth =  "Basic " + Base64.encodeBase64String(topicMgrCred.getBytes());
-
-
-               String responsemessage = null;
-               int rc = -1;
-
-
-               try {
-                       byte[] postData = postTopic.getBytes();
-                       logger.info( "post fields=" + Arrays.toString(postData));
-                       
-                       // when not using AAF, do not attempt Basic Authentication
-                       if ( useAAF ) {
-                               uc.setRequestProperty("Authorization", auth);
-                               logger.info( "Authenticating with " + auth );
-                       }
-                       uc.setRequestMethod("POST");
-                       uc.setRequestProperty("Content-Type", "application/json");
-                       uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
-                       uc.setUseCaches(false);
-                       uc.setDoOutput(true);
-                       OutputStream os = null;
-
-                       
-                       try {
+    }
+    
+    static String bodyToString( InputStream is ) {
+        StringBuilder sb = new StringBuilder();
+        BufferedReader br = new BufferedReader( new InputStreamReader(is));
+        String line;
+        try {
+            while ((line = br.readLine()) != null ) {
+                sb.append( line );
+            }
+        } catch (IOException ex ) {
+            errorLogger.error( "IOexception:" + ex);
+        }
+            
+        return sb.toString();
+    }
+    
+    public String doPostTopic( Topic postTopic, ApiError err ) {
+        String auth =  "Basic " + Base64.encodeBase64String(topicMgrCred.getBytes());
+
+
+        String responsemessage = null;
+        int rc = -1;
+
+
+        try {
+            byte[] postData = postTopic.getBytes();
+            logger.info( "post fields=" + Arrays.toString(postData));
+            
+            // when not using AAF, do not attempt Basic Authentication
+            if ( useAAF ) {
+                uc.setRequestProperty("Authorization", auth);
+                logger.info( "Authenticating with " + auth );
+            }
+            uc.setRequestMethod("POST");
+            uc.setRequestProperty("Content-Type", "application/json");
+            uc.setRequestProperty( "charset", "utf-8");
+            uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
+            uc.setUseCaches(false);
+            uc.setDoOutput(true);
+            OutputStream os = null;
+
+            
+            try {
                  uc.connect();
                  os = uc.getOutputStream();
                  os.write( postData );
@@ -174,21 +174,21 @@ public class MrProvConnection extends BaseLoggingClass{
                  } catch (Exception e) {
                  }
             } catch ( UnknownHostException uhe ) {
-               errorLogger.error( DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION , "Unknown Host Exception" , provURL );
-               err.setCode(500);
-               err.setMessage("Unknown Host Exception");
-               err.setFields( uc.getURL().getHost());
-               return new String( "500: " + uhe.getMessage());
+                errorLogger.error( DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION , "Unknown Host Exception" , provURL );
+                err.setCode(500);
+                err.setMessage("Unknown Host Exception");
+                err.setFields( uc.getURL().getHost());
+                return new String( "500: " + uhe.getMessage());
             }catch ( ConnectException ce ) {
-               errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception"  );
-               err.setCode(500);
-               err.setMessage("HTTP Connection Exception");
-               err.setFields( uc.getURL().getHost());
-               return new String( "500: " + ce.getMessage());
+                errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception"  );
+                err.setCode(500);
+                err.setMessage("HTTP Connection Exception");
+                err.setFields( uc.getURL().getHost());
+                return new String( "500: " + ce.getMessage());
             }
-                       rc = uc.getResponseCode();
-                       logger.info( "http response code:" + rc );
-                       err.setCode(rc);
+            rc = uc.getResponseCode();
+            logger.info( "http response code:" + rc );
+            err.setCode(rc);
             responsemessage = uc.getResponseMessage();
             logger.info( "responsemessage=" + responsemessage );
             err.setMessage(responsemessage);
@@ -207,29 +207,29 @@ public class MrProvConnection extends BaseLoggingClass{
                  }
             }
             if (rc >= 200 && rc < 300 ) {
-                       String responseBody = null;
-                       responseBody = bodyToString( uc.getInputStream() );
-                       logger.info( "responseBody=" + responseBody );
-                       return responseBody;
+                String responseBody = null;
+                 responseBody = bodyToString( uc.getInputStream() );
+                logger.info( "responseBody=" + responseBody );
+                return responseBody;
 
             } 
             
-               } catch (Exception e) {
-                       errorLogger.error("Unable to read response  " );
+        } catch (Exception e) {
+            errorLogger.error("Unable to read response  " );
            
         }
-               finally {
-                       try {
-                               uc.disconnect();
-                       } catch ( Exception e ) {
-                               errorLogger.error("Unable to disconnect");
-                       }
-               }
-               return new String( rc +": " + responsemessage );
+        finally {
+            try {
+                uc.disconnect();
+            } catch ( Exception e ) {
+                errorLogger.error("Unable to disconnect");
+            }
+        }
+        return new String( rc +": " + responsemessage );
 
-       }
-       
+    }
+    
 
 
-               
+        
 }
index a92dbc7..28a9add 100644 (file)
@@ -31,8 +31,10 @@ import java.net.ProtocolException;
 import java.net.URL;
 import java.net.HttpURLConnection;
 
+import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLSession;
 
 import org.apache.commons.codec.binary.Base64;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
@@ -49,13 +51,14 @@ public class MrTopicConnection extends BaseLoggingClass  {
        private  String mmProvCred; 
        private String unit_test;
        private boolean useAAF;
-
+       private boolean hostnameVerify;
 
        public MrTopicConnection(String user, String pwd ) {
                mmProvCred = new String( user + ":" + pwd );
                DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
         unit_test = p.getProperty( "UnitTest", "No" );
        useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
+       hostnameVerify= "true".equalsIgnoreCase(p.getProperty("MR.hostnameVerify", "true"));
        }
        
        public boolean makeTopicConnection( MR_Cluster cluster, String topic, String overrideFqdn ) {
@@ -71,13 +74,28 @@ public class MrTopicConnection extends BaseLoggingClass  {
                return makeConnection( topicURL );
        }
 
+       
        private boolean makeSecureConnection( String pURL ) {
                logger.info( "makeConnection to " + pURL );
-       
+               
                try {
+                       HostnameVerifier hostnameVerifier = new HostnameVerifier() {
+                               @Override
+                               public boolean verify( String hostname, SSLSession session ) {
+                                       return true;
+                               }
+                       
+                       };
+       
+               
                        URL u = new URL( pURL );
-                       uc = (HttpsURLConnection) u.openConnection();
+                       uc = (HttpsURLConnection) u.openConnection();                   
                        uc.setInstanceFollowRedirects(false);
+                       if ( ! hostnameVerify ) {
+                               HttpsURLConnection ucs = (HttpsURLConnection) uc;
+                               ucs.setHostnameVerifier(hostnameVerifier);
+                       }
+       
                        logger.info( "open connection to " + pURL );
                        return(true);
                } catch (Exception e) {
index a85f040..4b2ef90 100644 (file)
@@ -21,6 +21,7 @@
 package org.onap.dmaap.dbcapi.model;
 
 import javax.xml.bind.annotation.XmlRootElement;
+import java.util.Objects;
 
 @XmlRootElement
 public class DR_Node extends DmaapObject {
@@ -75,4 +76,19 @@ public class DR_Node extends DmaapObject {
                this.version = version;
        }
 
+       @Override
+       public boolean equals(Object o) {
+               if (this == o) return true;
+               if (o == null || getClass() != o.getClass()) return false;
+               DR_Node dr_node = (DR_Node) o;
+               return Objects.equals(fqdn, dr_node.fqdn) &&
+                               Objects.equals(dcaeLocationName, dr_node.dcaeLocationName) &&
+                               Objects.equals(hostName, dr_node.hostName) &&
+                               Objects.equals(version, dr_node.version);
+       }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(fqdn, dcaeLocationName, hostName, version);
+       }
 }
index 0d146b7..4e64089 100644 (file)
@@ -24,6 +24,8 @@ import javax.xml.bind.annotation.XmlRootElement;
 
 import org.onap.dmaap.dbcapi.util.RandomString;
 
+import java.util.Objects;
+
 @XmlRootElement
 public class DR_Pub extends DmaapObject {
 
@@ -151,5 +153,35 @@ public class DR_Pub extends DmaapObject {
                return ri.nextString();
                
        }
-       
+
+       @Override
+       public boolean equals(Object o) {
+               if (this == o) return true;
+               if (o == null || getClass() != o.getClass()) return false;
+               DR_Pub dr_pub = (DR_Pub) o;
+               return Objects.equals(dcaeLocationName, dr_pub.dcaeLocationName) &&
+                               Objects.equals(username, dr_pub.username) &&
+                               Objects.equals(userpwd, dr_pub.userpwd) &&
+                               Objects.equals(feedId, dr_pub.feedId) &&
+                               Objects.equals(pubId, dr_pub.pubId);
+       }
+
+       @Override
+       public int hashCode() {
+
+               return Objects.hash(dcaeLocationName, username, userpwd, feedId, pubId);
+       }
+
+       @Override
+       public String toString() {
+               return "DR_Pub{" +
+                               "dcaeLocationName='" + dcaeLocationName + '\'' +
+                               ", username='" + username + '\'' +
+                               ", userpwd='" + userpwd + '\'' +
+                               ", feedId='" + feedId + '\'' +
+                               ", pubId='" + pubId + '\'' +
+                               ", feedName='" + feedName + '\'' +
+                               ", feedVersion='" + feedVersion + '\'' +
+                               '}';
+       }
 }
index 8ac0880..90da956 100644 (file)
@@ -45,7 +45,7 @@ public class DR_Sub extends DmaapObject {
        private boolean guaranteedDelivery;
        private boolean guaranteedSequence;
        private boolean privilegedSubscriber;
-       private boolean decompressData;
+       private boolean decompress;
 
        // NOTE: the following fields are optional in the API but not stored in the DB
 
@@ -116,9 +116,9 @@ public class DR_Sub extends DmaapObject {
                        this.setPrivilegedSubscriber(false);
                }
                try {
-                       this.setDecompressData((boolean) jsonObj.get("decompressData"));
+                       this.setDecompress((boolean) jsonObj.get("decompress"));
                } catch( NullPointerException npe ) {
-                       this.setDecompressData(false);
+                       this.setDecompress(false);
                }
 
                JSONObject del = (JSONObject) jsonObj.get("delivery");
@@ -239,12 +239,12 @@ public class DR_Sub extends DmaapObject {
                this.privilegedSubscriber = privilegedSubscriber;
        }
 
-       public boolean isDecompressData() {
-               return decompressData;
+       public boolean isDecompress() {
+               return decompress;
        }
 
-       public void setDecompressData(boolean decompressData) {
-               this.decompressData = decompressData;
+       public void setDecompress(boolean decompressData) {
+               this.decompress = decompressData;
        }
 
        public String getFeedName() {
@@ -286,7 +286,7 @@ public class DR_Sub extends DmaapObject {
                                ,"0"
                                ,"true"
                                ,this.isPrivilegedSubscriber()
-                               ,this.isDecompressData()
+                               ,this.isDecompress()
                        );
 
                logger.info( postJSON );
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java
new file mode 100644 (file)
index 0000000..8739511
--- /dev/null
@@ -0,0 +1,128 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.log4j.Logger;
+import org.eclipse.jetty.http.HttpStatus;
+import org.onap.aaf.cadi.PropAccess;
+import org.onap.aaf.cadi.filter.CadiFilter;
+import org.onap.dmaap.dbcapi.model.ApiError;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+
+public class AAFAuthenticationFilter implements Filter {
+
+    private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName());
+    static final String CADI_PROPERTIES = "cadi.properties";
+    static final String AAF_AUTHN_FLAG = "UseAAF";
+
+    private boolean isAafEnabled;
+    private CadiFilter cadiFilter;
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        DmaapConfig dmaapConfig = getConfig();
+        String flag = dmaapConfig.getProperty(AAF_AUTHN_FLAG, "false");
+        isAafEnabled = "true".equalsIgnoreCase(flag);
+        initCadi(dmaapConfig);
+    }
+
+
+    @Override
+    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
+        throws IOException, ServletException {
+
+        if(isAafEnabled) {
+            cadiFilter.doFilter(servletRequest, servletResponse, filterChain);
+            updateResponseBody((HttpServletResponse)servletResponse);
+        } else {
+            filterChain.doFilter(servletRequest, servletResponse);
+        }
+    }
+
+    private void updateResponseBody(HttpServletResponse httpResponse)
+        throws IOException {
+        if(httpResponse.getStatus() == 401) {
+            String errorMsg = "invalid or no credentials provided";
+            LOGGER.error(errorMsg);
+            httpResponse.setContentType("application/json");
+            httpResponse.setCharacterEncoding("UTF-8");
+            httpResponse.getWriter().print(buildErrorResponse(errorMsg));
+            httpResponse.getWriter().flush();
+        }
+    }
+
+    private String buildErrorResponse(String msg) {
+        try {
+            return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.UNAUTHORIZED_401, msg, "Authentication"));
+        } catch (JsonProcessingException e) {
+            LOGGER.warn("Could not serialize response entity: " + e.getMessage());
+            return "";
+        }
+    }
+
+
+    @Override
+    public void destroy() {
+        //nothing to cleanup
+    }
+
+    private void initCadi(DmaapConfig dmaapConfig) throws ServletException {
+        if(isAafEnabled) {
+            try {
+                String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES);
+                if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) {
+                    cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile));
+                } else {
+                    throw new ServletException("Cannot initialize CADI filter.CADI properties not available.");
+                }
+            } catch (ServletException e) {
+                LOGGER.error("CADI init error :" + e.getMessage());
+                throw e;
+            }
+        }
+    }
+
+    DmaapConfig getConfig() {
+        return (DmaapConfig) DmaapConfig.getConfig();
+    }
+
+    //tests only
+    CadiFilter getCadiFilter() {
+        return cadiFilter;
+    }
+
+    void setCadiFilter(CadiFilter cadiFilter) {
+        this.cadiFilter = cadiFilter;
+    }
+
+    boolean isAafEnabled() {
+        return isAafEnabled;
+    }
+}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java
new file mode 100644 (file)
index 0000000..5bc3dec
--- /dev/null
@@ -0,0 +1,116 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.log4j.Logger;
+import org.eclipse.jetty.http.HttpStatus;
+import org.onap.dmaap.dbcapi.model.ApiError;
+import org.onap.dmaap.dbcapi.service.DmaapService;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+import org.onap.dmaap.dbcapi.util.PermissionBuilder;
+
+public class AAFAuthorizationFilter implements Filter{
+
+    private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName());
+    static final String AAF_AUTHZ_FLAG = "UseAAF";
+    private boolean isAafEnabled = false;
+
+    private PermissionBuilder permissionBuilder;
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        DmaapConfig dmaapConfig = getConfig();
+        isAafEnabled = "true".equalsIgnoreCase(dmaapConfig.getProperty(AAF_AUTHZ_FLAG, "false"));
+        if(isAafEnabled) {
+            permissionBuilder = new PermissionBuilder(dmaapConfig, getDmaapService());
+        }
+    }
+
+    @Override
+    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
+        throws IOException, ServletException {
+
+        if(isAafEnabled) {
+            HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
+            permissionBuilder.updateDmaapInstance();
+            String permission = permissionBuilder.buildPermission(httpRequest);
+
+            if (httpRequest.isUserInRole(permission)) {
+                LOGGER.info("User " + httpRequest.getUserPrincipal().getName() + " has permission " + permission);
+                filterChain.doFilter(servletRequest, servletResponse);
+            } else {
+                String msg = "User " + httpRequest.getUserPrincipal().getName() + " does not have permission " + permission;
+                LOGGER.error(msg);
+                ((HttpServletResponse) servletResponse).setStatus(HttpStatus.FORBIDDEN_403);
+                servletResponse.setContentType("application/json");
+                servletResponse.setCharacterEncoding("UTF-8");
+                servletResponse.getWriter().print(buildErrorResponse(msg));
+                servletResponse.getWriter().flush();
+            }
+        } else {
+            filterChain.doFilter(servletRequest, servletResponse);
+        }
+    }
+
+    @Override
+    public void destroy() {
+        //nothing to cleanup
+    }
+
+    DmaapConfig getConfig() {
+        return (DmaapConfig) DmaapConfig.getConfig();
+    }
+
+    DmaapService getDmaapService() {
+        return new DmaapService();
+    }
+
+    private String buildErrorResponse(String msg) {
+        try {
+            return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.FORBIDDEN_403, msg, "Authorization"));
+        } catch (JsonProcessingException e) {
+            LOGGER.warn("Could not serialize response entity: " + e.getMessage());
+            return "";
+        }
+    }
+
+    PermissionBuilder getPermissionBuilder() {
+        return permissionBuilder;
+    }
+
+    void setPermissionBuilder(PermissionBuilder permissionBuilder) {
+        this.permissionBuilder = permissionBuilder;
+    }
+
+    void setAafEnabled(boolean aafEnabled) {
+        isAafEnabled = aafEnabled;
+    }
+}
index fd5b4aa..3ed5717 100644 (file)
@@ -26,33 +26,44 @@ import javax.ws.rs.container.ContainerRequestFilter;
 import org.apache.log4j.Logger;
 import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException;
 import org.onap.dmaap.dbcapi.service.ApiService;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
 
 @Authorization
 public class AuthorizationFilter implements ContainerRequestFilter   {
-       
-       private Logger logger = Logger.getLogger(AuthorizationFilter.class.getName());
-       private ResponseBuilder responseBuilder = new ResponseBuilder();
-       
+
+       private static final String AAF_FLAG = "UseAAF";
+       private final Logger logger = Logger.getLogger(AuthorizationFilter.class.getName());
+       private final ResponseBuilder responseBuilder = new ResponseBuilder();
+       private final boolean isAafEnabled;
+
+
+       public AuthorizationFilter() {
+               DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig();
+               String flag = dmaapConfig.getProperty(AAF_FLAG, "false");
+               isAafEnabled = "true".equalsIgnoreCase(flag);
+       }
+
        @Override
        public void filter(ContainerRequestContext requestContext) {
 
-               ApiService apiResp = new ApiService()
-                       .setAuth( requestContext.getHeaderString("Authorization") )
-                       .setUriPath(requestContext.getUriInfo().getPath())
-                       .setHttpMethod( requestContext.getMethod() )
-                       .setRequestId( requestContext.getHeaderString("X-ECOMP-RequestID") );
-
-               try {
-                       apiResp.checkAuthorization();
-               } catch ( AuthenticationErrorException ae ) {
-                       logger.error("Error", ae);
-                       requestContext.abortWith( responseBuilder.unauthorized( apiResp.getErr().getMessage() ) );
-               } catch ( Exception e ) {
-                       logger.error("Error", e);
-                       requestContext.abortWith( responseBuilder.unavailable() );
-               }
+               if(!isAafEnabled) {
+                       ApiService apiResp = new ApiService()
+                               .setAuth(requestContext.getHeaderString("Authorization"))
+                               .setUriPath(requestContext.getUriInfo().getPath())
+                               .setHttpMethod(requestContext.getMethod())
+                               .setRequestId(requestContext.getHeaderString("X-ECOMP-RequestID"));
 
+                       try {
+                               apiResp.checkAuthorization();
+                       } catch (AuthenticationErrorException ae) {
+                               logger.error("Error", ae);
+                               requestContext.abortWith(responseBuilder.unauthorized(apiResp.getErr().getMessage()));
+                       } catch (Exception e) {
+                               logger.error("Error", e);
+                               requestContext.abortWith(responseBuilder.unavailable());
+                       }
+               }
        }
 
 }
index 49dc69a..f001136 100644 (file)
@@ -42,7 +42,6 @@ import javax.ws.rs.core.Response;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Node;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.DR_NodeService;
 
 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
@@ -83,10 +82,9 @@ public class DR_NodeResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = DR_Node.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response addDr_Node( 
-                       DR_Node node
-                       ) {
-               ApiService resp = new ApiService();
+       public Response addDr_Node(DR_Node node) {
+
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "dcaeLocation", node.getDcaeLocationName());
@@ -95,11 +93,11 @@ public class DR_NodeResource extends BaseLoggingClass {
                        return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
                                        "missing required field", "dcaeLocation, fqdn"));
                }
-               DR_Node nNode = dr_nodeService.addDr_Node(node, resp.getErr());
-               if ( resp.getErr().is2xx()) {
+               DR_Node nNode = dr_nodeService.addDr_Node(node, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(nNode);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
        
        @PUT
@@ -111,24 +109,23 @@ public class DR_NodeResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{fqdn}")
-       public Response updateDr_Node( 
-                       @PathParam("fqdn") String name, 
-                       DR_Node node
-                       ) {
-               ApiService resp = new ApiService();
+       public Response updateDr_Node(@PathParam("fqdn") String name, DR_Node node) {
+
+               ApiError apiError = new ApiError();
 
                try {
-                       checker.required( "dcaeLocation", name);
+                       checker.required( "dcaeLocation", node.getDcaeLocationName());
                        checker.required( "fqdn", node.getFqdn());
                } catch ( RequiredFieldException rfe ) {
-                       return responseBuilder.error(rfe.getApiError());
+                       return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(),
+                                       "missing required field", "dcaeLocation, fqdn"));
                }
                node.setFqdn(name);
-               DR_Node nNode = dr_nodeService.updateDr_Node(node, resp.getErr());
-               if ( resp.getErr().is2xx()) {
+               DR_Node nNode = dr_nodeService.updateDr_Node(node, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(nNode);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
        
        @DELETE
@@ -141,22 +138,16 @@ public class DR_NodeResource extends BaseLoggingClass {
        })
        @Path("/{fqdn}")
        public Response deleteDr_Node( 
-                       @PathParam("fqdn") String name
-                       ){
+                       @PathParam("fqdn") String name){
 
-               ApiService resp = new ApiService();
 
-               try {
-                       checker.required( "fqdn", name);
-               } catch ( RequiredFieldException rfe ) {
-                       logger.debug( rfe.getApiError().toString() );
-                       return responseBuilder.error(rfe.getApiError());
-               }
-               dr_nodeService.removeDr_Node(name, resp.getErr());
-               if ( resp.getErr().is2xx() ) {
+               ApiError apiError = new ApiError();
+
+               dr_nodeService.removeDr_Node(name, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 
        @GET
@@ -168,15 +159,14 @@ public class DR_NodeResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{fqdn}")
-       public Response get( 
-                       @PathParam("fqdn") String name
-                       ) {
-               ApiService resp = new ApiService();
+       public Response get(@PathParam("fqdn") String name) {
+
+               ApiError apiError = new ApiError();
 
-               DR_Node nNode = dr_nodeService.getDr_Node( name, resp.getErr() );
-               if ( resp.getErr().is2xx() ) {
+               DR_Node nNode = dr_nodeService.getDr_Node( name, apiError );
+               if (apiError.is2xx()) {
                        return responseBuilder.success(nNode);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 }
index 1054b03..6e652a8 100644 (file)
@@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Pub;
 import org.onap.dmaap.dbcapi.model.Feed;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.DR_PubService;
 import org.onap.dmaap.dbcapi.service.FeedService;
 
@@ -87,10 +86,8 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = DR_Pub.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response addDr_Pub( 
-                       DR_Pub pub
-                       ) {
-               ApiService resp = new ApiService();
+       public Response addDr_Pub(DR_Pub pub) {
+               ApiError apiError = new ApiError();
                FeedService feeds = new FeedService();
                Feed fnew = null;
 
@@ -107,9 +104,10 @@ public class DR_PubResource extends BaseLoggingClass {
                        }
                        // if we found a FeedName instead of a FeedId then try to look it up.
                        List<Feed> nfeeds =  feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
-                       if ( nfeeds.size() != 1 ) {
-                               logger.debug( "Attempt to match "+ pub.getFeedName() + " ver="+pub.getFeedVersion() + " matched " + nfeeds.size() );
-                               return responseBuilder.error(resp.getErr());
+                       if ( nfeeds.isEmpty() ) {
+                               apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                               apiError.setFields("feedName");
+                               return responseBuilder.error(apiError);
                        }
                        fnew = nfeeds.get(0);
                }
@@ -123,11 +121,11 @@ public class DR_PubResource extends BaseLoggingClass {
 
                // we may have fnew already if located by FeedName
                if ( fnew == null ) {
-                       fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+                       fnew = feeds.getFeed(pub.getFeedId(), apiError);
                }
                if ( fnew == null ) {
                        logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");        
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
 
                ArrayList<DR_Pub> pubs = fnew.getPubs();
@@ -143,15 +141,15 @@ public class DR_PubResource extends BaseLoggingClass {
                }
                pubs.add( pub );
                fnew.setPubs(pubs);
-               fnew = feeds.updateFeed( fnew, resp.getErr() ); 
+               fnew = feeds.updateFeed(fnew, apiError);
                
-               if ( ! resp.getErr().is2xx()) { 
-                       return responseBuilder.error(resp.getErr());
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                pubs = fnew.getPubs();
                logger.info( "num existing pubs after = " + pubs.size() );
                
-               DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), resp.getErr());
+               DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError);
                return responseBuilder.success(Status.CREATED.getStatusCode(), pnew);
        }
        
@@ -164,10 +162,7 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response updateDr_Pub( 
-                       @PathParam("pubId") String name, 
-                       DR_Pub pub
-                       ) {
+       public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) {
                logger.info( "Entry: PUT /dr_pubs");
                pub.setPubId(name);
                DR_Pub res = dr_pubService.updateDr_Pub(pub);
@@ -183,11 +178,9 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response deleteDr_Pub( 
-                       @PathParam("pubId") String id
-                       ){
+       public Response deleteDr_Pub(@PathParam("pubId") String id){
 
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "pubId", id);
@@ -195,21 +188,21 @@ public class DR_PubResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
 
-               DR_Pub pub =  dr_pubService.getDr_Pub( id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return responseBuilder.error(resp.getErr());
+               DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
+               if ( !apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                FeedService feeds = new FeedService();
-               Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+               Feed fnew = feeds.getFeed(pub.getFeedId(), apiError);
                if ( fnew == null ) {
                        logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");     
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
                ArrayList<DR_Pub> pubs = fnew.getPubs();
                if ( pubs.size() == 1 ) {
-                       resp.setCode(Status.BAD_REQUEST.getStatusCode());
-                       resp.setMessage( "Can't delete the last publisher of a feed");
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+                       apiError.setMessage( "Can't delete the last publisher of a feed");
+                       return responseBuilder.error(apiError);
                }
                
                for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) {
@@ -219,14 +212,14 @@ public class DR_PubResource extends BaseLoggingClass {
                        }
                }
                fnew.setPubs(pubs);
-               fnew = feeds.updateFeed( fnew, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return responseBuilder.error(resp.getErr());
+               fnew = feeds.updateFeed(fnew,apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                
-               dr_pubService.removeDr_Pub(id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       return responseBuilder.error(resp.getErr());
+               dr_pubService.removeDr_Pub(id, apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
        }
@@ -240,10 +233,8 @@ public class DR_PubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{pubId}")
-       public Response get( 
-                       @PathParam("pubId") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response get(@PathParam("pubId") String id) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "feedId", id);
@@ -251,9 +242,9 @@ public class DR_PubResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
 
-               DR_Pub pub =  dr_pubService.getDr_Pub( id, resp.getErr() );
-               if ( ! resp.getErr().is2xx()) { 
-                       resp.getErr();                  
+               DR_Pub pub =  dr_pubService.getDr_Pub(id, apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                return responseBuilder.success(Status.OK.getStatusCode(), pub);
        }
index d5a96de..2fa6ccd 100644 (file)
@@ -24,7 +24,6 @@ package org.onap.dmaap.dbcapi.resources;
 
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 import javax.ws.rs.Consumes;
@@ -44,7 +43,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Sub;
 import org.onap.dmaap.dbcapi.model.Feed;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.DR_SubService;
 import org.onap.dmaap.dbcapi.service.FeedService;
 
@@ -91,11 +89,9 @@ public class DR_SubResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = DR_Sub.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response addDr_Sub( 
-                       DR_Sub sub
-                       ) {
-       
-               ApiService resp = new ApiService();
+       public Response addDr_Sub(DR_Sub sub) {
+
+               ApiError apiError = new ApiError();
                FeedService feeds = new FeedService();
                Feed fnew = null;
                try {
@@ -110,14 +106,14 @@ public class DR_SubResource extends BaseLoggingClass {
                        // if we found a FeedName instead of a FeedId then try to look it up.
                        List<Feed> nfeeds =  feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
                        if ( nfeeds.isEmpty() ) {
-                               resp.setCode(Status.NOT_FOUND.getStatusCode());
-                               resp.setFields("feedName");
-                               return responseBuilder.error(resp.getErr());
+                               apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                               apiError.setFields("feedName");
+                               return responseBuilder.error(apiError);
                        } else if (nfeeds.size() > 1) {
                                logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
-                               resp.setCode(Status.CONFLICT.getStatusCode());
-                               resp.setFields("feedName");
-                               return responseBuilder.error(resp.getErr());
+                               apiError.setCode(Status.CONFLICT.getStatusCode());
+                               apiError.setFields("feedName");
+                               return responseBuilder.error(apiError);
                        }
                        fnew = Iterables.getOnlyElement(nfeeds);
                }
@@ -130,19 +126,19 @@ public class DR_SubResource extends BaseLoggingClass {
                }
                // we may have fnew already if located by FeedName
                if ( fnew == null ) {
-                       fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+                       fnew = feeds.getFeed( sub.getFeedId(), apiError);
                }
                if ( fnew == null ) {
                        logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
-                       resp.setCode(Status.NOT_FOUND.getStatusCode());
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                       return responseBuilder.error(apiError);
                }
                DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
                ArrayList<DR_Sub> subs = fnew.getSubs();
                logger.info( "num existing subs before = " + subs.size() );
-               DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() );
-               if ( ! resp.getErr().is2xx() ) {
-                       return responseBuilder.error(resp.getErr());
+               DR_Sub snew = dr_subService.addDr_Sub(sub, apiError);
+               if (!apiError.is2xx()) {
+                       return responseBuilder.error(apiError);
                }
                subs.add( snew );
                logger.info( "num existing subs after = " + subs.size() );
@@ -162,12 +158,9 @@ public class DR_SubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{subId}")
-       public Response updateDr_Sub( 
-                       @PathParam("subId") String name, 
-                       DR_Sub sub
-                       ) {
+       public Response updateDr_Sub(@PathParam("subId") String name, DR_Sub sub) {
 
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "subId", name);
@@ -179,19 +172,19 @@ public class DR_SubResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                FeedService feeds = new FeedService();
-               Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+               Feed fnew = feeds.getFeed(sub.getFeedId(), apiError);
                if ( fnew == null ) {
                        logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
 
                DR_SubService dr_subService = new DR_SubService();
                sub.setSubId(name);
-               DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
+               DR_Sub nsub = dr_subService.updateDr_Sub(sub, apiError);
                if ( nsub != null && nsub.isStatusValid() ) {
                        return responseBuilder.success(nsub);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
                
        @DELETE
@@ -203,11 +196,9 @@ public class DR_SubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{subId}")
-       public Response deleteDr_Sub( 
-                       @PathParam("subId") String id
-                       ){
+       public Response deleteDr_Sub(@PathParam("subId") String id){
 
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "subId", id);
@@ -216,9 +207,9 @@ public class DR_SubResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                DR_SubService dr_subService = new DR_SubService();
-               dr_subService.removeDr_Sub(id, resp.getErr() );
-               if ( ! resp.getErr().is2xx() ) {
-                       return responseBuilder.error(resp.getErr());
+               dr_subService.removeDr_Sub(id, apiError);
+               if (!apiError.is2xx() ) {
+                       return responseBuilder.error(apiError);
                }
                return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null );
        }
@@ -232,10 +223,9 @@ public class DR_SubResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{subId}")
-       public Response get( 
-                       @PathParam("subId") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response get(@PathParam("subId") String id) {
+
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "subId", id);
@@ -244,10 +234,10 @@ public class DR_SubResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                DR_SubService dr_subService = new DR_SubService();
-               DR_Sub sub =  dr_subService.getDr_Sub( id, resp.getErr() );
+               DR_Sub sub =  dr_subService.getDr_Sub(id, apiError);
                if ( sub != null && sub.isStatusValid() ) {
                        return responseBuilder.success(sub);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 }
index 6df135d..6589c0d 100644 (file)
@@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Pub;
 import org.onap.dmaap.dbcapi.model.Feed;
 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.FeedService;
 
 
@@ -94,7 +93,7 @@ public class FeedResource extends BaseLoggingClass {
                        @WebParam(name = "feed") Feed feed,
                        @QueryParam("useExisting") String useExisting) {
 
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "feedName", feed.getFeedName());
@@ -108,32 +107,32 @@ public class FeedResource extends BaseLoggingClass {
                
                
                FeedService feedService = new FeedService();
-               Feed nfeed =  feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() );
+               Feed nfeed =  feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), apiError);
                if ( nfeed == null ) {
-                       nfeed =  feedService.addFeed( feed, resp.getErr() );
+                       nfeed =  feedService.addFeed(feed, apiError);
                        if ( nfeed != null ) {
                                return responseBuilder.success(nfeed);
                        } else {
                                logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion());
 
-                               return responseBuilder.error(resp.getErr());
+                               return responseBuilder.error(apiError);
                        }
                } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) {
                        feed.setFeedId( nfeed.getFeedId());
-                       nfeed =  feedService.updateFeed(feed, resp.getErr());
+                       nfeed =  feedService.updateFeed(feed, apiError);
                        if ( nfeed != null ) {
                                return responseBuilder.success(nfeed);
                        } else {
                                logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
 
-                               return responseBuilder.error(resp.getErr());
+                               return responseBuilder.error(apiError);
                        }
                } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) {
                        return responseBuilder.success(nfeed);
                }
 
-               resp.setCode(Status.CONFLICT.getStatusCode());
-               return responseBuilder.error(resp.getErr());
+               apiError.setCode(Status.CONFLICT.getStatusCode());
+               return responseBuilder.error(apiError);
        }
        
        @PUT
@@ -150,7 +149,7 @@ public class FeedResource extends BaseLoggingClass {
                        @WebParam(name = "feed") Feed feed) {
 
                FeedService feedService = new FeedService();
-               ApiService resp = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "feedId", id);
@@ -159,7 +158,7 @@ public class FeedResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
 
-               Feed nfeed = feedService.getFeed( id, resp.getErr() );
+               Feed nfeed = feedService.getFeed(id, apiError);
                if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
                        return responseBuilder.notFound();
                }
@@ -170,13 +169,13 @@ public class FeedResource extends BaseLoggingClass {
                nfeed.setFeedDescription(feed.getFeedDescription());
                nfeed.setFormatUuid(feed.getFormatUuid());
                
-               nfeed =  feedService.updateFeed(nfeed, resp.getErr());
+               nfeed =  feedService.updateFeed(nfeed, apiError);
                if ( nfeed != null ) {
                        return responseBuilder.success(nfeed);
                } else {
                        logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion());
 
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
        }
        
@@ -189,25 +188,23 @@ public class FeedResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{id}")
-       public Response deleteFeed( 
-                       @PathParam("id") String id
-                       ){
-               ApiService resp = new ApiService();
+       public Response deleteFeed(@PathParam("id") String id){
+               ApiError apiError = new ApiError();
 
                logger.debug( "Entry: DELETE  " + id);
                FeedService feedService = new FeedService();
-               Feed nfeed =  feedService.getFeed( id, resp.getErr() );
+               Feed nfeed =  feedService.getFeed(id, apiError);
                if ( nfeed == null ) {
-                       resp.setCode(Status.NOT_FOUND.getStatusCode());
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                       return responseBuilder.error(apiError);
                }
-               nfeed = feedService.removeFeed( nfeed, resp.getErr() );
+               nfeed = feedService.removeFeed(nfeed, apiError);
                if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) {
                        return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
                }
                logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion());
 
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 
        @GET
@@ -219,16 +216,14 @@ public class FeedResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{id}")
-       public Response getFeed( 
-                       @PathParam("id") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response getFeed(@PathParam("id") String id) {
+               ApiError apiError = new ApiError();
 
                FeedService feedService = new FeedService();
-               Feed nfeed =  feedService.getFeed( id, resp.getErr() );
+               Feed nfeed =  feedService.getFeed(id, apiError);
                if ( nfeed == null ) {
-                       resp.setCode(Status.NOT_FOUND.getStatusCode());
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.NOT_FOUND.getStatusCode());
+                       return responseBuilder.error(apiError);
                }
                return responseBuilder.success(nfeed);
        }
@@ -244,24 +239,22 @@ public class FeedResource extends BaseLoggingClass {
                        @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path( "/sync")
-       public Response syncFeeds ( 
-                       @QueryParam("hard") String hardParam
-                       ) {
-               ApiService resp = new ApiService();
+       public Response syncFeeds (@QueryParam("hard") String hardParam) {
+               ApiError error = new ApiError();
                
                FeedService feedService = new FeedService();
                boolean hard = false;
                if (  hardParam != null && hardParam.equalsIgnoreCase("true")) {
                        hard = true;
                }
-               feedService.sync( hard, resp.getErr() );
-               if ( resp.getErr().is2xx()) {   
+               feedService.sync( hard, error );
+               if ( error.is2xx()) {
                        List<Feed> nfeeds =  feedService.getAllFeeds();
                        GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) {
                        };
                        return responseBuilder.success(list);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(error);
        }
        
 }
index a65007d..6df8ef6 100644 (file)
@@ -45,7 +45,6 @@ import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.MR_Client;
 import org.onap.dmaap.dbcapi.model.MR_Cluster;
 import org.onap.dmaap.dbcapi.model.Topic;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.MR_ClientService;
 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
 import org.onap.dmaap.dbcapi.service.TopicService;
@@ -91,9 +90,8 @@ public class MR_ClientResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = MR_Client.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response addMr_Client( 
-                       MR_Client client) {
-               ApiService resp = new ApiService();
+       public Response addMr_Client(MR_Client client) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "fqtn", client.getFqtn());
@@ -111,38 +109,38 @@ public class MR_ClientResource extends BaseLoggingClass {
                }
                MR_ClusterService clusters = new MR_ClusterService();
 
-               MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr());
+               MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError);
                if ( cluster == null ) {
 
-                       resp.setCode(Status.BAD_REQUEST.getStatusCode());
-                       resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
-                       resp.setFields("dcaeLocationName");
-                       logger.warn( resp.toString() );
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+                       apiError.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName());
+                       apiError.setFields("dcaeLocationName");
+                       logger.warn(apiError.toString());
+                       return responseBuilder.error(apiError);
                }
                String url = cluster.getFqdn();
                if ( url == null || url.isEmpty() ) {
 
-                       resp.setCode(Status.BAD_REQUEST.getStatusCode());
-                       resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
-                       resp.setFields("fqdn");
-                       logger.warn( resp.toString() );
-                       return responseBuilder.error(resp.getErr());
+                       apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+                       apiError.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() );
+                       apiError.setFields("fqdn");
+                       logger.warn(apiError.toString());
+                       return responseBuilder.error(apiError);
                }
                TopicService topics = new TopicService();
 
-               Topic t = topics.getTopic(client.getFqtn(), resp.getErr() );
+               Topic t = topics.getTopic(client.getFqtn(), apiError);
                if ( t == null ) {
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
-               MR_Client nClient =  mr_clientService.addMr_Client(client, t, resp.getErr());
-               if ( resp.getErr().is2xx()) {
-                       t = topics.getTopic(client.getFqtn(),  resp.getErr());
-                       topics.checkForBridge(t, resp.getErr());
+               MR_Client nClient =  mr_clientService.addMr_Client(client, t, apiError);
+               if (apiError.is2xx()) {
+                       t = topics.getTopic(client.getFqtn(), apiError);
+                       topics.checkForBridge(t, apiError);
                        return responseBuilder.success(nClient);
                }
                else {
-                       return responseBuilder.error(resp.getErr());
+                       return responseBuilder.error(apiError);
                }
        }
                
@@ -155,11 +153,8 @@ public class MR_ClientResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{clientId}")
-       public Response updateMr_Client( 
-                       @PathParam("clientId") String clientId, 
-                       MR_Client client
-                       ) {
-               ApiService resp = new ApiService();
+       public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "fqtn", client.getFqtn());
@@ -172,13 +167,13 @@ public class MR_ClientResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                client.setMrClientId(clientId);
-               MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() );
-               if ( resp.getErr().is2xx()) {
+               MR_Client nClient = mr_clientService.updateMr_Client(client, apiError);
+               if (apiError.is2xx()) {
                        return Response.ok(nClient)
                                .build();
                }
-               return Response.status(resp.getErr().getCode())
-                               .entity( resp.getErr() )
+               return Response.status(apiError.getCode())
+                               .entity(apiError)
                                .build();
        }
                
@@ -191,10 +186,8 @@ public class MR_ClientResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{subId}")
-       public Response deleteMr_Client( 
-                       @PathParam("subId") String id
-                       ){
-               ApiService resp = new ApiService();
+       public Response deleteMr_Client(@PathParam("subId") String id){
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "clientId", id);
@@ -202,12 +195,12 @@ public class MR_ClientResource extends BaseLoggingClass {
                        logger.debug( rfe.getApiError().toString() );
                        return responseBuilder.error(rfe.getApiError());
                }
-               mr_clientService.removeMr_Client(id, true, resp.getErr() );
-               if ( resp.getErr().is2xx()) {
+               mr_clientService.removeMr_Client(id, true, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(NO_CONTENT.getStatusCode(), null);
                }
                
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 
        @GET
@@ -219,10 +212,8 @@ public class MR_ClientResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{subId}")
-       public Response test( 
-                       @PathParam("subId") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response test(@PathParam("subId") String id) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "clientId", id);
@@ -230,10 +221,10 @@ public class MR_ClientResource extends BaseLoggingClass {
                        logger.debug( rfe.getApiError().toString() );
                        return responseBuilder.error(rfe.getApiError());
                }
-               MR_Client nClient =  mr_clientService.getMr_Client( id, resp.getErr() );
-               if ( resp.getErr().is2xx()) {
+               MR_Client nClient =  mr_clientService.getMr_Client(id, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(nClient);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 }
index 598fcc2..0a361ff 100644 (file)
@@ -43,7 +43,6 @@ import javax.ws.rs.core.Response.Status;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.MR_Cluster;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.MR_ClusterService;
 
 
@@ -82,9 +81,8 @@ public class MR_ClusterResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response  addMr_Cluster( 
-                       MR_Cluster cluster) {
-               ApiService resp = new ApiService();
+       public Response  addMr_Cluster(MR_Cluster cluster) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "dcaeLocationName", cluster.getDcaeLocationName());
@@ -92,11 +90,11 @@ public class MR_ClusterResource extends BaseLoggingClass {
                } catch( RequiredFieldException rfe ) {
                        return responseBuilder.error(rfe.getApiError());
                }
-               MR_Cluster mrc =  mr_clusterService.addMr_Cluster(cluster, resp.getErr() );
+               MR_Cluster mrc =  mr_clusterService.addMr_Cluster(cluster, apiError);
                if ( mrc != null && mrc.isStatusValid() ) {
                        return responseBuilder.success(Status.CREATED.getStatusCode(), mrc);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
 
        }
                
@@ -109,11 +107,8 @@ public class MR_ClusterResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{clusterId}")
-       public Response updateMr_Cluster( 
-                       @PathParam("clusterId") String clusterId, 
-                       MR_Cluster cluster
-                       ) {
-               ApiService resp = new ApiService();
+       public Response updateMr_Cluster(@PathParam("clusterId") String clusterId, MR_Cluster cluster) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "fqdn", clusterId);
@@ -122,11 +117,11 @@ public class MR_ClusterResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                cluster.setDcaeLocationName(clusterId);
-               MR_Cluster mrc =  mr_clusterService.updateMr_Cluster(cluster, resp.getErr() );
+               MR_Cluster mrc =  mr_clusterService.updateMr_Cluster(cluster, apiError);
                if ( mrc != null && mrc.isStatusValid() ) {
                        return responseBuilder.success(Status.CREATED.getStatusCode(), mrc);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
                
        @DELETE
@@ -138,21 +133,19 @@ public class MR_ClusterResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{clusterId}")
-       public Response deleteMr_Cluster( 
-                       @PathParam("clusterId") String id
-                       ){
-               ApiService resp = new ApiService();
+       public Response deleteMr_Cluster(@PathParam("clusterId") String id){
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "fqdn", id);
                } catch( RequiredFieldException rfe ) {
                        return responseBuilder.error(rfe.getApiError());
                }
-               mr_clusterService.removeMr_Cluster(id, resp.getErr() );
-               if ( resp.getErr().is2xx()) {
+               mr_clusterService.removeMr_Cluster(id, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
                } 
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 
        @GET
@@ -164,20 +157,18 @@ public class MR_ClusterResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{clusterId}")
-       public Response getMR_Cluster( 
-                       @PathParam("clusterId") String id
-                       ) {
-               ApiService resp = new ApiService();
+       public Response getMR_Cluster(@PathParam("clusterId") String id) {
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "dcaeLocationName", id);
                } catch( RequiredFieldException rfe ) {
                        return responseBuilder.error(rfe.getApiError());
                }
-               MR_Cluster mrc =  mr_clusterService.getMr_Cluster( id, resp.getErr() );
+               MR_Cluster mrc =  mr_clusterService.getMr_Cluster(id, apiError);
                if ( mrc != null && mrc.isStatusValid() ) {
                        return responseBuilder.success(Status.CREATED.getStatusCode(), mrc);
                }
-               return responseBuilder.error(resp.getErr());
+               return responseBuilder.error(apiError);
        }
 }
index 96b6a4a..3206cf7 100644 (file)
@@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.ReplicationType;
 import org.onap.dmaap.dbcapi.model.FqtnType;
 import org.onap.dmaap.dbcapi.model.Topic;
-import org.onap.dmaap.dbcapi.service.ApiService;
 import org.onap.dmaap.dbcapi.service.TopicService;
 import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
@@ -102,12 +101,9 @@ public class TopicResource extends BaseLoggingClass {
            @ApiResponse( code = 200, message = "Success", response = Topic.class),
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
-       public Response  addTopic( 
-                       Topic topic,
-                       @QueryParam("useExisting") String useExisting
-                       ) {
+       public Response  addTopic(Topic topic, @QueryParam("useExisting") String useExisting) {
                logger.info( "addTopic request: " + topic  + " useExisting=" + useExisting );
-               ApiService check = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "topicName", topic.getTopicName(), "^\\S+$" );  //no white space allowed in topicName
@@ -141,11 +137,11 @@ public class TopicResource extends BaseLoggingClass {
                        flag = "true".compareToIgnoreCase( useExisting ) == 0;
                }
                
-               Topic mrc =  mr_topicService.addTopic(topic, check.getErr(), flag);
-               if ( mrc != null && check.getErr().is2xx() ) {
+               Topic mrc =  mr_topicService.addTopic(topic, apiError, flag);
+               if ( mrc != null && apiError.is2xx() ) {
                        return responseBuilder.success(CREATED.getStatusCode(), mrc);
                }
-               return responseBuilder.error(check.getErr());
+               return responseBuilder.error(apiError);
        }
        
        @PUT
@@ -157,15 +153,13 @@ public class TopicResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response updateTopic( 
-                       @PathParam("topicId") String topicId
-                       ) {
-               ApiService check = new ApiService();
+       public Response updateTopic(@PathParam("topicId") String topicId) {
+               ApiError apiError = new ApiError();
 
-               check.setCode(Status.BAD_REQUEST.getStatusCode());
-               check.setMessage( "Method /PUT not supported for /topics");
+               apiError.setCode(Status.BAD_REQUEST.getStatusCode());
+               apiError.setMessage( "Method /PUT not supported for /topics");
                
-               return responseBuilder.error(check.getErr());
+               return responseBuilder.error(apiError);
        }
                
        @DELETE
@@ -177,10 +171,8 @@ public class TopicResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response deleteTopic( 
-                       @PathParam("topicId") String id
-                       ){
-               ApiService check = new ApiService();
+       public Response deleteTopic(@PathParam("topicId") String id){
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "fqtn", id);
@@ -189,11 +181,11 @@ public class TopicResource extends BaseLoggingClass {
                        return responseBuilder.error(rfe.getApiError());
                }
                
-               mr_topicService.removeTopic(id, check.getErr());
-               if ( check.getErr().is2xx()) {
+               mr_topicService.removeTopic(id, apiError);
+               if (apiError.is2xx()) {
                        return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null);
                } 
-               return responseBuilder.error(check.getErr());
+               return responseBuilder.error(apiError);
        }
        
 
@@ -206,11 +198,9 @@ public class TopicResource extends BaseLoggingClass {
            @ApiResponse( code = 400, message = "Error", response = ApiError.class )
        })
        @Path("/{topicId}")
-       public Response getTopic( 
-                       @PathParam("topicId") String id
-                       ) {
+       public Response getTopic(@PathParam("topicId") String id) {
                logger.info("Entry: /GET " + id);
-               ApiService check = new ApiService();
+               ApiError apiError = new ApiError();
 
                try {
                        checker.required( "topicName", id, "^\\S+$" );  //no white space allowed in topicName
@@ -218,9 +208,9 @@ public class TopicResource extends BaseLoggingClass {
                        logger.error("Error", rfe.getApiError());
                        return responseBuilder.error(rfe.getApiError());
                }
-               Topic mrc =  mr_topicService.getTopic( id, check.getErr() );
+               Topic mrc =  mr_topicService.getTopic(id, apiError);
                if ( mrc == null ) {
-                       return responseBuilder.error(check.getErr());
+                       return responseBuilder.error(apiError);
                }
                return responseBuilder.success(mrc);
                }
index 7f34725..7457ce9 100644 (file)
@@ -22,7 +22,8 @@
 
 package org.onap.dmaap.dbcapi.server;
 
-
+import com.google.common.collect.Sets;
+import javax.servlet.DispatcherType;
 import org.eclipse.jetty.server.*;
 import org.eclipse.jetty.servlet.DefaultServlet;
 import org.eclipse.jetty.servlet.ServletContextHandler;
@@ -31,6 +32,7 @@ import org.eclipse.jetty.util.ssl.SslContextFactory;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 
 import java.util.Properties;
+
 /**
  * A  Jetty server which supports:
  *     - http and https (simultaneously for dev env)
@@ -38,48 +40,47 @@ import java.util.Properties;
  *  - static html pages (for documentation).
  */
 public class JettyServer extends BaseLoggingClass {
-       private Server server;
 
+    private Server server;
 
-       public Server getServer() {
-               return server;
-       }
 
-    public JettyServer( Properties params ) throws Exception {
+    public Server getServer() {
+        return server;
+    }
+
+    public JettyServer(Properties params) throws Exception {
 
         server = new Server();
-       int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80" ));
-               int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443" ));
-               boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
-       serverLogger.info( "port params: http=" + httpPort + " https=" + sslPort );
-       serverLogger.info( "allowHttp=" + allowHttp );
-
-               // HTTP Server
-       HttpConfiguration http_config = new HttpConfiguration();
-       http_config.setSecureScheme("https");
-       http_config.setSecurePort(sslPort);
-       http_config.setOutputBufferSize(32768);
-
-       
-       
-        try(ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
-                       httpConnector.setPort(httpPort);
-                       httpConnector.setIdleTimeout(30000);
-
-
-                       // HTTPS Server
-
-                       HttpConfiguration https_config = new HttpConfiguration(http_config);
-                       https_config.addCustomizer(new SecureRequestCustomizer());
-                       SslContextFactory sslContextFactory = new SslContextFactory();
-
-                       setUpKeystore(params, sslContextFactory);
-                       setUpTrustStore(params, sslContextFactory);
-
-                       if (sslPort != 0) {
-                try(ServerConnector sslConnector = new ServerConnector(server,
-                                               new SslConnectionFactory(sslContextFactory, "http/1.1"),
-                                               new HttpConnectionFactory(https_config))) {
+        int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80"));
+        int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443"));
+        boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false"));
+        serverLogger.info("port params: http=" + httpPort + " https=" + sslPort);
+        serverLogger.info("allowHttp=" + allowHttp);
+
+        // HTTP Server
+        HttpConfiguration http_config = new HttpConfiguration();
+        http_config.setSecureScheme("https");
+        http_config.setSecurePort(sslPort);
+        http_config.setOutputBufferSize(32768);
+
+        try (ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) {
+            httpConnector.setPort(httpPort);
+            httpConnector.setIdleTimeout(30000);
+
+            // HTTPS Server
+
+            HttpConfiguration https_config = new HttpConfiguration(http_config);
+            https_config.addCustomizer(new SecureRequestCustomizer());
+            SslContextFactory sslContextFactory = new SslContextFactory();
+            sslContextFactory.setWantClientAuth(true);
+
+            setUpKeystore(params, sslContextFactory);
+            setUpTrustStore(params, sslContextFactory);
+
+            if (sslPort != 0) {
+                try (ServerConnector sslConnector = new ServerConnector(server,
+                    new SslConnectionFactory(sslContextFactory, "http/1.1"),
+                    new HttpConnectionFactory(https_config))) {
                     sslConnector.setPort(sslPort);
                     if (allowHttp) {
                         logger.info("Starting httpConnector on port " + httpPort);
@@ -91,62 +92,71 @@ public class JettyServer extends BaseLoggingClass {
                         server.setConnectors(new Connector[]{sslConnector});
                     }
                 }
-                       } else {
-                               serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
-                               if (allowHttp) {
-                                       serverLogger.info("Starting httpConnector on port " + httpPort);
-                                       server.setConnectors(new Connector[]{httpConnector});
-                               }
-                       }
-               }
+            } else {
+                serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https");
+                if (allowHttp) {
+                    serverLogger.info("Starting httpConnector on port " + httpPort);
+                    server.setConnectors(new Connector[]{httpConnector});
+                }
+            }
+        }
 
         // Set context for servlet.  This is shared for http and https
-               ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
-       context.setContextPath("/");
-        server.setHandler( context );
+        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+        context.setContextPath("/");
+        server.setHandler(context);
 
-        ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
+        ServletHolder jerseyServlet = context
+            .addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
         jerseyServlet.setInitOrder(1);
-        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources" );   
-        jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig" );
-        
+        jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources");
+        jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig");
+
         // also serve up some static pages...
-        ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
-        staticServlet.setInitParameter("resourceBase","www");
-        staticServlet.setInitParameter("pathInfoOnly","true");
+        ServletHolder staticServlet = context.addServlet(DefaultServlet.class, "/*");
+        staticServlet.setInitParameter("resourceBase", "www");
+        staticServlet.setInitParameter("pathInfoOnly", "true");
+
+        registerAuthFilters(context);
 
         try {
 
             serverLogger.info("Starting jetty server");
-               String unit_test = params.getProperty("UnitTest", "No");
+            String unit_test = params.getProperty("UnitTest", "No");
             serverLogger.info("UnitTest=" + unit_test);
-                       if ( unit_test.equals( "No" ) ) {
-                       server.start();
-                       server.dumpStdErr();
-               server.join();
-                       }
-        } catch ( Exception e ) {
-               errorLogger.error( "Exception " + e );
+            if (unit_test.equals("No")) {
+                server.start();
+                server.dumpStdErr();
+                server.join();
+            }
+        } catch (Exception e) {
+            errorLogger.error("Exception " + e);
         } finally {
-               server.destroy();
+            server.destroy();
         }
-        
+
+    }
+
+    private void registerAuthFilters(ServletContextHandler context) {
+        context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthenticationFilter", "/webapi/*",
+            Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
+        context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthorizationFilter", "/webapi/*",
+            Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class));
     }
 
-       private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) {
-               String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
-               logger.info("https Server using keystore at " + keystore);
-               sslContextFactory.setKeyStorePath(keystore);
-               sslContextFactory.setKeyStoreType(params.getProperty("KeyStoreType", "jks"));
-               sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit"));
-               sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit"));
-       }
-
-       private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) {
-               String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks");
-               logger.info("https Server using truststore at " + truststore);
-               sslContextFactory.setTrustStorePath(truststore);
-               sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks"));
-               sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit"));
-       }
+    private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) {
+        String keystore = params.getProperty("KeyStoreFile", "etc/keystore");
+        logger.info("https Server using keystore at " + keystore);
+        sslContextFactory.setKeyStorePath(keystore);
+        sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit"));
+        sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit"));
+    }
+
+    private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) {
+        String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks");
+        logger.info("https Server using truststore at " + truststore);
+        sslContextFactory.setTrustStorePath(truststore);
+        sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks"));
+        sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit"));
+    }
 }
index d8ee278..906337a 100644 (file)
@@ -62,7 +62,7 @@ public class Main extends BaseLoggingClass {
             MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getHostName());
             MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress());
         } catch (Exception e) {
-            e.printStackTrace();
+               errorLogger.error("Error while getting hostname or address", e);
         }
         MDC.put(MDC_INSTANCE_UUID, UUID.randomUUID().toString());
         MDC.put(MDC_ALERT_SEVERITY, "0");
index 35e61db..e1beb28 100644 (file)
@@ -100,25 +100,6 @@ public class ApiService extends BaseLoggingClass {
         return err;
     }
 
-
-    public void setErr(ApiError err) {
-        this.err = err;
-    }
-
-       public void setCode(int statusCode) {
-               err.setCode(statusCode);
-       }
-
-
-    public void setMessage(String string) {
-        err.setMessage(string);
-    }
-
-
-       public void setFields(String string) {
-               err.setFields(string);
-       }
-
     public void checkAuthorization() throws Exception {
 
         MDC.put(MDC_KEY_REQUEST_ID, requestId);
index b478dca..9181154 100644 (file)
@@ -215,11 +215,11 @@ public class DR_NodeService extends BaseLoggingClass {
        }
        
        public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) {
-               DR_Node old = dr_nodes.get( node );
+               DR_Node old = dr_nodes.get( node.getFqdn() );
                if ( old == null ) {
                        apiError.setCode(Status.NOT_FOUND.getStatusCode());
                        apiError.setFields( "fqdn");
-                       apiError.setMessage( "Node " + node + " does not exist");
+                       apiError.setMessage( "Node " + node.getFqdn() + " does not exist");
                        return null;
                }
                node.setLastMod();
index da9d822..5d695f4 100644 (file)
@@ -57,6 +57,7 @@ public class MirrorMakerService extends BaseLoggingClass {
        private static String defaultConsumerPort;
        private static String centralFqdn;
        private int maxTopicsPerMM;
+       private boolean mmPerMR;
        
        public MirrorMakerService() {
                super();
@@ -68,6 +69,7 @@ public class MirrorMakerService extends BaseLoggingClass {
                defaultConsumerPort = p.getProperty( "MR.TargetReplicationPort", "2181");       
                centralFqdn = p.getProperty("MR.CentralCname", "notSet");
                maxTopicsPerMM = Integer.valueOf( p.getProperty( "MaxTopicsPerMM", "5"));
+               mmPerMR = "true".equalsIgnoreCase(p.getProperty("MirrorMakerPerMR", "true"));
        }
 
        // will create a MM on MMagent if needed
@@ -79,34 +81,43 @@ public class MirrorMakerService extends BaseLoggingClass {
        
                DmaapService dmaap = new DmaapService();
                MR_ClusterService clusters = new MR_ClusterService();
-       
-               // in 1610, MM should only exist for edge-to-central
-               //  we use a cname for the central MR cluster that is active, and provision on agent topic on that target
-               // but only send 1 message so MM Agents can read it relying on kafka delivery
-               for( MR_Cluster central: clusters.getCentralClusters() ) {
-                       prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn  );
-                       ApiError resp = prov.doPostMessage(mm.createMirrorMaker( defaultConsumerPort, defaultProducerPort ));
-                       if ( ! resp.is2xx() ) {
-       
-                               errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR, "create MM", Integer.toString(resp.getCode()), resp.getMessage());
-                               mm.setStatus(DmaapObject_Status.INVALID);
-                       } else {
-                               prov.makeTopicConnection(central, dmaap.getBridgeAdminFqtn(), centralFqdn );
-                               resp = prov.doPostMessage(mm.getWhitelistUpdateJSON());
-                               if ( ! resp.is2xx()) {
-                                       errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR,"MR Bridge", Integer.toString(resp.getCode()), resp.getMessage());
-                                       mm.setStatus(DmaapObject_Status.INVALID);
-                               } else {
-                                       mm.setStatus(DmaapObject_Status.VALID);
-                               }
-                       }
-                       
-                       // we only want to send one message even if there are multiple central clusters
-                       break;
+               MR_Cluster target_cluster = null;
+               String override = null;
                
-               } 
+               if ( ! mmPerMR ) {
+                       // in ECOMP, MM Agent is only deployed at central, so this case is needed for backwards compatibility
+                       //  we use a cname for the central MR cluster that is active, and provision on agent topic on that target
+                       // but only send 1 message so MM Agents can read it relying on kafka delivery
+                       for( MR_Cluster cluster: clusters.getCentralClusters() ) {
+
+                               target_cluster = cluster;
+                               override = centralFqdn;
+                               // we only want to send one message even if there are multiple central clusters
+                               break;
+                       
+                       } 
+               } else {
+                       // In ONAP deployment architecture, the MM Agent is deployed with each target MR
+                       target_cluster = clusters.getMr_ClusterByFQDN(mm.getTargetCluster());
+                       override = null;
+               }
                
+               prov.makeTopicConnection(target_cluster, dmaap.getBridgeAdminFqtn(), override  );
+               ApiError resp = prov.doPostMessage(mm.createMirrorMaker( defaultConsumerPort, defaultProducerPort ));
+               if ( ! resp.is2xx() ) {
 
+                       errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR, "create MM", Integer.toString(resp.getCode()), resp.getMessage());
+                       mm.setStatus(DmaapObject_Status.INVALID);
+               } else {
+                       prov.makeTopicConnection(target_cluster, dmaap.getBridgeAdminFqtn(), override );
+                       resp = prov.doPostMessage(mm.getWhitelistUpdateJSON());
+                       if ( ! resp.is2xx()) {
+                               errorLogger.error( DmaapbcLogMessageEnum.MM_PUBLISH_ERROR,"MR Bridge", Integer.toString(resp.getCode()), resp.getMessage());
+                               mm.setStatus(DmaapObject_Status.INVALID);
+                       } else {
+                               mm.setStatus(DmaapObject_Status.VALID);
+                       }
+               }
 
                mm.setLastMod();
                return mirrors.put( mm.getMmName(), mm);
index c5937f4..a7991e8 100644 (file)
@@ -70,6 +70,7 @@ public class TopicService extends BaseLoggingClass {
        private static String centralCname;
        private static boolean createTopicRoles;
        private boolean strictGraph = true;
+       private boolean mmPerMR;
 
 
        public TopicService(){
@@ -81,9 +82,11 @@ public class TopicService extends BaseLoggingClass {
                if ( unit_test.equals( "Yes" ) ) {
                        strictGraph = false;
                }
+               mmPerMR = "true".equalsIgnoreCase(p.getProperty("MirrorMakerPerMR", "true"));
                logger.info( "TopicService properties: CentralCname=" + centralCname + 
                                "   defaultGlobarlMrHost=" + defaultGlobalMrHost +
-                               " createTopicRoles=" + createTopicRoles );
+                               " createTopicRoles=" + createTopicRoles +
+                               " mmPerMR=" + mmPerMR );
        }
        
        public Map<String, Topic> getTopics() {                 
@@ -451,11 +454,11 @@ public class TopicService extends BaseLoggingClass {
                                case REPLICATION_EDGE_TO_CENTRAL:
                                case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL:  // NOTE: this is for E2C portion only
                                        source = cluster.getFqdn();
-                                       target = centralCname;
+                                       target = (mmPerMR)? groupCentralCluster.getFqdn() : centralCname;
                                        break;
                                case REPLICATION_CENTRAL_TO_EDGE:
                                case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE:  // NOTE: this is for C2E portion only
-                                       source = centralCname;
+                                       source = (mmPerMR) ? groupCentralCluster.getFqdn() : centralCname;
                                        target = cluster.getFqdn();
                                        break;
                                case REPLICATION_CENTRAL_TO_GLOBAL:
diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java b/src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java
new file mode 100644 (file)
index 0000000..44c94af
--- /dev/null
@@ -0,0 +1,86 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.util;
+
+import javax.servlet.http.HttpServletRequest;
+import org.onap.dmaap.dbcapi.model.Dmaap;
+import org.onap.dmaap.dbcapi.service.DmaapService;
+
+public class PermissionBuilder {
+
+    static final String API_NS_PROP = "ApiNamespace";
+    static final String DEFAULT_API_NS = "org.onap.dmaap-bc.api";
+    static final String BOOT_INSTANCE = "boot";
+    private static final String PERM_SEPARATOR = "|";
+    private static final String NS_SEPARATOR = ".";
+    private DmaapConfig dmaapConfig;
+    private DmaapService dmaapService;
+    private String instance;
+    private String apiNamespace;
+
+    public PermissionBuilder(DmaapConfig dmaapConfig, DmaapService dmaapService) {
+        this.dmaapConfig = dmaapConfig;
+        this.dmaapService = dmaapService;
+        initFields();
+    }
+
+    public synchronized void updateDmaapInstance() {
+        if(instance == null || instance.isEmpty() || instance.equalsIgnoreCase(BOOT_INSTANCE)) {
+            String dmaapName = getDmaapName();
+            instance = (dmaapName == null || dmaapName.isEmpty()) ? BOOT_INSTANCE : dmaapName;
+        }
+    }
+
+    public String buildPermission(HttpServletRequest httpRequest) {
+
+        StringBuilder sb = new StringBuilder(apiNamespace);
+        sb.append(NS_SEPARATOR)
+            .append(getPermissionType(httpRequest.getPathInfo()))
+            .append(PERM_SEPARATOR)
+            .append(instance)
+            .append(PERM_SEPARATOR)
+            .append(httpRequest.getMethod());
+        return sb.toString();
+    }
+
+
+    private void initFields() {
+        apiNamespace = dmaapConfig.getProperty(API_NS_PROP, DEFAULT_API_NS);
+        updateDmaapInstance();
+    }
+
+    private String getDmaapName() {
+        Dmaap dmaap = dmaapService.getDmaap();
+        return ( dmaap != null ) ? dmaap.getDmaapName() : BOOT_INSTANCE;
+    }
+
+    private String getPermissionType(String pathInfo) {
+        char pathSeparator = '/';
+        String relativePath = (pathInfo.charAt(pathInfo.length()-1) == pathSeparator) ?
+            pathInfo.substring(0,pathInfo.length()-1) : pathInfo;
+
+        String[] pathSlices = relativePath.split(String.valueOf(pathSeparator));
+        return pathSlices[pathSlices.length-1];
+    }
+
+    String getInstance() {
+        return instance;
+    }
+}
index 22f0559..c4cef5f 100644 (file)
@@ -24,7 +24,7 @@
     add column  guaranteed_delivery     boolean,
     add column  guaranteed_sequence     boolean,
     add column  privileged_subscriber    boolean,
-    add column  decompress_data          boolean
+    add column  decompress                      boolean
 ;
 
 
index c1e9c35..5171aaa 100644 (file)
@@ -1,10 +1,11 @@
-
 /*-
  * ============LICENSE_START=======================================================
  * org.onap.dmaap
  * ================================================================================
  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 IBM
+ * ===================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.dmaap.dbcapi.database;
 
-import org.onap.dmaap.dbcapi.database.DBFieldHandler;
-import org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.testframework.ReflectionHarness;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 
-import static org.junit.Assert.*;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 
-import org.junit.After;
-import org.junit.Before;
+import org.apache.log4j.Logger;
 import org.junit.Test;
-import java.util.*;
-import java.sql.*;
+import org.onap.dmaap.dbcapi.authentication.AafLurAndFish;
+import org.onap.dmaap.dbcapi.model.ReplicationType;
+import org.onap.dmaap.dbcapi.testframework.ReflectionHarness;
 
 public class DBFieldHandlerTest {
 
-       private static final String  fmt = "%24s: %s%n";
+    private static final Logger logger = Logger.getLogger(AafLurAndFish.class);
 
-       ReflectionHarness rh = new ReflectionHarness();
+    private static final String fmt = "%24s: %s%n";
 
-   private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
+    ReflectionHarness rh = new ReflectionHarness();
+
+    private static class TopicReplicationTypeHandler implements DBFieldHandler.SqlOp {
         public Object get(ResultSet rs, int index) throws Exception {
             int val = rs.getInt(index);
 
             return (ReplicationType.valueOf(val));
         }
+
         public void set(PreparedStatement ps, int index, Object val) throws Exception {
             if (val == null) {
                 ps.setInt(index, 0);
@@ -55,53 +60,55 @@ public class DBFieldHandlerTest {
         }
     }
 
+    @Test
+    public void test1() {
+        // rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "get",
+        // "idNotSet@namespaceNotSet:pwdNotSet" );
+    }
 
+    @Test
+    public void test2() {
+        String v = "Validate";
+        // rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "set", v );
+    }
 
-       @Before
-       public void setUp() throws Exception {
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-
-
-       @Test
-       public void test1() {
-
-
-               //rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "get", "idNotSet@namespaceNotSet:pwdNotSet" );      
-       
-       }
-
-       @Test
-       public void test2() {
-               String v = "Validate";
-               //rh.reflect( "org.onap.dmaap.dbcapi.aaf.client.MrTopicConnection", "set", v );
-
-       }
-
-       @Test
-       public void test3() {
-
-               try {
-                       DBFieldHandler fh = new DBFieldHandler( String.class, "aString", 1 );
-               } catch (Exception e ) {
-               }
-
-       }
-
-       @Test
-       public void test4() {
-
-               try {
-                       DBFieldHandler fh = new DBFieldHandler( String.class, "aString", 1, null );
-               } catch (Exception e ) {
-               }
+    @Test
+    public void test3() {
+        try {
+            DBFieldHandler fh = new DBFieldHandler(String.class, "aString", 1);
+        } catch (Exception e) {
+            logger.error("Error", e);
+        }
+    }
 
-       }
+    @Test
+    public void test4() {
+        try {
+            DBFieldHandler fh = new DBFieldHandler(String.class, "aString", 1, null);
+        } catch (Exception e) {
+            logger.error("Error", e);
+        }
+    }
 
+    @Test
+    public void testfesc() {
+        String sampleString = "@xyz,ww;,";
+        String finalString = DBFieldHandler.fesc(sampleString);
+        assertEquals("@axyz@cww@s@c", finalString);
+    }
 
+    @Test
+    public void testfunesc() {
+        String sampleString = "@axyz@cww@s@c";
+        String convertedString = DBFieldHandler.funesc(sampleString);
+        assertEquals("@xyz,ww;,", convertedString);
+    }
 
+    @Test
+    public void testfescWithNull() {
+        String sampleString1 = DBFieldHandler.fesc(null);
+        String sampleString2 = DBFieldHandler.funesc(null);
+        assertNull(null, sampleString1);
+        assertNull(null, sampleString2);
+    }
 }
-
index 59a4023..95cf9c9 100644 (file)
@@ -69,7 +69,7 @@ public class DRSubTest {
                assertTrue( t.isGuaranteedDelivery() == false );
                assertTrue( t.isGuaranteedSequence() == false );
                assertTrue( t.isPrivilegedSubscriber() == false );
-               assertTrue( t.isDecompressData() == false );
+               assertTrue( t.isDecompress() == false );
        }
 
        @Test
@@ -113,8 +113,8 @@ public class DRSubTest {
                assertTrue( t.isGuaranteedSequence() == v );
                t.setPrivilegedSubscriber(v);
                assertTrue( t.isPrivilegedSubscriber() == v );
-               t.setDecompressData(v);
-               assertTrue( t.isDecompressData() == v );
+               t.setDecompress(v);
+               assertTrue( t.isDecompress() == v );
        }
 
        @Test
index 5180b99..5da3aed 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 IBM
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  */
 package org.onap.dmaap.dbcapi.model;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
 import org.onap.dmaap.dbcapi.testframework.ReflectionHarness;
 
-
 public class TopicTest {
 
-       private static final String  fmt = "%24s: %s%n";
-
-       ReflectionHarness rh = new ReflectionHarness();
-
-       String f, t, d, e, o;
-
-       @Before
-       public void setUp() throws Exception {
-               f = "org.onap.dmaap.interestingTopic";
-               t = "interestingTopic";
-               d = "A so very interesting topic";
-               e = "Yes";
-               o = "m12345";
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-
-
-       @Test
-       public void test1() {
-
-
-               rh.reflect( "org.onap.dmaap.dbcapi.model.Topic", "get", null ); 
-       
-       }
-
-       @Test
-       public void test2() {
-               String[] a = { "put", "view" };
-               Topic obj = new Topic( f, t, d, e, o );
-
-
-               assertTrue( f.equals( obj.getFqtn() ));
-               assertTrue( t.equals( obj.getTopicName() ));
-               assertTrue( d.equals( obj.getTopicDescription() ));
-               assertTrue( e.equals( obj.getTnxEnabled() ));
-               assertTrue( o.equals( obj.getOwner() ));
-       }
-
-       @Test
-       public void test3() {
-
-               String v = "Validate";
-               rh.reflect( "org.onap.dmaap.dbcapi.model.Topic", "set", v );
-       }
+    ReflectionHarness rh = new ReflectionHarness();
+
+    String f, t, d, e, o;
+
+    @Before
+    public void setUp() throws Exception {
+        f = "org.onap.dmaap.interestingTopic";
+        t = "interestingTopic";
+        d = "A so very interesting topic";
+        e = "Yes";
+        o = "m12345";
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+
+    @Test
+    public void test1() {
+        rh.reflect("org.onap.dmaap.dbcapi.model.Topic", "get", null);
+    }
+
+    @Test
+    public void test2() {
+        Topic obj = new Topic(f, t, d, e, o);
+        assertTrue(f.equals(obj.getFqtn()));
+        assertTrue(t.equals(obj.getTopicName()));
+        assertTrue(d.equals(obj.getTopicDescription()));
+        assertTrue(e.equals(obj.getTnxEnabled()));
+        assertTrue(o.equals(obj.getOwner()));
+    }
+
+    @Test
+    public void test3() {
+        String v = "Validate";
+        rh.reflect("org.onap.dmaap.dbcapi.model.Topic", "set", v);
+    }
+
+    @Test
+    public void getNumClientsHavingMRClientListNull() {
+        Topic obj = new Topic(f, t, d, e, o);
+        obj.setClients(null);
+        assertEquals(0, obj.getNumClients());
+    }
+
+    @Test
+    public void testTopicInitializationWithInvalidJsonString() {
+        String json = "{\"key\":\"value\"";
+        Topic obj = new Topic(json);
+        assertEquals(DmaapObject_Status.INVALID, obj.getStatus());
+    }
 
 }
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java
new file mode 100644 (file)
index 0000000..d5ae5fd
--- /dev/null
@@ -0,0 +1,178 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.aaf.cadi.filter.CadiFilter;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AAFAuthenticationFilterTest {
+
+    @Spy
+    private AAFAuthenticationFilter filter;
+    @Mock
+    private FilterConfig filterConfig;
+    @Mock
+    private CadiFilter cadiFilterMock;
+    @Mock
+    private HttpServletRequest servletRequest;
+    @Mock
+    private HttpServletResponse servletResponse;
+    @Mock
+    private FilterChain filterChain;
+    @Mock
+    private DmaapConfig dmaapConfig;
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    @Before
+    public void setUp() throws Exception {
+        doReturn(dmaapConfig).when(filter).getConfig();
+    }
+
+    @Test
+    public void init_shouldNotInitializeCADI_whenAafIsNotUsed() throws Exception {
+        //given
+        doReturn("false").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
+
+        //when
+        filter.init(filterConfig);
+
+        //then
+        assertFalse(filter.isAafEnabled());
+        assertNull(filter.getCadiFilter());
+    }
+
+    @Test
+    public void doFilter_shouldSkipCADI_whenAafIsNotUsed() throws Exception {
+        //given
+        doReturn("false").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
+        filter.init(filterConfig);
+        filter.setCadiFilter(cadiFilterMock);
+
+        //when
+        filter.doFilter(servletRequest, servletResponse, filterChain);
+
+        //then
+        verify(filterChain).doFilter(servletRequest,servletResponse);
+        verifyZeroInteractions(cadiFilterMock,servletRequest,servletResponse);
+    }
+
+    @Test
+    public void init_shouldFail_whenAafIsUsed_andCadiPropertiesHasNotBeenSet() throws Exception {
+        //given
+        doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
+        doReturn("").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
+
+        //then
+        thrown.expect(ServletException.class);
+        thrown.expectMessage("Cannot initialize CADI filter.CADI properties not available.");
+
+        //when
+        filter.init(filterConfig);
+    }
+
+    @Test
+    public void init_shouldInitializeCADI_whenAafIsUsed_andCadiPropertiesSet() throws Exception {
+        //given
+        doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
+        doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
+
+        //when
+        filter.init(filterConfig);
+
+        //then
+        assertTrue(filter.isAafEnabled());
+        assertNotNull(filter.getCadiFilter());
+    }
+
+    @Test
+    public void doFilter_shouldUseCADIfilter_andAuthenticateUser_whenAAFisUsed_andUserIsValid() throws Exception{
+        //given
+        initCADIFilter();
+        doReturn(200).when(servletResponse).getStatus();
+
+        //when
+        filter.doFilter(servletRequest,servletResponse,filterChain);
+
+        //then
+        verify(cadiFilterMock).doFilter(servletRequest,servletResponse,filterChain);
+        verify(servletResponse).getStatus();
+        verifyNoMoreInteractions(servletResponse);
+        verifyZeroInteractions(filterChain, servletRequest);
+    }
+
+    @Test
+    public void doFilter_shouldUseCADIfilter_andReturnAuthenticationError_whenAAFisUsed_andUserInvalid() throws Exception{
+        //given
+        String errorResponseJson = "{\"code\":401,\"message\":\"invalid or no credentials provided\",\"fields\":\"Authentication\",\"2xx\":false}";
+        initCADIFilter();
+        doReturn(401).when(servletResponse).getStatus();
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        doReturn(pw).when(servletResponse).getWriter();
+
+        //when
+        filter.doFilter(servletRequest,servletResponse,filterChain);
+
+        //then
+        verify(cadiFilterMock).doFilter(servletRequest,servletResponse,filterChain);
+        verify(servletResponse).getStatus();
+        verify(servletResponse).setContentType("application/json");
+        verifyZeroInteractions(filterChain, servletRequest);
+        assertEquals(errorResponseJson, sw.toString());
+    }
+
+    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);
+        filter.init(filterConfig);
+        filter.setCadiFilter(cadiFilterMock);
+    }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilterTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilterTest.java
new file mode 100644 (file)
index 0000000..73794cd
--- /dev/null
@@ -0,0 +1,172 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.dmaap.dbcapi.model.Dmaap;
+import org.onap.dmaap.dbcapi.service.DmaapService;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+import org.onap.dmaap.dbcapi.util.PermissionBuilder;
+import sun.security.acl.PrincipalImpl;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AAFAuthorizationFilterTest {
+
+    @Spy
+    private AAFAuthorizationFilter filter;
+    @Mock
+    private FilterConfig filterConfig;
+    @Mock
+    private HttpServletRequest servletRequest;
+    @Mock
+    private HttpServletResponse servletResponse;
+    @Mock
+    private FilterChain filterChain;
+    @Mock
+    private DmaapConfig dmaapConfig;
+    @Mock
+    private PermissionBuilder permissionBuilder;
+    @Mock
+    private DmaapService dmaapService;
+
+    @Before
+    public void setUp() throws Exception {
+        filter.setPermissionBuilder(permissionBuilder);
+        doReturn(dmaapConfig).when(filter).getConfig();
+        doReturn(dmaapService).when(filter).getDmaapService();
+    }
+
+    @Test
+    public void init_shouldNotInitializePermissionBuilder_whenAAFnotUsed() throws Exception {
+        //given
+        filter.setPermissionBuilder(null);
+        configureAAFUsage(false);
+
+        //when
+        filter.init(filterConfig);
+
+        //then
+        assertNull(filter.getPermissionBuilder());
+    }
+
+    @Test
+    public void init_shouldInitializePermissionBuilder_whenAAFisUsed() throws Exception {
+        //given
+        filter.setPermissionBuilder(null);
+        configureAAFUsage(true);
+        //doReturn(provideEmptyInstance()).when(dmaapService).getDmaap();
+        when(dmaapService.getDmaap()).thenReturn(mock(Dmaap.class));
+
+        //when
+        filter.init(filterConfig);
+
+        //then
+        assertNotNull(permissionBuilder);
+    }
+
+    @Test
+    public void doFilter_shouldSkipAuthorization_whenAAFnotUsed() throws Exception {
+        //given
+        filter.setAafEnabled(false);
+
+        //when
+        filter.doFilter(servletRequest,servletResponse,filterChain);
+
+        //then
+        verify(filterChain).doFilter(servletRequest,servletResponse);
+        verifyNoMoreInteractions(filterChain);
+        verifyZeroInteractions(permissionBuilder, servletRequest, servletResponse);
+    }
+
+    @Test
+    public void doFilter_shouldPass_whenUserHasPermissionToResourceEndpoint() throws Exception {
+        //given
+        String user = "johnny";
+        String permission = "org.onap.dmaap-bc.api.topics|mr|GET";
+        when(permissionBuilder.buildPermission(servletRequest)).thenReturn(permission);
+        configureServletRequest(permission, user, true);
+        filter.setAafEnabled(true);
+
+        //when
+        filter.doFilter(servletRequest,servletResponse,filterChain);
+
+        //then
+        verify(filterChain).doFilter(servletRequest,servletResponse);
+        verify(permissionBuilder).updateDmaapInstance();
+        verifyZeroInteractions(servletResponse);
+    }
+
+    @Test
+    public void doFilter_shouldReturnError_whenUserDontHavePermissionToResourceEndpoint() throws Exception {
+        //given
+        String user = "jack";
+        String permission = "org.onap.dmaap-bc.api.topics|mr|GET";
+        when(permissionBuilder.buildPermission(servletRequest)).thenReturn(permission);
+        configureServletRequest(permission, user, false);
+        filter.setAafEnabled(true);
+
+        String errorMsgJson = "{\"code\":403,\"message\":\"User "+user+" does not have permission "
+            + permission +"\",\"fields\":\"Authorization\",\"2xx\":false}";
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        when(servletResponse.getWriter()).thenReturn(pw);
+
+        //when
+        filter.doFilter(servletRequest,servletResponse,filterChain);
+
+        //then
+        verifyZeroInteractions(filterChain);
+        verify(permissionBuilder).updateDmaapInstance();
+        verify(servletResponse).setStatus(403);
+        assertEquals(errorMsgJson, sw.toString());
+    }
+
+    private void configureServletRequest(String permission, String user, boolean isUserInRole) {
+        when(servletRequest.getUserPrincipal()).thenReturn(new PrincipalImpl(user));
+        when(servletRequest.isUserInRole(permission)).thenReturn(isUserInRole);
+    }
+
+    private void configureAAFUsage(Boolean isUsed) {
+        doReturn(isUsed.toString()).when(dmaapConfig).getProperty(eq(AAFAuthorizationFilter.AAF_AUTHZ_FLAG), anyString());
+    }
+}
\ No newline at end of file
index 01ef6ae..856a789 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  */
 package org.onap.dmaap.dbcapi.resources;
 
-import org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.service.*;
-import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
-
-import static org.junit.Assert.*;
-
-import org.junit.After;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
-import java.util.*;
-import java.sql.*;
+import org.onap.dmaap.dbcapi.database.DatabaseClass;
+import org.onap.dmaap.dbcapi.model.ApiError;
+import org.onap.dmaap.dbcapi.model.DR_Node;
+import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
 
-import org.glassfish.jersey.test.JerseyTest;
-import org.glassfish.jersey.server.ResourceConfig;
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.Path;
-import javax.ws.rs.GET;
-
-
-public class DR_NodeResourceTest extends JerseyTest {
-
-       static DmaapObjectFactory factory = new DmaapObjectFactory();
-       static String entry_path = "dr_nodes";
-
-       @Override
-       protected Application configure() {
-               return new ResourceConfig( DR_NodeResource.class );
-       }
-
-       private static final String  fmt = "%24s: %s%n";
-
-
-
-/*  may conflict with test framework! 
-       @Before
-       public void preTest() throws Exception {
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-*/
-
-
-       @Test
-       public void GetTest() {
-               Response resp = target( entry_path ).request().get( Response.class );
-               System.out.println( "GET " + entry_path + " resp=" + resp.getStatus() );
-
-               assertTrue( resp.getStatus() == 200 );
-       }
-       @Test
-       public void PostTest() {
-               DR_Node node = factory.genDR_Node( "central" );
-               Entity<DR_Node> reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-               Response resp = target( entry_path ).request().post( reqEntity, Response.class );
-               System.out.println( "POST " + entry_path + " resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
-               assertTrue( resp.getStatus() == 200 );
-       }
-
-       @Test
-       public void PutTest() {
-
-/*
-               try {
-                       DcaeLocation loc = factory.genDcaeLocation( "central" );
-                       Entity<DcaeLocation> reqEntity = Entity.entity( loc, MediaType.APPLICATION_JSON );
-                       Response resp = target( "dcaeLocations").request().post( reqEntity, Response.class );
-                       System.out.println( "POST dcaeLocation resp=" + resp.getStatus() + " " + resp.readEntity( String.class ));
-                       assertTrue( resp.getStatus() == 201 );
-               } catch (Exception e ) {
-               }
-*/
-
-               DR_Node node = factory.genDR_Node( "central" );
-               Entity<DR_Node> reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-               Response resp = target( entry_path ).request().post( reqEntity, Response.class );
-
-               // first, add it 
-               System.out.println( "POST " + entry_path + " resp=" + resp.getStatus() + " " + resp.readEntity( String.class ) );
-               assertTrue( resp.getStatus() == 200 );
-
-               // now change a field
-               node.setVersion( "1.0.2" );
-               reqEntity = Entity.entity( node, MediaType.APPLICATION_JSON );
-
-               // update  currently fails...
-               resp = target( entry_path )
-                                       .path( node.getFqdn())
-                                       .request()
-                                       .put( reqEntity, Response.class );
-               System.out.println( "PUT " + entry_path + "/" + node.getFqdn() + " resp=" + resp.getStatus() + " " + resp.readEntity(String.class));
-               assertTrue( resp.getStatus() == 404 );
-
-       }
-
-
 
+import static javax.ws.rs.client.Entity.entity;
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+
+public class DR_NodeResourceTest {
+
+    private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
+    private static FastJerseyTestContainer testContainer;
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+        DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
+
+        testContainer = new FastJerseyTestContainer(new ResourceConfig()
+                .register(DR_NodeResource.class));
+        testContainer.init();
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+        testContainer.destroy();
+        /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
+
+        DatabaseClass.clearDatabase();
+        DatabaseClass.getDmaap().remove();*/
+    }
+
+    @Test
+    public void getDr_Nodes_test() {
+        Response response = testContainer.target("dr_nodes").request().get(Response.class);
+        System.out.println("GET dr_subs response=" + response.getStatus());
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+    }
+
+    @Test
+    public void addDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
+        DR_Node node = new DR_Node(null, null, "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .request()
+                .post(requestedEntity, Response.class);
+
+        assertEquals(400, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("dcaeLocation, fqdn", responseError.getFields());
+    }
+
+    @Test
+    public void addDr_Node_shouldReturnError_whenDrNodeWithGiveFqdnAlreadyExists() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        addDrNode(node);
+        Response response = addDrNode(node);
+
+        assertEquals(409, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn already exists", responseError.getMessage());
+    }
+
+    @Test
+    public void addDr_Node_shouldExecuteSuccessfully() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        Response response = addDrNode(node);
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertDrNodeExistInDB(response.readEntity(DR_Node.class));
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenNoLocationAndFqdnProvided() {
+        DR_Node node = new DR_Node(null, null, "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(400, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("dcaeLocation, fqdn", responseError.getFields());
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path("")
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(405, response.getStatus());
+    }
+
+    @Test
+    public void updateDr_Node_shouldReturnError_whenDrNodeForUpdateDoesNotExistInDb() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(node, APPLICATION_JSON);
+
+        Response response = testContainer.target("dr_nodes")
+                .path(node.getFqdn())
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node " + node.getFqdn() + " does not exist", responseError.getMessage());
+    }
+
+    @Test
+    public void updateDr_Node_ShouldExecuteSuccessfully() {
+        DR_Node toUpdate = new DR_Node("fqdn", "location", "hostName", "1.0");
+        Entity<DR_Node> requestedEntity = entity(toUpdate, APPLICATION_JSON);
+
+        addDrNode(new DR_Node("fqdn", "old_location", "old_hostName", "old_1.0"));
+        Response response = testContainer.target("dr_nodes")
+                .path(toUpdate.getFqdn())
+                .request()
+                .put(requestedEntity, Response.class);
+
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertEquals(toUpdate, response.readEntity(DR_Node.class));
+    }
+
+    @Test
+    public void deleteDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .delete();
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn does not exist", responseError.getMessage());
+    }
+
+    @Test
+    public void deleteDr_Node_shouldReturnError_whenNoExistingFqdnProvided() {
+        Response response = testContainer.target("dr_nodes")
+                .path("")
+                .request()
+                .delete();
+
+        assertEquals(405, response.getStatus());
+    }
+
+    @Test
+    public void deleteDr_Node_shouldExecuteSuccessfully() {
+        DR_Node node = new DR_Node("fqdn", "location", "hostName", "1.0");
+
+        addDrNode(node);
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .delete();
+
+        assertEquals(204, response.getStatus());
+    }
+
+    @Test
+    public void getDr_Node_shouldReturnError_whenDrNodeForDeleteDoesNotExistInDb() {
+        Response response = testContainer.target("dr_nodes")
+                .path("fqdn")
+                .request()
+                .get();
+
+        assertEquals(404, response.getStatus());
+        ApiError responseError = response.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("fqdn", responseError.getFields());
+        assertEquals("Node fqdn does not exist", responseError.getMessage());
+    }
+
+    private Response addDrNode(DR_Node node) {
+        return testContainer.target("dr_nodes")
+                .request()
+                .post(entity(node, APPLICATION_JSON), Response.class);
+    }
+
+    private void assertDrNodeExistInDB(DR_Node created) {
+        Response response = testContainer.target("dr_nodes")
+                .path(created.getFqdn())
+                .request()
+                .get();
+        assertEquals(200, response.getStatus());
+        assertTrue(response.hasEntity());
+        assertEquals(created, response.readEntity(DR_Node.class));
+    }
+
+    @Before
+    public void cleanupDatabase() {
+        DatabaseClass.clearDatabase();
+    }
 
 }
 
index d4f71bb..bf03088 100644 (file)
@@ -8,9 +8,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * ============LICENSE_END=========================================================
  */
 package org.onap.dmaap.dbcapi.resources;
-import org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.service.*;
-import static org.junit.Assert.*;
 
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import java.util.*;
-import java.sql.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
-import org.glassfish.jersey.test.JerseyTest;
-import org.glassfish.jersey.server.ResourceConfig;
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
-import javax.ws.rs.core.Response;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.Path;
-import javax.ws.rs.GET;
-
-public class DR_PubResourceTest extends JerseyTest{
-
-       @Override
-       protected Application configure() {
-               return new ResourceConfig()
-                               .register( DR_PubResource.class )
-                               .register( FeedResource.class );
-       }
-
-       private static final String  fmt = "%24s: %s%n";
-       String d, un, up, f, p;
-/*
-       @Before
-       public void setUp() throws Exception {
-               d = "central-onap";
-               un = "user1";
-               up = "secretW0rd";
-               f = "234";
-               p = "678";
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-*/
-
-
-
-/*  may conflict with test framework! 
-       @Before
-       public void setUp() throws Exception {
-       }
-
-       @After
-       public void tearDown() throws Exception {
-       }
-*/
-
-       private Feed addFeed( String name, String desc ) {
-               Feed feed = new Feed( name, "1.0", desc, "dgl", "unrestricted" );
-               Entity<Feed> reqEntity = Entity.entity( feed, MediaType.APPLICATION_JSON );
-               Response resp = target( "feeds").request().post( reqEntity, Response.class );
-               int rc = resp.getStatus();
-               System.out.println( "POST feed resp=" + rc );
-               assertTrue( rc == 200 || rc == 409 );
-               feed = resp.readEntity( Feed.class );
-               return feed;
-       }
-       
-       private DR_Pub addPub( String d, String un, String up, String feedId ) {
-               DR_Pub dr_pub = new DR_Pub( d, un, up, feedId, "" );
-               Entity<DR_Pub> reqEntity2 = Entity.entity( dr_pub, MediaType.APPLICATION_JSON);
-               Response resp = target( "dr_pubs").request().post( reqEntity2, Response.class);
-               System.out.println( "POST dr_pubs resp=" + resp.getStatus() );
-               assertTrue( resp.getStatus() == 201 );
-               dr_pub = resp.readEntity( DR_Pub.class );
-               
-               return dr_pub;
-       }
-       
-       private DR_Pub addPubByName( String d, String un, String up, String feedName) {
-               DR_Pub dr_pub = new DR_Pub( d, un, up, null, "" );
-               dr_pub.setFeedName(feedName);
-               Entity<DR_Pub> reqEntity2 = Entity.entity( dr_pub, MediaType.APPLICATION_JSON);
-               Response resp = target( "dr_pubs").request().post( reqEntity2, Response.class);
-               System.out.println( "POST dr_pubs resp=" + resp.getStatus() );
-               assertTrue( resp.getStatus() == 201 );
-               dr_pub = resp.readEntity( DR_Pub.class );
-               
-               return dr_pub;
-       }
-       
-       @Test
-       public void GetTest() {
-               Response resp = target( "dr_pubs").request().get( Response.class );
-               System.out.println( "GET dr_pubs resp=" + resp.getStatus() );
-
-               assertTrue( resp.getStatus() == 200 );
-       }
-       
-       @Test
-       public void PostTest() {
-
-               Feed feed = addFeed( "pubPostTest", "post unit test" );
-               System.out.println( "fpubPostTest: feedId=" + feed.getFeedId());
-               
-               String d, un, up;
-               d = "central-onap";
-               un = "user1";
-               up = "secretW0rd";
-
-               DR_Pub dr_pub = addPub( d, un, up, feed.getFeedId() );
-       }
-       
-       @Test
-       public void PostTestByName() {
-
-               Feed feed = addFeed( "pubPostTest2", "post unit test" );
-               System.out.println( "fpubPostTest: feedId=" + feed.getFeedId());
-               
-               String d, un, up;
-               d = "central-onap";
-               un = "user1";
-               up = "secretW0rd";
-
-               DR_Pub dr_pub = addPubByName( d, un, up, "pubPostTest2" );      
-       }
-
-       @Test
-       public void PutTest() {
-
-               Feed feed = addFeed( "pubPutTest", "put unit test");
-               String d, un, up;
-               d = "central-onap";
-               un = "user1";
-               up = "secretW0rd";
-
-               DR_Pub dr_pub = addPub( d, un, up, feed.getFeedId() );
-               
-               dr_pub.setUserpwd("newSecret");
-               Entity<DR_Pub> reqEntity2 = Entity.entity( dr_pub, MediaType.APPLICATION_JSON);
-               Response resp = target( "dr_pubs")
-                               .path( dr_pub.getPubId() )
-                               .request()
-                               .put( reqEntity2, Response.class);
-               System.out.println( "PUT dr_pubs resp=" + resp.getStatus() );
-               assertTrue( resp.getStatus() == 200 );
-       }
-               
+import javax.ws.rs.core.Response;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.dmaap.dbcapi.database.DatabaseClass;
+import org.onap.dmaap.dbcapi.model.ApiError;
+import org.onap.dmaap.dbcapi.model.DR_Pub;
+import org.onap.dmaap.dbcapi.model.Feed;
+import org.onap.dmaap.dbcapi.testframework.DmaapObjectFactory;
+
+public class DR_PubResourceTest {
+
+    private static final DmaapObjectFactory DMAAP_OBJECT_FACTORY = new DmaapObjectFactory();
+
+    private static final String DCAE_LOCATION_NAME = "central-onap";
+    private static final String USERNAME = "user1";
+    private static final String USRPWD = "secretW0rd";
+    private static final String FEED_ID = "someFakeFeedId";
+    private static final String PUB_ID = "0";
+    private static FastJerseyTestContainer testContainer;
+    private static TestFeedCreator testFeedCreator;
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+        //TODO: init is still needed here to assure that dmaap is not null
+        DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
+
+        testContainer = new FastJerseyTestContainer(new ResourceConfig()
+            .register(DR_PubResource.class)
+            .register(FeedResource.class));
+
+        testContainer.init();
+        testFeedCreator = new TestFeedCreator(testContainer);
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+        testContainer.destroy();
+        /*TODO: Cannot cleanup yet until still other Resources tests depends on the static DB content
+
+        DatabaseClass.clearDatabase();
+        DatabaseClass.getDmaap().remove();*/
+    }
+
+    @Before
+    public void cleanupDatabase() {
+        DatabaseClass.clearDatabase();
+    }
+
+    @Test
+    public void getDr_Pub_test() {
+        Response resp = testContainer.target("dr_pubs").request().get(Response.class);
+        assertTrue(resp.getStatus() == 200);
+        assertTrue(resp.hasEntity());
+    }
+
+    @Test
+    public void addDr_Pub_shallReturnError_whenNoFeedIdAndFeedNameInPubProvided() {
+        //given
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
+        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .request()
+            .post(requestedEntity, Response.class);
+
+        //then
+        assertEquals(400, resp.getStatus());
+        ApiError responseError = resp.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("feedName", responseError.getFields());
+    }
+
+    @Test
+    public void addDr_Pub_shallReturnError_whenFeedNameProvided_butFeedNotExist() {
+        //given
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
+        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+        drPub.setFeedName("feed_name");
+
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .request()
+            .post(requestedEntity, Response.class);
+
+        //then
+        assertEquals(404, resp.getStatus());
+        ApiError responseError = resp.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("feedName", responseError.getFields());
+
+    }
+
+    @Test
+    public void addDr_Pub_shallReturnError_whenFeedIdProvided_butFeedNotExist() {
+        //given
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
+        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .request()
+            .post(requestedEntity, Response.class);
+
+        //then
+        assertEquals(404, resp.getStatus());
+        ApiError responseError = resp.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("feedId=" + FEED_ID, responseError.getFields());
+    }
+
+    @Test
+    public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedIdProvided() {
+        //given
+        String feedId = assureFeedIsInDB();
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
+        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .request()
+            .post(requestedEntity, Response.class);
+
+        //then
+        assertEquals(201, resp.getStatus());
+    }
+
+    @Test
+    public void addDr_Pub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
+        //given
+        String feedName = "testFeed";
+        testFeedCreator.addFeed(feedName, "test feed");
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, PUB_ID);
+        drPub.setFeedName(feedName);
+        Entity<DR_Pub> requestedEntity = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .request()
+            .post(requestedEntity, Response.class);
+
+        //then
+        assertEquals(201, resp.getStatus());
+    }
+
+    @Test
+    public void updateDr_Pub_shallExecuteSuccessfully_whenAddingNewPublisher() {
+        //given
+        String pubId = "5";
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, "feedId", PUB_ID);
+        Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .path(pubId)
+            .request()
+            .put(reqEntity2, Response.class);
+
+        //then
+        assertEquals(200, resp.getStatus());
+
+    }
+
+    @Test
+    public void updateDr_Pub_shallReturnError_whenPathIsWrong() {
+        //given
+        DR_Pub drPub = new DR_Pub(DCAE_LOCATION_NAME, USERNAME, USRPWD, FEED_ID, PUB_ID);
+        Entity<DR_Pub> reqEntity2 = Entity.entity(drPub, MediaType.APPLICATION_JSON);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .path("")
+            .request()
+            .put(reqEntity2, Response.class);
+
+        //then
+        assertEquals(405, resp.getStatus());
+    }
+
+    @Test
+    public void deleteDr_Pub_shouldDeleteObjectWithSuccess() {
+        //given
+        String feedId = assureFeedIsInDB();
+        DR_Pub dr_pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .path(dr_pub.getPubId())
+            .request()
+            .delete();
+
+        //then
+        assertEquals("Shall delete subscription with success", 204, resp.getStatus());
+        assertFalse("No entity object shall be returned", resp.hasEntity());
+    }
+
+    @Test
+    public void deleteDr_Pub_shouldReturnErrorResponse_whenGivenPubIdNotFound() {
+        //given
+        String notExistingPubId = "6789";
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+            .path(notExistingPubId)
+            .request()
+            .delete();
+
+        //then
+        assertEquals("Shall return error, when trying to delete not existing subscription", 404, resp.getStatus());
+        ApiError responseError = resp.readEntity(ApiError.class);
+        assertNotNull(responseError);
+        assertEquals("pubId", responseError.getFields());
+    }
+
+    @Test
+    public void get_shallReturnExistingObject() {
+        //given
+        String feedId = assureFeedIsInDB();
+        DR_Pub dr_Pub = addPub(DCAE_LOCATION_NAME, USERNAME, USRPWD, feedId);
+
+        //when
+        Response resp = testContainer.target("dr_pubs")
+                .path(dr_Pub.getPubId())
+                .request()
+                .get();
+
+        //then
+        assertEquals("Subscription shall be found", 200, resp.getStatus());
+        assertEquals("Retrieved object shall be equal to eh one put into DB", dr_Pub, resp.readEntity(DR_Pub.class));
+    }
 
+    private DR_Pub addPub(String d, String un, String up, String feedId) {
+        DR_Pub dr_pub = new DR_Pub(d, un, up, feedId, "");
+        Entity<DR_Pub> reqEntity2 = Entity.entity(dr_pub, MediaType.APPLICATION_JSON);
+        Response resp = testContainer.target("dr_pubs").request().post(reqEntity2, Response.class);
+        System.out.println("POST dr_pubs resp=" + resp.getStatus());
+        assertTrue(resp.getStatus() == 201);
+        dr_pub = resp.readEntity(DR_Pub.class);
+        return dr_pub;
+    }
 
+    private String assureFeedIsInDB() {
+        Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
+        assertNotNull("Feed shall be added into DB properly", feed);
+        return feed.getFeedId();
+    }
 }
 
 
index f9513a6..13b89ea 100644 (file)
@@ -25,11 +25,9 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.core.Application;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import org.glassfish.jersey.server.ResourceConfig;
-import org.glassfish.jersey.test.JerseyTest;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -51,19 +49,19 @@ public class DR_SubResourceTest {
     private static final String LOG_URL = "https://dr-prov/sublog/id";
     private static final String DELIVERY_URL_TEMPLATE = "https://subscriber.onap.org/delivery/";
     private static final String LOG_URL_TEMPLATE = "https://dr-prov/sublog/";
-    private static FastJerseyTest testContainer;
+    private static FastJerseyTestContainer testContainer;
+    private static TestFeedCreator testFeedCreator;
 
     @BeforeClass
     public static void setUpClass() throws Exception {
         //TODO: init is still needed here to assure that dmaap is not null
         DatabaseClass.getDmaap().init(DMAAP_OBJECT_FACTORY.genDmaap());
-        DatabaseClass.getDmaap().update(DMAAP_OBJECT_FACTORY.genDmaap());
 
-        testContainer = new FastJerseyTest(new ResourceConfig()
+        testContainer = new FastJerseyTestContainer(new ResourceConfig()
             .register(DR_SubResource.class)
-            .register(FeedResource.class)
-            .register(DmaapResource.class));
+            .register(FeedResource.class));
         testContainer.init();
+        testFeedCreator = new TestFeedCreator(testContainer);
     }
 
     @AfterClass
@@ -76,7 +74,7 @@ public class DR_SubResourceTest {
     }
 
     @Before
-    public void cleanupDatabase() throws Exception {
+    public void cleanupDatabase() {
         DatabaseClass.clearDatabase();
     }
 
@@ -86,7 +84,7 @@ public class DR_SubResourceTest {
         Response resp = testContainer.target("dr_subs").request().get(Response.class);
         System.out.println("GET dr_subs resp=" + resp.getStatus());
 
-        assertTrue(resp.getStatus() == 200);
+        assertEquals(200, resp.getStatus());
         assertTrue(resp.hasEntity());
     }
 
@@ -193,7 +191,7 @@ public class DR_SubResourceTest {
     public void addDr_Sub_shallExecuteSuccessfully_whenValidFeedNameProvided() {
         //given
         String feedName = "testFeed";
-        addFeed(feedName, "test feed");
+        testFeedCreator.addFeed(feedName, "test feed");
         DR_Sub drSub = new DR_Sub(DCAE_LOCATION_NAME, USERNAME, USRPWD, null, DELIVERY_URL, LOG_URL, true);
         drSub.setFeedName(feedName);
         Entity<DR_Sub> requestedEntity = Entity.entity(drSub, MediaType.APPLICATION_JSON);
@@ -394,17 +392,6 @@ public class DR_SubResourceTest {
         assertNotNull(resp.readEntity(ApiError.class));
     }
 
-    private Feed addFeed(String name, String desc) {
-        Feed feed = new Feed(name, "1.0", desc, "dgl", "unrestricted");
-        Entity<Feed> reqEntity = Entity.entity(feed, MediaType.APPLICATION_JSON);
-        Response resp = testContainer.target("feeds").request().post(reqEntity, Response.class);
-        int rc = resp.getStatus();
-        System.out.println("POST feed resp=" + rc);
-        assertTrue(rc == 200 || rc == 409);
-        feed = resp.readEntity(Feed.class);
-        return feed;
-    }
-
     private DR_Sub addSub(String d, String un, String up, String feedId) {
         DR_Sub dr_sub = new DR_Sub(d, un, up, feedId,
             "https://subscriber.onap.org/foo", "https://dr-prov/sublog", true);
@@ -412,14 +399,14 @@ public class DR_SubResourceTest {
         Entity<DR_Sub> reqEntity2 = Entity.entity(dr_sub, MediaType.APPLICATION_JSON);
         Response resp = testContainer.target("dr_subs").request().post(reqEntity2, Response.class);
         System.out.println("POST dr_subs resp=" + resp.getStatus());
-        assertTrue(resp.getStatus() == 201);
+        assertEquals(201, resp.getStatus());
         dr_sub = resp.readEntity(DR_Sub.class);
 
         return dr_sub;
     }
 
     private String assureFeedIsInDB() {
-        Feed feed = addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
+        Feed feed = testFeedCreator.addFeed("SubscriberTestFeed", "feed for DR_Sub testing");
         assertNotNull("Feed shall be added into DB properly", feed);
         return feed.getFeedId();
     }
@@ -442,22 +429,6 @@ public class DR_SubResourceTest {
         assertTrue(response.hasEntity());
         assertEquals(sub, response.readEntity(DR_Sub.class));
     }
-
-    //TODO: move it outside class and use in other Resource integration tests
-    private static class FastJerseyTest extends JerseyTest {
-
-        FastJerseyTest(Application jaxrsApplication) {
-            super(jaxrsApplication);
-        }
-
-        void init() throws Exception {
-            this.setUp();
-        }
-
-        void destroy() throws Exception {
-            this.tearDown();
-        }
-    }
 }
 
 
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java b/src/test/java/org/onap/dmaap/dbcapi/resources/FastJerseyTestContainer.java
new file mode 100644 (file)
index 0000000..8d38a9f
--- /dev/null
@@ -0,0 +1,39 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import org.glassfish.jersey.test.JerseyTest;
+
+import javax.ws.rs.core.Application;
+
+class FastJerseyTestContainer extends JerseyTest {
+
+    FastJerseyTestContainer(Application jaxrsApplication) {
+        super(jaxrsApplication);
+    }
+
+    void init() throws Exception {
+        this.setUp();
+    }
+
+    void destroy() throws Exception {
+        this.tearDown();
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/RequiredFieldExceptionTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/RequiredFieldExceptionTest.java
new file mode 100644 (file)
index 0000000..d3dcc42
--- /dev/null
@@ -0,0 +1,51 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Modifications Copyright (c) 2019 IBM
+ * ===================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dmaap.dbcapi.resources;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.dmaap.dbcapi.model.ApiError;
+
+public class RequiredFieldExceptionTest {
+    ApiError apiError;
+    RequiredFieldException requiredFieldException;
+    String expectedValue;
+
+    @Before
+    public void setUp() {
+        apiError = new ApiError(BAD_REQUEST.getStatusCode(), "value 'with white space' violates regexp check '^\\S+$'",
+                "field_name");
+
+        expectedValue = "RequiredFieldException{" + "apiError=" + apiError + '}';
+
+        requiredFieldException = new RequiredFieldException(apiError);
+    }
+
+    @Test
+    public void testRequiredFieldExceptionToString() {
+        assertEquals(expectedValue, requiredFieldException.toString());
+    }
+}
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/TestFeedCreator.java b/src/test/java/org/onap/dmaap/dbcapi/resources/TestFeedCreator.java
new file mode 100644 (file)
index 0000000..e4dedb1
--- /dev/null
@@ -0,0 +1,49 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.dmaap\r
+ * ================================================================================\r
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.dmaap.dbcapi.resources;\r
+\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import javax.ws.rs.client.Entity;\r
+import javax.ws.rs.core.MediaType;\r
+import javax.ws.rs.core.Response;\r
+import org.onap.dmaap.dbcapi.model.Feed;\r
+\r
+\r
+public class TestFeedCreator {\r
+\r
+\r
+    private final FastJerseyTestContainer testContainer;\r
+\r
+    public TestFeedCreator(FastJerseyTestContainer testContainer) {\r
+        this.testContainer = testContainer;\r
+    }\r
+\r
+    Feed addFeed(String name, String desc) {\r
+        Feed feed = new Feed(name, "1.0", desc, "dgl", "unrestricted");\r
+        Entity<Feed> reqEntity = Entity.entity(feed, MediaType.APPLICATION_JSON);\r
+        Response resp = testContainer.target("feeds").request().post(reqEntity, Response.class);\r
+        int rc = resp.getStatus();\r
+        System.out.println("POST feed resp=" + rc);\r
+        assertTrue(rc == 200 || rc == 409);\r
+        feed = resp.readEntity(Feed.class);\r
+        return feed;\r
+    }\r
+}\r
diff --git a/src/test/java/org/onap/dmaap/dbcapi/util/PermissionBuilderTest.java b/src/test/java/org/onap/dmaap/dbcapi/util/PermissionBuilderTest.java
new file mode 100644 (file)
index 0000000..1858e47
--- /dev/null
@@ -0,0 +1,151 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.util;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.atMost;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.http.HttpServletRequest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.dmaap.dbcapi.model.Dmaap;
+import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
+import org.onap.dmaap.dbcapi.service.DmaapService;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PermissionBuilderTest {
+
+    private static final String DMAAP_NAME = "mr";
+    private PermissionBuilder permissionBuilder;
+    @Mock
+    private DmaapConfig dmaapConfig;
+    @Mock
+    private DmaapService dmaapService;
+    @Mock
+    private HttpServletRequest request;
+
+
+    @Test
+    public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsNotInitialized() {
+        //given
+        doReturn(null).when(dmaapService).getDmaap();
+        permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
+
+        //when
+        permissionBuilder.updateDmaapInstance();
+
+        //then
+        assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
+    }
+
+    @Test
+    public void updateDmaapInstance_shouldSetBootInstance_whenDmaapIsInitializedWithDefaultInstance() {
+        //given
+        doReturn(provideDefaultInstance()).when(dmaapService).getDmaap();
+        permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
+
+        //when
+        permissionBuilder.updateDmaapInstance();
+
+        //then
+        assertEquals(PermissionBuilder.BOOT_INSTANCE, permissionBuilder.getInstance());
+    }
+
+    @Test
+    public void updateDmaapInstance_shouldSetRealInstance_whenDmaapServiceProvidesOne() {
+        //given
+        when(dmaapService.getDmaap()).thenReturn(provideDefaultInstance(), provideRealInstance(DMAAP_NAME));
+        permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
+
+        //when
+        permissionBuilder.updateDmaapInstance();
+
+        //then
+        assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
+    }
+
+    @Test
+    public void updateDmaapInstance_shouldNotUpdateDmaapInstance_whenAlreadyInitializedWithRealInstance() {
+        //given
+        when(dmaapService.getDmaap()).thenReturn(provideRealInstance(DMAAP_NAME), provideRealInstance("newName"));
+        permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
+
+        //when
+        permissionBuilder.updateDmaapInstance();
+
+        //then
+        assertEquals(DMAAP_NAME, permissionBuilder.getInstance());
+        verify(dmaapService, atMost(1)).getDmaap();
+    }
+
+    @Test
+    public void buildPermission_shouldBuildPermissionWithBootInstance() {
+        //given
+        String path = "/dmaap";
+        String method = "GET";
+        initPermissionBuilder(path, method, provideDefaultInstance());
+
+        //when
+        String permission = permissionBuilder.buildPermission(request);
+
+        //then
+        assertEquals("org.onap.dmaap-bc.api.dmaap|boot|GET", permission);
+    }
+
+    @Test
+    public void buildPermission_shouldBuildPermissionWithRealInstance() {
+        //given
+        String path = "/subpath/topics/";
+        String method = "GET";
+        initPermissionBuilder(path, method, provideRealInstance(DMAAP_NAME));
+
+        //when
+        String permission = permissionBuilder.buildPermission(request);
+
+        //then
+        assertEquals("org.onap.dmaap-bc.api.topics|mr|GET", permission);
+    }
+
+    private void initPermissionBuilder(String path, String method, Dmaap dmaapInstance) {
+        when(dmaapConfig.getProperty(PermissionBuilder.API_NS_PROP, PermissionBuilder.DEFAULT_API_NS))
+            .thenReturn(PermissionBuilder.DEFAULT_API_NS);
+        when(dmaapService.getDmaap()).thenReturn(dmaapInstance);
+        permissionBuilder = new PermissionBuilder(dmaapConfig, dmaapService);
+
+        when(request.getPathInfo()).thenReturn(path);
+        when(request.getMethod()).thenReturn(method);
+    }
+
+    private Dmaap provideDefaultInstance() {
+        return new  Dmaap("0", "", "", "", "", "", "", "");
+    }
+
+    private Dmaap provideRealInstance(String dmaapName) {
+        Dmaap dmaap = new Dmaap("1", "org.onap.dmaap", dmaapName, "https://dmaap-dr-prov:8443", "", "DCAE_MM_AGENT", "", "");
+        dmaap.setStatus(DmaapObject_Status.VALID);
+        return dmaap;
+    }
+
+}
\ No newline at end of file
index 102b722..8a9bf52 100644 (file)
@@ -27,7 +27,7 @@
 
 major=1
 minor=0
-patch=24
+patch=26
 base_version=${major}.${minor}.${patch}
 
 # Release must be completed with git revision # in Jenkins