2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dmaap.dbcapi.service;
22 import org.junit.Test;
23 import org.onap.dmaap.dbcapi.model.DcaeLocation;
24 import org.onap.dmaap.dbcapi.model.DmaapObject;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertSame;
34 import static org.junit.Assert.assertTrue;
36 public class DcaeLocationServiceTest {
38 private static final String LOCATION_A = "locationA";
39 private static final String LOCATION_B = "locationB";
40 private DcaeLocationService locationService = new DcaeLocationService(new HashMap<>());
43 public void getAllDcaeLocations_shouldReturnEmptyCollection() {
45 List<DcaeLocation> allDcaeLocations = locationService.getAllDcaeLocations();
47 assertTrue(allDcaeLocations.isEmpty());
51 public void addDcaeLocation_shouldAddLocationToMap() {
52 DcaeLocation locationA = createDcaeLocation(LOCATION_A);
54 DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
56 assertEquals(locationA, locationService.getDcaeLocation(LOCATION_A));
57 assertSame(locationA, addedLocation);
61 public void addDcaeLocation_shouldSetStatusAndLastModDate() {
62 DcaeLocation locationA = createDcaeLocation(LOCATION_A);
63 Date creationDate = new Date(10);
64 locationA.setLastMod(creationDate);
66 DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
68 assertTrue(addedLocation.getLastMod().after(creationDate));
69 assertEquals(DmaapObject.DmaapObject_Status.VALID, addedLocation.getStatus());
73 public void updateDcaeLocation_shouldUpdateLocationAndLastModDate() {
74 DcaeLocation location = createDcaeLocation(LOCATION_A);
75 Date creationDate = new Date(10);
76 location.setLastMod(creationDate);
77 locationService.addDcaeLocation(location);
79 DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
81 assertTrue(updatedLocation.getLastMod().after(creationDate));
82 assertSame(location, updatedLocation);
86 public void updateDcaeLocation_shouldShouldReturnNullWhenLocationNameIsEmpty() {
87 DcaeLocation location = createDcaeLocation("");
89 DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
91 assertNull(updatedLocation);
92 assertTrue(locationService.getAllDcaeLocations().isEmpty());
96 public void removeDcaeLocation_shouldRemoveLocationFromService() {
97 locationService.addDcaeLocation(createDcaeLocation(LOCATION_A));
99 locationService.removeDcaeLocation(LOCATION_A);
101 assertTrue(locationService.getAllDcaeLocations().isEmpty());
105 public void getCentralLocation_shouldGetFirstCentralLocation() {
106 locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
107 locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
109 assertEquals(LOCATION_B, locationService.getCentralLocation());
113 public void getCentralLocation_shouldReturnDefaultCentralLocationNameWhenThereIsNoCentralLocation() {
114 locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
116 assertEquals("aCentralLocation", locationService.getCentralLocation());
120 public void isEdgeLocation_shouldReturnTrueForNotCentralLocation() {
121 locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
122 locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
124 assertTrue(locationService.isEdgeLocation(LOCATION_A));
125 assertFalse(locationService.isEdgeLocation(LOCATION_B));
129 public void isEdgeLocation_shouldReturnFalseWhenLocationDoesNotExist() {
130 locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
132 assertFalse(locationService.isEdgeLocation("not_existing_location"));
135 private DcaeLocation createDcaeLocation(String locationName) {
136 return createDcaeLocation(locationName, "dcaeLayer");
139 private DcaeLocation createDcaeLocation(String locationName, String dcaeLayer) {
140 return new DcaeLocation("clli", dcaeLayer, locationName, "openStackAvailabilityZone", "subnet");