diff options
author | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2019-01-11 22:07:56 -0500 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2019-01-11 22:12:54 -0500 |
commit | fa90fa8c70668f23d6c3953496293af447495e09 (patch) | |
tree | e25385a1c6b8ee7eb691bce8e26d9fdd42e4a98c /bpmn/so-bpmn-tasks/src/main | |
parent | de5958dddd97b086ec8b603974f434fd814386ac (diff) |
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>
Diffstat (limited to 'bpmn/so-bpmn-tasks/src/main')
4 files changed, 43 insertions, 11 deletions
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 f91ad44f2b..6bee5a9b1e 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.HttpMethod; 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 @@ public class NamingClient{ @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 ab0639574c..e790fa4b72 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.MessageEnum; 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 @@ public class NamingClientResponseValidator { 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 c3f216e288..82bad8db79 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 class NamingRequestObjectBuilder{ 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 8b443a165b..d0bf6da6c9 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 @@ public class NamingServiceResources { @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); |