naming ms client fixes

Fixed failing unit test for request object builder
Added throws declaration for IOException
Changed endpoint to point to wiremock
Added exception handling for httpstatuscode
Reverted changes to validator
Added exception handling for response from naming mS
Changed how element object properties are set
Changed yaml to point to naming mS dev env
Began adding unit tests for naming client

Change-Id: Ic753e0dbaed52065ff5c0dfc9a59a726fdbdc642
Issue-ID: SO-1387
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClient.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClient.java
index f91ad44..6bee5a9 100644
--- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClient.java
+++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClient.java
@@ -1,5 +1,6 @@
 package org.onap.so.client.namingservice;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -17,6 +18,7 @@
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 
 
@@ -34,21 +36,31 @@
 	@Autowired
 	private NamingClientResponseValidator namingClientResponseValidator;
 	
-	public String postNameGenRequest(NameGenRequest request) throws BadResponseException {
+	public String postNameGenRequest(NameGenRequest request) throws BadResponseException, IOException {
 		String targetUrl = env.getProperty(ENDPOINT);
 		HttpHeaders headers = setHeaders(env.getProperty(AUTH)); 
 		msoLogger.info("Sending postNameGenRequest to url: " + targetUrl);
 		HttpEntity<NameGenRequest> requestEntity = new HttpEntity<>(request, headers);
-		ResponseEntity<NameGenResponse> response = restTemplate.postForEntity(targetUrl, requestEntity, NameGenResponse.class);
+		ResponseEntity<NameGenResponse> response;
+		try{
+			 response = restTemplate.postForEntity(targetUrl, requestEntity, NameGenResponse.class);
+		}catch(HttpStatusCodeException e){
+			throw new BadResponseException(namingClientResponseValidator.formatError(e));
+		}
 		return namingClientResponseValidator.validateNameGenResponse(response);
 	}
 
-	public String deleteNameGenRequest(NameGenDeleteRequest request) throws BadResponseException {
+	public String deleteNameGenRequest(NameGenDeleteRequest request) throws BadResponseException, IOException {
 		String targetUrl = env.getProperty(ENDPOINT);
 		HttpHeaders headers = setHeaders(env.getProperty(AUTH)); 
 		msoLogger.info("Sending deleteNameGenRequest to url: " + targetUrl);
 		HttpEntity<NameGenDeleteRequest> requestEntity = new HttpEntity<>(request, headers);
-		ResponseEntity<NameGenDeleteResponse> response = restTemplate.exchange(targetUrl, HttpMethod.DELETE, requestEntity, NameGenDeleteResponse.class);
+		ResponseEntity<NameGenDeleteResponse> response;
+		try{
+			response = restTemplate.exchange(targetUrl, HttpMethod.DELETE, requestEntity, NameGenDeleteResponse.class);
+		}catch(HttpStatusCodeException e){
+			throw new BadResponseException(namingClientResponseValidator.formatError(e));
+		}
 		return namingClientResponseValidator.validateNameGenDeleteResponse(response);
 	}
 
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClientResponseValidator.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClientResponseValidator.java
index ab06395..e790fa4 100644
--- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClientResponseValidator.java
+++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingClientResponseValidator.java
@@ -1,5 +1,6 @@
 package org.onap.so.client.namingservice;
 
+import java.io.IOException;
 import java.util.List;
 
 import org.apache.http.HttpStatus;
@@ -12,6 +13,9 @@
 import org.onap.so.logger.MsoLogger;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpStatusCodeException;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
 
 @Component
 public class NamingClientResponseValidator {
@@ -99,5 +103,20 @@
 	private boolean isHttpCodeSuccess(int code) {
         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
     }
+	
+	protected String formatError(HttpStatusCodeException e) throws IOException {
+		ObjectMapper mapper = new ObjectMapper();
+		NameGenResponse errorResponse = mapper.readValue(e.getResponseBodyAsString(), NameGenResponse.class);
+		NameGenResponseError error = errorResponse.getError();
+		
+		String errorMessageString = null;
+		if (error != null) {
+			errorMessageString = error.getMessage();
+		}
+		String errorMessage = String.format(NAMING_SERVICE_ERROR, errorMessageString);
+		msoLogger.error(MessageEnum.RA_GENERAL_EXCEPTION, errorMessage, "BPMN", MsoLogger.getServiceName(),
+				MsoLogger.ErrorCode.DataError, errorMessage);
+		return errorMessage;
+	}
 
 }
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingRequestObjectBuilder.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingRequestObjectBuilder.java
index c3f216e..82bad8d 100644
--- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingRequestObjectBuilder.java
+++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/namingservice/NamingRequestObjectBuilder.java
@@ -13,11 +13,11 @@
 	
 	public Element elementMapper(String instanceGroupId, String policyInstanceName, String namingType, String nfNamingCode, String instanceGroupName){
 		Element element = new Element();
-		element.setExternalKey(instanceGroupId);
-		element.setPolicyInstanceName(policyInstanceName);
-		element.setNamingType(namingType);
-		element.setResourceName(instanceGroupName);
-		element.setNamingIngredientsZeroOrMore(nfNamingCode);
+		element.put("external-key", instanceGroupId);
+		element.put("policy-instance-name", policyInstanceName);
+		element.put("naming-type", namingType);
+		element.put("resource-name", instanceGroupName);
+		element.put("nf-naming-code", nfNamingCode);
 		return element;
 	}
 	public Deleteelement deleteElementMapper(String instanceGroupId){
diff --git a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/NamingServiceResources.java b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/NamingServiceResources.java
index 8b443a1..d0bf6da 100644
--- a/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/NamingServiceResources.java
+++ b/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/client/orchestration/NamingServiceResources.java
@@ -20,6 +20,7 @@
 
 package org.onap.so.client.orchestration;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -44,14 +45,14 @@
 	@Autowired
 	private NamingRequestObjectBuilder namingRequestObjectBuilder;	
 	
-	public String generateInstanceGroupName(InstanceGroup instanceGroup, String policyInstanceName, String nfNamingCode) throws BadResponseException {
+	public String generateInstanceGroupName(InstanceGroup instanceGroup, String policyInstanceName, String nfNamingCode) throws BadResponseException, IOException {
 		Element element = namingRequestObjectBuilder.elementMapper(instanceGroup.getId(), policyInstanceName, NAMING_TYPE, nfNamingCode, instanceGroup.getInstanceGroupName());
 		List<Element> elements = new ArrayList<Element>();
 		elements.add(element);		
 		return(namingClient.postNameGenRequest(namingRequestObjectBuilder.nameGenRequestMapper(elements)));		
 	}
 	
-	public String deleteInstanceGroupName(InstanceGroup instanceGroup) throws BadResponseException {		
+	public String deleteInstanceGroupName(InstanceGroup instanceGroup) throws BadResponseException, IOException {		
 		Deleteelement deleteElement = namingRequestObjectBuilder.deleteElementMapper(instanceGroup.getId());
 		List<Deleteelement> deleteElements = new ArrayList<Deleteelement>();
 		deleteElements.add(deleteElement);		
diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingClientTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingClientTest.java
new file mode 100644
index 0000000..c1769e3
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingClientTest.java
@@ -0,0 +1,99 @@
+package org.onap.so.client.namingservice;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.delete;
+import static com.github.tomakehurst.wiremock.client.WireMock.post;
+import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.HttpStatus;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onap.namingservice.model.Deleteelement;
+import org.onap.namingservice.model.Element;
+import org.onap.namingservice.model.NameGenDeleteRequest;
+import org.onap.namingservice.model.NameGenRequest;
+import org.onap.so.BaseIntegrationTest;
+import org.onap.so.client.exception.BadResponseException;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+public class NamingClientTest extends BaseIntegrationTest{
+	@Autowired
+	NamingClient client;
+	@Autowired
+	NamingRequestObjectBuilder requestBuilder;
+	@Rule
+	public ExpectedException thrown = ExpectedException.none();
+	
+	@Test
+	public void assignNameGenRequest() throws BadResponseException, IOException{
+		stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
+				.willReturn(aResponse().withHeader("Content-Type", "application/json")
+				.withBodyFile("NamingClient/AssignResponse.json")
+				.withStatus(HttpStatus.SC_ACCEPTED)));
+		
+		NameGenRequest request = assignSetup();
+		String response = client.postNameGenRequest(request);
+		assertTrue(response.equals("$vnf-name"));
+	}
+	@Test
+	public void assignNameGenRequestError() throws BadResponseException, IOException{
+		stubFor(post(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
+				.willReturn(aResponse().withHeader("Content-Type", "application/json")
+				.withBodyFile("NamingClient/ErrorResponse.json")
+				.withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
+		
+		thrown.expect(BadResponseException.class);
+		thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
+		NameGenRequest request = assignSetup();
+		client.postNameGenRequest(request);
+	}
+	@Test
+	public void unassignNameGenRequest() throws BadResponseException, IOException{
+		stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
+				.willReturn(aResponse().withHeader("Content-Type", "application/json")
+				.withBodyFile("NamingClient/UnassignResponse.json")
+				.withStatus(HttpStatus.SC_ACCEPTED)));
+		
+		String response = client.deleteNameGenRequest(unassignSetup());
+		assertTrue(response.equals(""));
+	}
+	@Test
+	public void unassignNameGenRequestError() throws BadResponseException, IOException{
+		stubFor(delete(urlPathEqualTo("/web/service/v1/genNetworkElementName"))
+				.willReturn(aResponse().withHeader("Content-Type", "application/json")
+				.withBodyFile("NamingClient/ErrorResponse.json")
+				.withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
+		
+		thrown.expect(BadResponseException.class);
+		thrown.expectMessage("Error from Naming Service: External Key is required and must be unique");
+		client.deleteNameGenRequest(unassignSetup());
+	}
+	
+	public NameGenRequest assignSetup() throws JsonProcessingException{
+		NameGenRequest request = new NameGenRequest();
+		List<Element> elements = new ArrayList<>();
+		Element testElement = new Element();
+		testElement = requestBuilder.elementMapper("SomeUniqueValue", "SDNC_Policy.Config_MS_1806SRIOV_VNATJson.4.xml", "VNF", "nfNamingCode", "vnf_name");
+		elements.add(testElement);
+		request = requestBuilder.nameGenRequestMapper(elements);
+		return request;
+	}
+	public NameGenDeleteRequest unassignSetup() throws JsonProcessingException{
+		NameGenDeleteRequest request = new NameGenDeleteRequest();
+		List<Deleteelement> deleteElements = new ArrayList<>();
+		Deleteelement testElement = new Deleteelement();
+		testElement = requestBuilder.deleteElementMapper("instanceGroupId");
+		deleteElements.add(testElement);
+		request = requestBuilder.nameGenDeleteRequestMapper(deleteElements);
+		return request;
+	}
+}
\ No newline at end of file
diff --git a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingRequestObjectBuilderTest.java b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingRequestObjectBuilderTest.java
index 0a2dc1e..27839d6 100644
--- a/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingRequestObjectBuilderTest.java
+++ b/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/client/namingservice/NamingRequestObjectBuilderTest.java
@@ -26,11 +26,11 @@
 	public void elementMapperTest(){
 		// Expected element
 		Element expected = new Element();
-		expected.setExternalKey(instanceGroupId);
-		expected.setPolicyInstanceName(policyInstanceName);
-		expected.setNamingType(namingType);
-		expected.setResourceName(instanceGroupName);
-		expected.setNamingIngredientsZeroOrMore(nfNamingCode);
+		expected.put("external-key", instanceGroupId);
+		expected.put("policy-instance-name", policyInstanceName);
+		expected.put("naming-type", namingType);
+		expected.put("resource-name", instanceGroupName);
+		expected.put("nf-naming-code", nfNamingCode);
 		
 		// Actual element
 		Element actual = mapper.elementMapper(instanceGroupId, policyInstanceName, namingType, nfNamingCode, instanceGroupName);
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/AssignResponse.json b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/AssignResponse.json
new file mode 100644
index 0000000..b065c21
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/AssignResponse.json
@@ -0,0 +1,14 @@
+{
+  "elements": [
+    {
+      "external-key": "$vnf-id",
+      "resource-name": "instance-group-name",
+      "resource-value": "$vnf-name"
+    },
+    {
+      "external-key": "$vnf-id",
+      "resource-name": "vm-name",
+      "resource-value": "$vm-name"
+    }
+  ]
+}
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/ErrorResponse.json b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/ErrorResponse.json
new file mode 100644
index 0000000..8bac7cd
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/ErrorResponse.json
@@ -0,0 +1,6 @@
+{
+    "error": {
+        "errorId": "NELGEN-0003",
+        "message": "External Key is required and must be unique"
+    }
+}
\ No newline at end of file
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/UnassignResponse.json b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/UnassignResponse.json
new file mode 100644
index 0000000..b065c21
--- /dev/null
+++ b/bpmn/so-bpmn-tasks/src/test/resources/__files/NamingClient/UnassignResponse.json
@@ -0,0 +1,14 @@
+{
+  "elements": [
+    {
+      "external-key": "$vnf-id",
+      "resource-name": "instance-group-name",
+      "resource-value": "$vnf-name"
+    },
+    {
+      "external-key": "$vnf-id",
+      "resource-name": "vm-name",
+      "resource-value": "$vm-name"
+    }
+  ]
+}
diff --git a/bpmn/so-bpmn-tasks/src/test/resources/application-test.yaml b/bpmn/so-bpmn-tasks/src/test/resources/application-test.yaml
index 3d45cad..fed2aa6 100644
--- a/bpmn/so-bpmn-tasks/src/test/resources/application-test.yaml
+++ b/bpmn/so-bpmn-tasks/src/test/resources/application-test.yaml
@@ -43,7 +43,7 @@
 mso:
   naming:
     endpoint: http://localhost:${wiremock.server.port}/web/service/v1/genNetworkElementName
-    auth: Basic YnBlbDptc28tZGItMTUwNyE=
+    auth: Basic YnBlbDptc28tZGItMTUwNyE= 
   adapters:
     requestDb:
       auth: Basic YnBlbDptc28tZGItMTUwNyE=