DcaeLocation tests and refactor 09/85009/2
authorpkaras <piotr.karas@nokia.com>
Thu, 11 Apr 2019 06:07:35 +0000 (08:07 +0200)
committerpkaras <piotr.karas@nokia.com>
Thu, 11 Apr 2019 07:06:35 +0000 (09:06 +0200)
Change-Id: I14f6d2db0aa81a6ca1558c081ee9a98951a3c316
Issue-ID: DMAAP-1157
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java
src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java
src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java

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 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 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 );
-       }
 
 }