Skip to content
This repository has been archived by the owner on Jan 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #23 from openregister/fix-primary-key-assumption
Browse files Browse the repository at this point in the history
Fix primary key assumption
  • Loading branch information
om-sharma committed Jul 31, 2015
2 parents 2c924e8 + a4f85e0 commit 0892593
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import java.net.URI;

public abstract class ResourceBase {
public static final int ENTRY_LIMIT = 100;
Expand All @@ -12,15 +11,12 @@ public abstract class ResourceBase {
//Note: copied the logic to fetch primary key from alpha register.
//Note: We might need to change the logic of extracting register primary key for beta registers
protected String getRegisterPrimaryKey() {
try {
String host = new URI(httpServletRequest.getRequestURL().toString()).getHost();
String host = httpServletRequest.getHeader("Host");

//hack for functional tests
if (host.startsWith("localhost")) return "ft_test_pkey";
else return host.replaceAll("([^\\.]+)\\.(openregister)\\..*", "$1");

} catch (Exception e) {
throw new RuntimeException(e);
if (host.contains(".")) {
return host.substring(0, host.indexOf("."));
} else {
return host;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class CsvRepresentationTest extends FunctionalTestBase{
@BeforeClass
public static void publishTestMessages() {
publishMessagesToDB(ImmutableList.of(
"{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"123,45\"}}",
"{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft_test_pkey\":\"6789\"}}",
"{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"145678\"}}"
"{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"123,45\"}}",
"{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft-test-pkey\":\"6789\"}}",
"{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"145678\"}}"
));
}

Expand All @@ -24,14 +24,14 @@ public void csvRepresentationIsSupportedForEntries() {
Response response = getRequest("/all.csv");

assertThat(response.getHeaderString("Content-Type"), equalTo("text/csv;charset=utf-8"));
assertThat(response.readEntity(String.class), equalTo("hash,name,ft_test_pkey\r\nhash2,presley,6789\r\nhash3,ellis,145678\r\nhash1,ellis,\"123,45\"\r\n"));
assertThat(response.readEntity(String.class), equalTo("hash,name,ft-test-pkey\r\nhash2,presley,6789\r\nhash3,ellis,145678\r\nhash1,ellis,\"123,45\"\r\n"));
}

@Test
public void tsvRepresentationIsSupportedForEntries() {
Response response = getRequest("/all.tsv");

assertThat(response.getHeaderString("Content-Type"), equalTo("text/tab-separated-values;charset=utf-8"));
assertThat(response.readEntity(String.class), equalTo("hash\tname\tft_test_pkey\nhash2\tpresley\t6789\nhash3\tellis\t145678\nhash1\tellis\t123,45\n"));
assertThat(response.readEntity(String.class), equalTo("hash\tname\tft-test-pkey\nhash2\tpresley\t6789\nhash3\tellis\t145678\nhash1\tellis\t123,45\n"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ public class FindEntityTest extends FunctionalTestBase {
@BeforeClass
public static void publishTestMessages() {
publishMessagesToDB(ImmutableList.of(
"{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"12345\"}}",
"{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft_test_pkey\":\"6789\"}}",
"{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"145678\"}}"
"{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"12345\"}}",
"{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft-test-pkey\":\"6789\"}}",
"{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"145678\"}}"
));
OBJECT_MAPPER = Jackson.newObjectMapper();
}

@Test
public void findByPrimaryKey_shouldReturnEntryWithThPrimaryKey() {
Response response = getRequest("/ft_test_pkey/12345.json");
Response response = getRequest("/ft-test-pkey/12345.json");

assertThat(response.readEntity(String.class), equalTo("{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"12345\"}}"));
assertThat(response.readEntity(String.class), equalTo("{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"12345\"}}"));
}

@Test
Expand All @@ -48,7 +48,7 @@ public void findByHash_shouldReturnEntryForTheGivenHash() throws IOException {
Response response = getRequest("/hash/hash2.json");

assertThat(OBJECT_MAPPER.readValue(response.readEntity(String.class), JsonNode.class),
equalTo(OBJECT_MAPPER.readValue("{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft_test_pkey\":\"6789\"}}", JsonNode.class)));
equalTo(OBJECT_MAPPER.readValue("{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft-test-pkey\":\"6789\"}}", JsonNode.class)));
}

@Test
Expand All @@ -57,15 +57,15 @@ public void all_shouldReturnAllCurrentVersionsOnly() throws InterruptedException

String jsonResponse = response.readEntity(String.class);
assertThat(OBJECT_MAPPER.readValue(jsonResponse, JsonNode.class),
equalTo(OBJECT_MAPPER.readValue("[{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft_test_pkey\":\"6789\"}},{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"12345\"}}]", JsonNode.class)));
equalTo(OBJECT_MAPPER.readValue("[{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft-test-pkey\":\"6789\"}},{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"12345\"}}]", JsonNode.class)));
}

@Test
public void search_returnsAllMatchingEntries() {
Response response = getRequest("/search.json?name=ellis");

assertThat(response.readEntity(String.class), equalTo(
"[{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"12345\"}}]"
"[{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"12345\"}}]"
));
}

Expand All @@ -74,7 +74,7 @@ public void search_returnsAllEntriesWhenNoSearchQueryIsGiven() {
Response response = getRequest("/search.json");

assertThat(response.readEntity(String.class), equalTo(
"[{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft_test_pkey\":\"6789\"}},{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft_test_pkey\":\"12345\"}}]"
"[{\"hash\":\"hash2\",\"entry\":{\"name\":\"presley\",\"ft-test-pkey\":\"6789\"}},{\"hash\":\"hash3\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"145678\"}},{\"hash\":\"hash1\",\"entry\":{\"name\":\"ellis\",\"ft-test-pkey\":\"12345\"}}]"
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void beforeClass() throws InterruptedException {
}

Response getRequest(String path) {
return client.target(String.format("http://localhost:%d%s", APPLICATION_PORT, path)).request().get();
return client.target(String.format("http://localhost:%d%s", APPLICATION_PORT, path)).request().header("Host","ft-test-pkey.beta.openregister.org").get();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public class TurtleRepresentationTest extends FunctionalTestBase {
@BeforeClass
public static void publishTestMessages() {
publishMessagesToDB(ImmutableList.of(
"{\"hash\":\"someHash1\",\"entry\":{\"name\":\"The Entry 1\", \"key1\":\"value1\", \"ft_test_pkey\":\"12345\"}}",
"{\"hash\":\"someHash2\",\"entry\":{\"name\":\"The Entry 2\", \"key1\":\"value2\", \"ft_test_pkey\":\"67890\"}}"
"{\"hash\":\"someHash1\",\"entry\":{\"name\":\"The Entry 1\", \"key1\":\"value1\", \"ft-test-pkey\":\"12345\"}}",
"{\"hash\":\"someHash2\",\"entry\":{\"name\":\"The Entry 2\", \"key1\":\"value2\", \"ft-test-pkey\":\"67890\"}}"
));
}

public static final String EXPECTED_SINGLE_RECORD = "<http://localhost:9000/hash/someHash1>;\n" +
" key1 \"value1\" ;\n" +
" name \"The Entry 1\" ;\n" +
" ft_test_pkey \"12345\" .\n";
" ft-test-pkey \"12345\" .\n";


public static final String TEXT_TURTLE = "text/turtle;charset=utf-8";
Expand All @@ -38,7 +38,7 @@ public void turtleRepresentationIsSupportedForSingleEntryView() {
"<http://localhost:9000/hash/someHash2>;\n" +
" key1 \"value2\" ;\n" +
" name \"The Entry 2\" ;\n" +
" ft_test_pkey \"67890\" .\n"
" ft-test-pkey \"67890\" .\n"
+ EXPECTED_SINGLE_RECORD;

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.register.presentation.resource;

import org.junit.Test;

import javax.servlet.http.HttpServletRequest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ResourceBaseTest {
@Test
public void takesRegisterNameFromHttpHost() throws Exception {
ResourceBase resourceBase = new TestResourceBase("school.beta.openregister.org");

String registerPrimaryKey = resourceBase.getRegisterPrimaryKey();

assertThat(registerPrimaryKey, equalTo("school"));
}

@Test
public void behavesGracefullyWhenGivenHostWithNoDots() throws Exception {
ResourceBase resourceBase = new TestResourceBase("school");

String registerPrimaryKey = resourceBase.getRegisterPrimaryKey();

assertThat(registerPrimaryKey, equalTo("school"));
}

public class TestResourceBase extends ResourceBase {
public TestResourceBase(String hostHeader) {
this.httpServletRequest = mock(HttpServletRequest.class);
when(httpServletRequest.getHeader("Host")).thenReturn(hostHeader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void findByPrimaryKey_throwsNotFoundException_whenSearchedKeyIsNotPrimary
SearchResource resource = new SearchResource(queryDAO);
resource.httpServletRequest = httpServletRequest;

when(httpServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost:9999/someOtherKey/value"));
when(httpServletRequest.getHeader("Host")).thenReturn("localhost");
try {
resource.findByPrimaryKey("someOtherKey", "value");
fail("Must fail");
Expand All @@ -50,7 +50,7 @@ public void findByPrimaryKey_throwsNotFoundException_whenSearchedKeyIsNotFound()
SearchResource resource = new SearchResource(queryDAO);
resource.httpServletRequest = httpServletRequest;

when(httpServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://school.openregister.org/school/value"));
when(httpServletRequest.getHeader("Host")).thenReturn("school.openregister.org");
when(queryDAO.findByKeyValue("school", "value")).thenReturn(Optional.<Entry>absent());
try {
resource.findByPrimaryKey("school", "value");
Expand All @@ -66,7 +66,7 @@ public void findByHash_throwsNotFoundWhenHashIsNotFound() {
SearchResource resource = new SearchResource(queryDAO);
resource.httpServletRequest = httpServletRequest;

when(httpServletRequest.getRequestURL()).thenReturn(new StringBuffer("http://school.openregister.org/hash/123"));
when(httpServletRequest.getHeader("Host")).thenReturn("school.openregister.org");
when(queryDAO.findByHash("123")).thenReturn(Optional.<Entry>absent());
try {
resource.findByHash("123");
Expand Down

0 comments on commit 0892593

Please sign in to comment.