Merge "Junits for classes in util package"
authorDominic Lunanuova <dgl@research.att.com>
Fri, 12 Apr 2019 13:31:26 +0000 (13:31 +0000)
committerGerrit Code Review <gerrit@onap.org>
Fri, 12 Apr 2019 13:31:26 +0000 (13:31 +0000)
docs/api.rst
src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java
src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java
src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java
src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java
src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java
src/test/resources/cadi.properties [new file with mode: 0644]

index 7f187d4..9eab4da 100644 (file)
@@ -4928,54 +4928,3 @@ Topic Model Structure
         topicName | No | string |  |  | the short name used by humans, and utilized to construct the `FQTN`
         version | No | string |  |  | a hook for any versioning needed for managing a `Topic` over time
 
-Security
-~~~~~~~~
-As default security is in Dmaap Bus Controller disabled.
-
-Enable
-------
-Settings to turn on security is in file dmaapbc.properties. The file is located in OOM project in path: ./oom/kubernetes/dmaap/components/dmaap-bc/resources/config/dmaapbc.properties
-During deployment the the file is placed into ConfigMap XXX-dmaap-bus-controller-config (XXX depend on deployment setup). The config map is linked to volume with read only permission so it can not be changed from pod level.
-Ater updating ConfigMap the bus controller pod needs to be restarted.
-
-Settings
-++++++++
-
-In the dmaapbc.properties for security settings there is a main flag:
-
-1.UseAAF: true
-
-If set to true then creating topic also will create required perms in AAF. The perms will be created in org.onap.dmaap.mr.
-The last element -mr- is related to another setting - MR.projectID .
-
-Example:
-  Topic name:
-    aSimpleTopic
-  Permitions
-    org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|pub
-    org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|sub
-    org.onap.dmaap.mr.topic|:topic.org.onap.dmaap.mr.aSimpleTopic|view
-
-
-Hint: User defined in the certificate of cadi (property:cadi.properties, user:dmaap-bc@dmaap-bc.onap.org) needs to have permissions to create and view such topics (org.onap.dmaap.mr.topic|*|*).
-
-
-Authentication is using CADI
-
-CADI confilguration is stored in CADI files. Location of the files is defined in varaible:
-cadi.properties: /opt/app/osaaf/local/org.onap.dmaap-bc.props
-
-The configuration is a mandatory and missing parameter or file cause exception.
-
-Authorization is done by CADI - configuration is required as above
-
-Call to bus controller needs to have given user credentials. The user ich checked in AAF for permission to call topic.
-The check is done in org.onap.dmaap-bc.api according to ApiNamespace setting.
-
-Hint: User defined in the certificate of cadi (property:cadi.properties, user:dmaap-bc@dmaap-bc.onap.org) needs to have permission to read the namespace (org.onap.dmaap-bc.api.access|*|read).
-
-
-2.UseAAF: false
-
-For backward compatibility, if AAF flag is turned off the previous implementation is enabled based on AuthorizationFilter and ApiPermission class.
-This filter switching has been made due to the technology differences: Jersey filters do not implement directly servlet API, but CADI filter is based on it.
\ No newline at end of file
index b4b5e2e..f459c6c 100644 (file)
@@ -22,11 +22,10 @@ package org.onap.dmaap.dbcapi.model;
 
 import javax.xml.bind.annotation.XmlRootElement;
 
-import org.apache.log4j.Logger;
+import java.util.Objects;
 
 @XmlRootElement
 public class DcaeLocation extends DmaapObject {
-       static final Logger errorLogger = Logger.getLogger(MR_Cluster.class);
        private String clli;
        private String dcaeLayer;
        private String dcaeLocationName;
@@ -100,4 +99,21 @@ public class DcaeLocation extends DmaapObject {
                this.subnet = subnet;
        }
 
+       @Override
+       public boolean equals(Object o) {
+               if (this == o) return true;
+               if (o == null || getClass() != o.getClass()) return false;
+               DcaeLocation that = (DcaeLocation) o;
+               return Objects.equals(clli, that.clli) &&
+                               Objects.equals(dcaeLayer, that.dcaeLayer) &&
+                               Objects.equals(dcaeLocationName, that.dcaeLocationName) &&
+                               Objects.equals(openStackAvailabilityZone, that.openStackAvailabilityZone) &&
+                               Objects.equals(subnet, that.subnet);
+       }
+
+       @Override
+       public int hashCode() {
+
+               return Objects.hash(clli, dcaeLayer, dcaeLocationName, openStackAvailabilityZone, subnet);
+       }
 }
index 8739511..c5c29fa 100644 (file)
@@ -21,7 +21,9 @@ package org.onap.dmaap.dbcapi.resources;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.Properties;
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
@@ -98,7 +100,7 @@ public class AAFAuthenticationFilter implements Filter {
             try {
                 String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES);
                 if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) {
-                    cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile));
+                    cadiFilter = new CadiFilter(loadCadiProperties(cadiPropertiesFile));
                 } else {
                     throw new ServletException("Cannot initialize CADI filter.CADI properties not available.");
                 }
@@ -109,6 +111,18 @@ public class AAFAuthenticationFilter implements Filter {
         }
     }
 
+    private PropAccess loadCadiProperties(String propertiesFilePath) throws ServletException {
+        try {
+            Properties props = new Properties();
+            props.load(new FileInputStream(propertiesFilePath));
+            return new PropAccess(props);
+        } catch (IOException e) {
+            String msg = "Could not load CADI properties file: " + propertiesFilePath;
+            LOGGER.error(msg, e);
+            throw new ServletException(msg);
+        }
+    }
+
     DmaapConfig getConfig() {
         return (DmaapConfig) DmaapConfig.getConfig();
     }
index de72ade..ad6c993 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.service;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
 import org.onap.dmaap.dbcapi.database.DatabaseClass;
 import org.onap.dmaap.dbcapi.model.DcaeLocation;
 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
 
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
 public class DcaeLocationService {
-       
-       private Map<String, DcaeLocation> dcaeLocations = DatabaseClass.getDcaeLocations();
-       
-       public Map<String, DcaeLocation> getDcaeLocations() {
-               return dcaeLocations;
-       }
-       
-       public List<DcaeLocation> getAllDcaeLocations() {
-               return new ArrayList<DcaeLocation>(dcaeLocations.values());
-       }
-       
-       public DcaeLocation getDcaeLocation( String name ) {
-               return dcaeLocations.get(name);
-       }
-
-       public DcaeLocation addDcaeLocation( DcaeLocation location ) {
-               location.setLastMod();
-               location.setStatus(DmaapObject_Status.VALID);
-               dcaeLocations.put( location.getDcaeLocationName(), location );
-               return location;
-       }
-       
-       public DcaeLocation updateDcaeLocation( DcaeLocation location ) {
-               if ( location.getDcaeLocationName().isEmpty()) {
-                       return null;
-               }
-               location.setLastMod();
-               dcaeLocations.put( location.getDcaeLocationName(), location );
-               return location;
-       }
-       
-       public DcaeLocation removeDcaeLocation( String locationName ) {
-               return dcaeLocations.remove(locationName);
-       }
-
-       public String getCentralLocation() {
-               for( Map.Entry<String, DcaeLocation> entry: dcaeLocations.entrySet() ) {
-                       DcaeLocation loc = entry.getValue();
-                       if ( loc.isCentral() ) {
-                               // use the name of the first central location we hit
-                               return loc.getDcaeLocationName();
-                       }
-                       
-               }
-               return "aCentralLocation";  // default value that is obvious to see is wrong
-       }       
-       
-       public boolean isEdgeLocation(String aName) {
-               DcaeLocation loc = dcaeLocations.get(aName);
-               if ( loc == null ) {
-                       return false;
-               }
-               if ( ! loc.isCentral() ) {
-                               return true;
-               }
-               return false;
-       }       
+
+    private static final String DEFAULT_CENTRAL_LOCATION = "aCentralLocation"; // default value that is obvious to see is wrong
+    private final Map<String, DcaeLocation> dcaeLocations;
+
+    public DcaeLocationService() {
+        this(DatabaseClass.getDcaeLocations());
+    }
+
+    DcaeLocationService(Map<String, DcaeLocation> dcaeLocations) {
+        this.dcaeLocations = dcaeLocations;
+    }
+
+    public List<DcaeLocation> getAllDcaeLocations() {
+        return new ArrayList<>(dcaeLocations.values());
+    }
+
+    public DcaeLocation getDcaeLocation(String name) {
+        return dcaeLocations.get(name);
+    }
+
+    public DcaeLocation addDcaeLocation(DcaeLocation location) {
+        location.setLastMod();
+        location.setStatus(DmaapObject_Status.VALID);
+        dcaeLocations.put(location.getDcaeLocationName(), location);
+        return location;
+    }
+
+    public DcaeLocation updateDcaeLocation(DcaeLocation location) {
+        if (location.getDcaeLocationName().isEmpty()) {
+            return null;
+        }
+        location.setLastMod();
+        dcaeLocations.put(location.getDcaeLocationName(), location);
+        return location;
+    }
+
+    public DcaeLocation removeDcaeLocation(String locationName) {
+        return dcaeLocations.remove(locationName);
+    }
+
+    String getCentralLocation() {
+
+        Optional<DcaeLocation> firstCentralLocation =
+                dcaeLocations.values().stream().filter(DcaeLocation::isCentral).findFirst();
+
+        return firstCentralLocation.isPresent() ? firstCentralLocation.get().getDcaeLocationName() : DEFAULT_CENTRAL_LOCATION;
+    }
+
+    boolean isEdgeLocation(String aName) {
+        return dcaeLocations.get(aName) != null && !dcaeLocations.get(aName).isCentral();
+    }
 
 }
index d5ae5fd..53c8021 100644 (file)
@@ -118,10 +118,25 @@ public class AAFAuthenticationFilterTest {
     }
 
     @Test
-    public void init_shouldInitializeCADI_whenAafIsUsed_andCadiPropertiesSet() throws Exception {
+    public void init_shouldFail_whenAafIsUsed_andInvalidCadiPropertiesSet() throws Exception {
         //given
+        String invalidFilePath = "src/test/resources/notExisting.properties";
         doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
-        doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
+        doReturn(invalidFilePath).when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
+
+        //then
+        thrown.expect(ServletException.class);
+        thrown.expectMessage("Could not load CADI properties file: "+invalidFilePath);
+
+        //when
+        filter.init(filterConfig);
+    }
+
+    @Test
+    public void init_shouldInitializeCADI_whenAafIsUsed_andValidCadiPropertiesSet() throws Exception {
+        //given
+        doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
+        doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
 
         //when
         filter.init(filterConfig);
@@ -170,7 +185,7 @@ public class AAFAuthenticationFilterTest {
 
     private void initCADIFilter() throws Exception{
         doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString());
-        doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
+        doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES);
         filter.init(filterConfig);
         filter.setCadiFilter(cadiFilterMock);
     }
index 370fa82..2b8ef34 100644 (file)
@@ -2,14 +2,14 @@
  * ============LICENSE_START=======================================================
  * org.onap.dmaap
  * ================================================================================
- * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
+ * 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.
  */
 package org.onap.dmaap.dbcapi.service;
 
-import  org.onap.dmaap.dbcapi.model.*;
-import org.onap.dmaap.dbcapi.testframework.ReflectionHarness;
-
-import static org.junit.Assert.*;
-
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Test;
+import org.onap.dmaap.dbcapi.model.DcaeLocation;
+import org.onap.dmaap.dbcapi.model.DmaapObject;
+
+import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 
+import static junit.framework.Assert.assertNull;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
 public class DcaeLocationServiceTest {
 
-       private static final String  fmt = "%24s: %s%n";
+    private static final String LOCATION_A = "locationA";
+    private static final String LOCATION_B = "locationB";
+    private DcaeLocationService locationService = new DcaeLocationService(new HashMap<>());
+
+    @Test
+    public void getAllDcaeLocations_shouldReturnEmptyCollection() {
+
+        List<DcaeLocation> allDcaeLocations = locationService.getAllDcaeLocations();
+
+        assertTrue(allDcaeLocations.isEmpty());
+    }
+
+    @Test
+    public void addDcaeLocation_shouldAddLocationToMap() {
+        DcaeLocation locationA = createDcaeLocation(LOCATION_A);
+
+        DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
+
+        assertEquals(locationA, locationService.getDcaeLocation(LOCATION_A));
+        assertSame(locationA, addedLocation);
+    }
+
+    @Test
+    public void addDcaeLocation_shouldSetStatusAndLastModDate() {
+        DcaeLocation locationA = createDcaeLocation(LOCATION_A);
+        Date creationDate = new Date(10);
+        locationA.setLastMod(creationDate);
+
+        DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
+
+        assertTrue(addedLocation.getLastMod().after(creationDate));
+        assertEquals(DmaapObject.DmaapObject_Status.VALID, addedLocation.getStatus());
+    }
+
+    @Test
+    public void updateDcaeLocation_shouldUpdateLocationAndLastModDate() {
+        DcaeLocation location = createDcaeLocation(LOCATION_A);
+        Date creationDate = new Date(10);
+        location.setLastMod(creationDate);
+        locationService.addDcaeLocation(location);
 
-       ReflectionHarness rh = new ReflectionHarness();
+        DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
 
-       DcaeLocationService ds;
+        assertTrue(updatedLocation.getLastMod().after(creationDate));
+        assertSame(location, updatedLocation);
+    }
 
-       @Before
-       public void setUp() throws Exception {
-               ds = new DcaeLocationService();
-       }
+    @Test
+    public void updateDcaeLocation_shouldShouldReturnNullWhenLocationNameIsEmpty() {
+        DcaeLocation location = createDcaeLocation("");
 
-       @After
-       public void tearDown() throws Exception {
-       }
+        DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
 
+        assertNull(updatedLocation);
+        assertTrue(locationService.getAllDcaeLocations().isEmpty());
+    }
 
-       @Test
-       public void test1() {
+    @Test
+    public void removeDcaeLocation_shouldRemoveLocationFromService() {
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_A));
 
+        locationService.removeDcaeLocation(LOCATION_A);
 
-               //rh.reflect( "org.onap.dmaap.dbcapi.service.DcaeLocationService", "get", null );       
-       
-       }
+        assertTrue(locationService.getAllDcaeLocations().isEmpty());
+    }
 
-       @Test
-       public void test2() {
-               String v = "Validate";
-               rh.reflect( "org.onap.dmaap.dbcapi.service.DcaeLocationService", "set", v );
+    @Test
+    public void getCentralLocation_shouldGetFirstCentralLocation() {
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
 
-       }
+        assertEquals(LOCATION_B, locationService.getCentralLocation());
+    }
 
-       @Test
-       public void test3() {
-               String n = "demo-network-c";
-               DcaeLocation nd = new DcaeLocation( "CLLI0123", "central-layer", n,  "zoneA", "10.10.10.0/24" );
-               
-               DcaeLocation gd = ds.addDcaeLocation( nd );
+    @Test
+    public void getCentralLocation_shouldReturnDefaultCentralLocationNameWhenThereIsNoCentralLocation() {
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
 
-               assertTrue( nd.getDcaeLocationName().equals( gd.getDcaeLocationName() ));
-       }
+        assertEquals("aCentralLocation", locationService.getCentralLocation());
+    }
 
-       @Test
-       public void test4() {
-               List<DcaeLocation> d = ds.getAllDcaeLocations();
+    @Test
+    public void isEdgeLocation_shouldReturnTrueForNotCentralLocation() {
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
 
-       }
+        assertTrue(locationService.isEdgeLocation(LOCATION_A));
+        assertFalse(locationService.isEdgeLocation(LOCATION_B));
+    }
 
-       @Test
-       public void test5() {
-               String n = "demo-network-c";
-               DcaeLocation nd = new DcaeLocation( "CLLI9999", "central-layer", n,  "zoneA", "10.10.10.0/24" );
-               DcaeLocation gd = ds.updateDcaeLocation( nd );
+    @Test
+    public void isEdgeLocation_shouldReturnFalseWhenLocationDoesNotExist() {
+        locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
 
-               assertTrue( nd.getDcaeLocationName().equals( gd.getDcaeLocationName() ));
+        assertFalse(locationService.isEdgeLocation("not_existing_location"));
+    }
 
-       }
+    private DcaeLocation createDcaeLocation(String locationName) {
+        return createDcaeLocation(locationName, "dcaeLayer");
+    }
 
-       @Test
-       public void test6() {
+    private DcaeLocation createDcaeLocation(String locationName, String dcaeLayer) {
+        return new DcaeLocation("clli", dcaeLayer, locationName, "openStackAvailabilityZone", "subnet");
+    }
 
-               String n = "demo-network-c";
-               DcaeLocation gd = ds.removeDcaeLocation( n );
-       }
 
 }
diff --git a/src/test/resources/cadi.properties b/src/test/resources/cadi.properties
new file mode 100644 (file)
index 0000000..e69de29