diff options
Diffstat (limited to 'src')
6 files changed, 209 insertions, 123 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java b/src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java index b4b5e2e..f459c6c 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java +++ b/src/main/java/org/onap/dmaap/dbcapi/model/DcaeLocation.java @@ -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); + } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java index 8739511..c5c29fa 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java @@ -21,7 +21,9 @@ package org.onap.dmaap.dbcapi.resources; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.FileInputStream; import java.io.IOException; +import java.util.Properties; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -98,7 +100,7 @@ public class AAFAuthenticationFilter implements Filter { try { String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES); if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) { - cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile)); + cadiFilter = new CadiFilter(loadCadiProperties(cadiPropertiesFile)); } else { throw new ServletException("Cannot initialize CADI filter.CADI properties not available."); } @@ -109,6 +111,18 @@ public class AAFAuthenticationFilter implements Filter { } } + private PropAccess loadCadiProperties(String propertiesFilePath) throws ServletException { + try { + Properties props = new Properties(); + props.load(new FileInputStream(propertiesFilePath)); + return new PropAccess(props); + } catch (IOException e) { + String msg = "Could not load CADI properties file: " + propertiesFilePath; + LOGGER.error(msg, e); + throw new ServletException(msg); + } + } + DmaapConfig getConfig() { return (DmaapConfig) DmaapConfig.getConfig(); } diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java b/src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java index de72ade..ad6c993 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/DcaeLocationService.java @@ -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. @@ -20,71 +20,66 @@ 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(); + } } diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java index d5ae5fd..53c8021 100644 --- a/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java +++ b/src/test/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilterTest.java @@ -118,10 +118,25 @@ public class AAFAuthenticationFilterTest { } @Test - public void init_shouldInitializeCADI_whenAafIsUsed_andCadiPropertiesSet() throws Exception { + public void init_shouldFail_whenAafIsUsed_andInvalidCadiPropertiesSet() throws Exception { //given + String invalidFilePath = "src/test/resources/notExisting.properties"; doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); - doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + doReturn(invalidFilePath).when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + + //then + thrown.expect(ServletException.class); + thrown.expectMessage("Could not load CADI properties file: "+invalidFilePath); + + //when + filter.init(filterConfig); + } + + @Test + public void init_shouldInitializeCADI_whenAafIsUsed_andValidCadiPropertiesSet() throws Exception { + //given + doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); + doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); //when filter.init(filterConfig); @@ -170,7 +185,7 @@ public class AAFAuthenticationFilterTest { private void initCADIFilter() throws Exception{ doReturn("true").when(dmaapConfig).getProperty(eq(AAFAuthenticationFilter.AAF_AUTHN_FLAG), anyString()); - doReturn("cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); + doReturn("src/test/resources/cadi.properties").when(dmaapConfig).getProperty(AAFAuthenticationFilter.CADI_PROPERTIES); filter.init(filterConfig); filter.setCadiFilter(cadiFilterMock); } diff --git a/src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java b/src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java index 370fa82..2b8ef34 100644 --- a/src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java +++ b/src/test/java/org/onap/dmaap/dbcapi/service/DcaeLocationServiceTest.java @@ -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. @@ -19,80 +19,126 @@ */ 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 index 0000000..e69de29 --- /dev/null +++ b/src/test/resources/cadi.properties |