DcaeLocation tests and refactor
[dmaap/dbcapi.git] / src / test / java / org / onap / dmaap / dbcapi / service / DcaeLocationServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dmaap.dbcapi.service;
21
22 import org.junit.Test;
23 import org.onap.dmaap.dbcapi.model.DcaeLocation;
24 import org.onap.dmaap.dbcapi.model.DmaapObject;
25
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29
30 import static junit.framework.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;
35
36 public class DcaeLocationServiceTest {
37
38     private static final String LOCATION_A = "locationA";
39     private static final String LOCATION_B = "locationB";
40     private DcaeLocationService locationService = new DcaeLocationService(new HashMap<>());
41
42     @Test
43     public void getAllDcaeLocations_shouldReturnEmptyCollection() {
44
45         List<DcaeLocation> allDcaeLocations = locationService.getAllDcaeLocations();
46
47         assertTrue(allDcaeLocations.isEmpty());
48     }
49
50     @Test
51     public void addDcaeLocation_shouldAddLocationToMap() {
52         DcaeLocation locationA = createDcaeLocation(LOCATION_A);
53
54         DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
55
56         assertEquals(locationA, locationService.getDcaeLocation(LOCATION_A));
57         assertSame(locationA, addedLocation);
58     }
59
60     @Test
61     public void addDcaeLocation_shouldSetStatusAndLastModDate() {
62         DcaeLocation locationA = createDcaeLocation(LOCATION_A);
63         Date creationDate = new Date(10);
64         locationA.setLastMod(creationDate);
65
66         DcaeLocation addedLocation = locationService.addDcaeLocation(locationA);
67
68         assertTrue(addedLocation.getLastMod().after(creationDate));
69         assertEquals(DmaapObject.DmaapObject_Status.VALID, addedLocation.getStatus());
70     }
71
72     @Test
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);
78
79         DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
80
81         assertTrue(updatedLocation.getLastMod().after(creationDate));
82         assertSame(location, updatedLocation);
83     }
84
85     @Test
86     public void updateDcaeLocation_shouldShouldReturnNullWhenLocationNameIsEmpty() {
87         DcaeLocation location = createDcaeLocation("");
88
89         DcaeLocation updatedLocation = locationService.updateDcaeLocation(location);
90
91         assertNull(updatedLocation);
92         assertTrue(locationService.getAllDcaeLocations().isEmpty());
93     }
94
95     @Test
96     public void removeDcaeLocation_shouldRemoveLocationFromService() {
97         locationService.addDcaeLocation(createDcaeLocation(LOCATION_A));
98
99         locationService.removeDcaeLocation(LOCATION_A);
100
101         assertTrue(locationService.getAllDcaeLocations().isEmpty());
102     }
103
104     @Test
105     public void getCentralLocation_shouldGetFirstCentralLocation() {
106         locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
107         locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
108
109         assertEquals(LOCATION_B, locationService.getCentralLocation());
110     }
111
112     @Test
113     public void getCentralLocation_shouldReturnDefaultCentralLocationNameWhenThereIsNoCentralLocation() {
114         locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
115
116         assertEquals("aCentralLocation", locationService.getCentralLocation());
117     }
118
119     @Test
120     public void isEdgeLocation_shouldReturnTrueForNotCentralLocation() {
121         locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
122         locationService.addDcaeLocation(createDcaeLocation(LOCATION_B, "centralLayer"));
123
124         assertTrue(locationService.isEdgeLocation(LOCATION_A));
125         assertFalse(locationService.isEdgeLocation(LOCATION_B));
126     }
127
128     @Test
129     public void isEdgeLocation_shouldReturnFalseWhenLocationDoesNotExist() {
130         locationService.addDcaeLocation(createDcaeLocation(LOCATION_A, "layerA"));
131
132         assertFalse(locationService.isEdgeLocation("not_existing_location"));
133     }
134
135     private DcaeLocation createDcaeLocation(String locationName) {
136         return createDcaeLocation(locationName, "dcaeLayer");
137     }
138
139     private DcaeLocation createDcaeLocation(String locationName, String dcaeLayer) {
140         return new DcaeLocation("clli", dcaeLayer, locationName, "openStackAvailabilityZone", "subnet");
141     }
142
143
144 }