Bug fix - SO GR Api sends wrong reposnse after service instantiation

Issue-ID: SO-2836
Signed-off-by: Krzysztof Gajewski <krzysztof.gajewski@nokia.com>
Change-Id: Ib37b7972a939cd353a6cbb0370aed126534315d4
diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java
index c806e9f..fec512a 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandler.java
@@ -163,12 +163,14 @@
         try {
             URL aUrl = new URL(url);
             String aPath = aUrl.getPath();
-            if (aPath.indexOf("/v") == -1) {
-                version = aPath.substring(aPath.indexOf("/V"), aPath.indexOf("/V") + 4);
-            } else {
-                version = aPath.substring(aPath.indexOf("/v"), aPath.indexOf("/v") + 4);
-            }
-            String selfLinkPath = Constants.ORCHESTRATION_REQUESTS_PATH.concat(version).concat(requestId);
+            int indexOfVersion = Math.max(aPath.indexOf("/V"), aPath.indexOf("/v"));
+            version = aPath.substring(indexOfVersion, indexOfVersion + 4);
+
+            String pathWithSOAction = aPath.substring(0, indexOfVersion);
+            String pathWithoutSOAction = pathWithSOAction.substring(0, pathWithSOAction.lastIndexOf("/"));
+
+            String selfLinkPath =
+                    pathWithoutSOAction.concat(Constants.ORCHESTRATION_REQUESTS_PATH).concat(version).concat(requestId);
             selfLinkUrl = Optional.of(new URL(aUrl.getProtocol(), aUrl.getHost(), aUrl.getPort(), selfLinkPath));
         } catch (Exception e) {
             selfLinkUrl = Optional.empty(); // ignore
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/BaseTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/BaseTest.java
index 5da16f4..0ca9888 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/BaseTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/BaseTest.java
@@ -70,6 +70,10 @@
         return "http://localhost:" + port + uri;
     }
 
+    protected String createURLWithPort(String uri, String orchestrationPath) {
+        return "http://localhost:" + port + orchestrationPath + uri;
+    }
+
     protected String createURLWithPort(String uri, int iPort) {
         return "http://localhost:" + iPort + uri;
     }
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/InstanceManagementTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/InstanceManagementTest.java
index 33ed5fa..7b2e502 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/InstanceManagementTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/InstanceManagementTest.java
@@ -71,6 +71,7 @@
     private String wiremockPort;
 
     private final String instanceManagementUri = "/onap/so/infra/instanceManagement/";
+    private final String orchestration_path = "/onap/so/infra";
 
     private String uri;
     private URL selfLink;
@@ -92,7 +93,7 @@
         headers.set(ONAP_PARTNER_NAME, "VID");
         headers.set(REQUESTOR_ID, "xxxxxx");
         try { // generate one-time port number to avoid RANDOM port number later.
-            initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
+            initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH, orchestration_path));
             initialPort = initialUrl.getPort();
         } catch (MalformedURLException e) {
             e.printStackTrace();
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/ServiceInstancesTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/ServiceInstancesTest.java
index ef90b22..1910b54 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/ServiceInstancesTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/ServiceInstancesTest.java
@@ -97,6 +97,8 @@
 
     private final String servInstanceuri = "/onap/so/infra/serviceInstantiation/";
     private final String servInstanceUriPrev7 = "/onap/so/infra/serviceInstances/";
+    private final String orchestration_path = "/onap/so/infra";
+
     private String uri;
     private URL selfLink;
     private URL initialUrl;
@@ -115,7 +117,7 @@
         headers.set(ONAP_PARTNER_NAME, "VID");
         headers.set(REQUESTOR_ID, "xxxxxx");
         try { // generate one-time port number to avoid RANDOM port number later.
-            initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH));
+            initialUrl = new URL(createURLWithPort(Constants.ORCHESTRATION_REQUESTS_PATH, orchestration_path));
             initialPort = initialUrl.getPort();
         } catch (MalformedURLException e) {
             e.printStackTrace();
diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandlerTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandlerTest.java
index d39192c..6c643c7 100644
--- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandlerTest.java
+++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/infra/rest/handler/AbstractRestHandlerTest.java
@@ -61,4 +61,15 @@
                 restHandler.createResponse("instanceId", "requestId", mockRequestContext);
         assertThat(actualResponse, sameBeanAs(expectedResponse));
     }
+
+    @Test
+    public void test_buildSelfLinkUrl() throws MalformedURLException {
+        String initialLink = "http://some.domain.com:30277/onap/so/infra/serviceInstantiation/v7/serviceInstances";
+        String requestId = "4d0437c3-ee48-4361-a4f7-e1613c82493a";
+        Optional<URL> expectedLink = Optional.of(new URL(
+                "http://some.domain.com:30277/onap/so/infra/orchestrationRequests/v7/4d0437c3-ee48-4361-a4f7-e1613c82493a"));
+        Optional<URL> resultURL = restHandler.buildSelfLinkUrl(initialLink, requestId);
+
+        assertThat(resultURL, sameBeanAs(expectedLink));
+    }
 }