killbill-uncached

Details

diff --git a/jaxrs/src/main/java/org/killbill/billing/jaxrs/util/JaxrsUriBuilder.java b/jaxrs/src/main/java/org/killbill/billing/jaxrs/util/JaxrsUriBuilder.java
index 1ea74af..6146fe7 100644
--- a/jaxrs/src/main/java/org/killbill/billing/jaxrs/util/JaxrsUriBuilder.java
+++ b/jaxrs/src/main/java/org/killbill/billing/jaxrs/util/JaxrsUriBuilder.java
@@ -38,11 +38,13 @@ public class JaxrsUriBuilder {
 
     public URI buildLocation(final UriInfo uriInfo, final Class<? extends JaxrsResource> theClass,
                              final String getMethodName, final Object objectId, final boolean pathLikeUrl) {
-        final UriBuilder uriBuilder =
-                pathLikeUrl ? getUriBuilder(uriInfo.getBaseUri().getPath(), theClass, getMethodName) :
-                getUriBuilder(theClass, getMethodName).scheme(uriInfo.getAbsolutePath().getScheme())
-                                                      .host(uriInfo.getAbsolutePath().getHost())
-                                                      .port(uriInfo.getAbsolutePath().getPort());
+        final UriBuilder uriBuilder = getUriBuilder(uriInfo.getBaseUri().getPath(), theClass, getMethodName);
+
+        if (!pathLikeUrl) {
+            uriBuilder.scheme(uriInfo.getAbsolutePath().getScheme())
+              .host(uriInfo.getAbsolutePath().getHost())
+              .port(uriInfo.getAbsolutePath().getPort());
+        }
         return objectId != null ? uriBuilder.build(objectId) : uriBuilder.build();
     }
 
diff --git a/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestBuildResponse.java b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestBuildResponse.java
new file mode 100644
index 0000000..b9eb768
--- /dev/null
+++ b/profiles/killbill/src/test/java/org/killbill/billing/jaxrs/TestBuildResponse.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014 Groupon, Inc
+ * Copyright 2014 The Billing Project, LLC
+ *
+ * The Billing Project licenses this file to you 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.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.killbill.billing.jaxrs;
+
+import org.killbill.billing.jaxrs.resources.AccountResource;
+import org.killbill.billing.jaxrs.util.JaxrsUriBuilder;
+import org.testng.annotations.Test;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import java.net.URI;
+import java.util.UUID;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+
+public class TestBuildResponse {
+    @Test(groups = "fast", description = "Tests Uri Builder with Path Like URL and Root location")
+    public void testUriBuilderWithPathLikeUrlAndRoot() throws Exception {
+        UUID objectId = UUID.randomUUID();
+
+        final UriInfo uriInfo = mock(UriInfo.class);
+        URI uri = URI.create("http://localhost:8080");
+        when(uriInfo.getBaseUri()).thenReturn(uri);
+
+        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
+        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, true);
+
+        assertEquals(response.getMetadata().get("Location").get(0), "/1.0/kb/accounts/" + objectId.toString());
+    }
+
+    @Test(groups = "fast", description = "Tests Uri Builder with Path Like URL and non root Location")
+    public void testUriBuilderWithPathLikeUrlAndNonRoot() throws Exception {
+        UUID objectId = UUID.randomUUID();
+
+        final UriInfo uriInfo = mock(UriInfo.class);
+        URI uri = URI.create("http://localhost:8080/killbill");
+        when(uriInfo.getBaseUri()).thenReturn(uri);
+
+        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
+        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, true);
+
+        assertEquals(response.getMetadata().get("Location").get(0), "/killbill/1.0/kb/accounts/" + objectId.toString());
+    }
+
+    @Test(groups = "fast", description = "Tests Uri Builder without Path Like URL and root Location")
+    public void testUriBuilderWithoutPathLikeUrlAndRoot() throws Exception {
+        UUID objectId = UUID.randomUUID();
+
+        final UriInfo uriInfo = mock(UriInfo.class);
+        URI uri = URI.create("http://localhost:8080");
+        when(uriInfo.getBaseUri()).thenReturn(uri);
+        when(uriInfo.getAbsolutePath()).thenReturn(uri);
+
+        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
+        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, false);
+
+        assertEquals(response.getMetadata().get("Location").get(0).toString(), uri.toString() + "/1.0/kb/accounts/" + objectId.toString());
+    }
+
+    @Test(groups = "fast", description = "Tests Uri Builder without Path Like URL and root Location")
+    public void testUriBuilderWithoutPathLikeUrlAndNonRoot() throws Exception {
+        UUID objectId = UUID.randomUUID();
+
+        final UriInfo uriInfo = mock(UriInfo.class);
+        URI uri = URI.create("http://localhost:8080/killbill");
+        when(uriInfo.getBaseUri()).thenReturn(uri);
+        when(uriInfo.getAbsolutePath()).thenReturn(uri);
+
+        JaxrsUriBuilder uriBuilder = new JaxrsUriBuilder();
+        Response response = uriBuilder.buildResponse(uriInfo, AccountResource.class, "getAccount", objectId, false);
+
+        assertEquals(response.getMetadata().get("Location").get(0).toString(), uri.toString() + "/1.0/kb/accounts/" + objectId.toString());
+    }
+}