Merge "AaiController unit tests"
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/AicZones.java b/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/AicZones.java
index 4efaf05..77553a6 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/AicZones.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/AicZones.java
@@ -3,6 +3,7 @@
  * VID
  * ================================================================================
  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,11 +21,22 @@
 
 package org.onap.vid.aai.model.AaiGetAicZone;
 
+import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
+import java.util.Collections;
 import java.util.List;
 
-public class AicZones {
-	@JsonProperty("zone")
-	public List<Zone> zones;
+public final class AicZones {
+
+	private final List<Zone> zones;
+
+	@JsonCreator
+	public AicZones(@JsonProperty("zone") List<Zone> zones) {
+		this.zones = Collections.unmodifiableList(zones);
+	}
+
+	public List<Zone> getZones() {
+		return zones;
+	}
 }
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/Zone.java b/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/Zone.java
index 343a87f..c366180 100644
--- a/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/Zone.java
+++ b/vid-app-common/src/main/java/org/onap/vid/aai/model/AaiGetAicZone/Zone.java
@@ -3,13 +3,14 @@
  * VID
  * ================================================================================
  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nokia.
  * ================================================================================
  * 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,14 +21,27 @@
 
 package org.onap.vid.aai.model.AaiGetAicZone;
 
+import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
 @JsonIgnoreProperties(ignoreUnknown = true)
-public class Zone {
-    @JsonProperty("zone-id")
-    public String zoneId;
+public final class Zone {
 
-    @JsonProperty("zone-name")
-    public String zoneName;
+    private final String zoneId;
+    private final String zoneName;
+
+    @JsonCreator
+    public Zone(@JsonProperty("zone-id") String zoneId, @JsonProperty("zone-name") String zoneName) {
+        this.zoneId = zoneId;
+        this.zoneName = zoneName;
+    }
+
+    public String getZoneId() {
+        return zoneId;
+    }
+
+    public String getZoneName() {
+        return zoneName;
+    }
 }
diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java
index 6a3b4f9..ca40b7d 100644
--- a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java
+++ b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java
@@ -149,14 +149,14 @@
     private ResponseEntity<String> aaiResponseToResponseEntity(AaiResponse aaiResponseData)
         throws IOException {
         ResponseEntity<String> responseEntity;
-        ObjectMapper objectMapper = new ObjectMapper();
         if (aaiResponseData.getHttpCode() == 200) {
-            responseEntity = new ResponseEntity<>(objectMapper.writeValueAsString(aaiResponseData.getT()),
+            responseEntity = new ResponseEntity<>(new ObjectMapper().writeValueAsString(aaiResponseData.getT()),
                 HttpStatus.OK);
         } else {
             responseEntity = new ResponseEntity<>(aaiResponseData.getErrorMessage(),
                 HttpStatus.valueOf(aaiResponseData.getHttpCode()));
         }
+
         return responseEntity;
     }
 
diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java
index bd6e0f9..2f0d1cd 100644
--- a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java
@@ -27,6 +27,7 @@
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMultimap;
 import com.google.common.collect.Lists;
@@ -42,6 +43,8 @@
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigData;
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataError;
 import org.onap.vid.aai.AaiResponseTranslator.PortMirroringConfigDataOk;
+import org.onap.vid.aai.model.AaiGetAicZone.AicZones;
+import org.onap.vid.aai.model.AaiGetAicZone.Zone;
 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetails;
 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsError;
 import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk;
@@ -59,6 +62,7 @@
 
     private static final String ID_1 = "id1";
     private static final String ID_2 = "id2";
+    private final ObjectMapper objectMapper = new ObjectMapper();
     @Mock
     private AaiService aaiService;
     @Mock
@@ -67,7 +71,6 @@
     private RoleProvider roleProvider;
     @Mock
     private SystemPropertiesWrapper systemPropertiesWrapper;
-
     private MockMvc mockMvc;
     private AaiController aaiController;
 
@@ -93,7 +96,7 @@
                 .contentType(MediaType.APPLICATION_JSON)
                 .accept(MediaType.APPLICATION_JSON))
             .andExpect(status().isOk())
-            .andExpect(content().json(new ObjectMapper().writeValueAsString(expectedJson)));
+            .andExpect(content().json(objectMapper.writeValueAsString(expectedJson)));
     }
 
     @Test
@@ -112,7 +115,7 @@
                 .contentType(MediaType.APPLICATION_JSON)
                 .accept(MediaType.APPLICATION_JSON))
             .andExpect(status().isOk())
-            .andExpect(content().json(new ObjectMapper().writeValueAsString(expectedJson.asMap())));
+            .andExpect(content().json(objectMapper.writeValueAsString(expectedJson.asMap())));
     }
 
     @Test
@@ -136,5 +139,30 @@
             .andExpect(status().isOk())
             .andExpect(content().string(expectedResponseBody));
     }
+
+    @Test
+    public void getAicZones_shouldReturnAaiZones_whenAaiHttpStatusIsOK() throws Exception {
+        AicZones aicZones = new AicZones(ImmutableList.of(new Zone("TEST_ZONE_ID", "TEST_ZONE_NAME")));
+        given(aaiService.getAaiZones()).willReturn(new AaiResponse(aicZones, "", HttpStatus.OK.value()));
+
+        mockMvc.perform(get("/aai_get_aic_zones")
+            .contentType(MediaType.APPLICATION_JSON)
+            .accept(MediaType.APPLICATION_JSON))
+            .andExpect(status().isOk())
+            .andExpect(content().json(objectMapper.writeValueAsString(aicZones)));
+    }
+
+    @Test
+    public void getAicZones_shouldReturnErrorResponse_whenAaiHttpStatusOtherThanOK() throws Exception {
+        String expectedErrorMessage = "Calling AAI Failed";
+        given(aaiService.getAaiZones())
+            .willReturn(new AaiResponse(null, expectedErrorMessage, HttpStatus.INTERNAL_SERVER_ERROR.value()));
+
+        mockMvc.perform(get("/aai_get_aic_zones")
+            .contentType(MediaType.APPLICATION_JSON)
+            .accept(MediaType.APPLICATION_JSON))
+            .andExpect(status().isInternalServerError())
+            .andExpect(content().string(expectedErrorMessage));
+    }
 }