Skip to content

Commit

Permalink
Update ProjectGenerationStatPublisherTests to use actual timestamp
Browse files Browse the repository at this point in the history
Instead of checking that the timestamp was between 2 seconds ago and now,
we can get the actual timestamp from the event and assert on that.

Closes gh-954
  • Loading branch information
mbhave committed Jul 12, 2019
1 parent 218fc20 commit dd43fb5
Showing 1 changed file with 32 additions and 36 deletions.
Expand Up @@ -19,8 +19,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;
Expand Down Expand Up @@ -132,11 +130,12 @@ private void testAuthorization(String expectedUri, RequestMatcher authorizationM
ProjectRequest request = createProjectRequest();
request.setGroupId("com.example.foo");
request.setArtifactId("my-project");
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo(expectedUri)).andExpect(method(HttpMethod.POST))
.andExpect(authorizationMatcher)
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));
handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -152,13 +151,13 @@ void publishDocument() {
request.getParameters().put("user-agent", "curl/1.2.4");
request.getParameters().put("cf-connecting-ip", "10.0.0.42");
request.getParameters().put("cf-ipcountry", "BE");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-simple.json"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-simple.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -171,13 +170,13 @@ void publishDocumentWithNoClientInformation() {
request.setBootVersion("2.1.0.RELEASE");
request.setDependencies(Arrays.asList("web", "data-jpa"));
request.setLanguage("java");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-no-client.json"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-no-client.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -190,13 +189,14 @@ void publishDocumentWithInvalidType() {
request.setBootVersion("2.1.0.RELEASE");
request.setDependencies(Arrays.asList("web", "data-jpa"));
request.setLanguage("java");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-invalid-type.json"))
.andExpect(method(HttpMethod.POST))
.andExpect(json("stat/request-invalid-type.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -209,13 +209,14 @@ void publishDocumentWithInvalidLanguage() {
request.setBootVersion("2.1.0.RELEASE");
request.setDependencies(Arrays.asList("web", "data-jpa"));
request.setLanguage("c");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-invalid-language.json"))
.andExpect(method(HttpMethod.POST))
.andExpect(json("stat/request-invalid-language.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -229,13 +230,14 @@ void publishDocumentWithInvalidJavaVersion() {
request.setDependencies(Arrays.asList("web", "data-jpa"));
request.setLanguage("java");
request.setJavaVersion("1.2");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-invalid-java-version.json"))
.andExpect(method(HttpMethod.POST))
.andExpect(json("stat/request-invalid-java-version.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -248,20 +250,21 @@ void publishDocumentWithInvalidDependencies() {
request.setBootVersion("2.1.0.RELEASE");
request.setDependencies(Arrays.asList("invalid-2", "web", "invalid-1"));
request.setLanguage("java");

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andExpect(json("stat/request-invalid-dependencies.json"))
.andExpect(method(HttpMethod.POST))
.andExpect(json("stat/request-invalid-dependencies.json", event.getTimestamp()))
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

@Test
void recoverFromError() {
ProjectRequest request = createProjectRequest();

ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));

Expand All @@ -273,13 +276,14 @@ void recoverFromError() {
.andRespond(withStatus(HttpStatus.CREATED).body(mockResponse(UUID.randomUUID().toString(), true))
.contentType(MediaType.APPLICATION_JSON));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

@Test
void fatalErrorOnlyLogs() {
ProjectRequest request = createProjectRequest();
ProjectGeneratedEvent event = new ProjectGeneratedEvent(request, this.metadata);
this.retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, Collections.singletonMap(Exception.class, true)));

this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
Expand All @@ -288,7 +292,7 @@ void fatalErrorOnlyLogs() {
this.mockServer.expect(requestTo("http://example.com/elastic/initializr/request"))
.andExpect(method(HttpMethod.POST)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));

handleEvent(request);
this.statPublisher.handleEvent(event);
this.mockServer.verify();
}

Expand All @@ -298,10 +302,6 @@ private WebProjectRequest createProjectRequest() {
return request;
}

private void handleEvent(ProjectRequest request) {
this.statPublisher.handleEvent(new ProjectGeneratedEvent(request, this.metadata));
}

private static String mockResponse(String id, boolean created) {
return "{\"_index\":\"initializr\",\"_type\":\"request\",\"_id\":\"" + id + "\",\"_version\":1,\"_shards\""
+ ":{\"total\":1,\"successful\":1,\"failed\":0},\"created\":" + created + "}";
Expand All @@ -314,10 +314,10 @@ private static StatsProperties createProperties() {
return properties;
}

private static RequestMatcher json(String location) {
private static RequestMatcher json(String location, long expectedTimestamp) {
return (request) -> {
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
assertJsonContent(readJson(location), mockRequest.getBodyAsString());
assertJsonContent(readJson(location), mockRequest.getBodyAsString(), expectedTimestamp);
};
}

Expand All @@ -332,14 +332,10 @@ private static String readJson(String location) {
}
}

private static void assertJsonContent(String expected, String actual) {
private static void assertJsonContent(String expected, String actual, long expectedTimestamp) {
try {
JSONAssert.assertEquals(expected, actual, new CustomComparator(JSONCompareMode.STRICT,
Customization.customization("generationTimestamp", (o1, o2) -> {
Instant timestamp = Instant.ofEpochMilli((long) o1);
return timestamp.isAfter(Instant.now().minus(2, ChronoUnit.SECONDS))
&& timestamp.isBefore(Instant.now());
})));
Customization.customization("generationTimestamp", (o1, o2) -> (long) o1 == expectedTimestamp)));
}
catch (JSONException ex) {
throw new AssertionError("Failed to parse expected or actual JSON request content", ex);
Expand Down

0 comments on commit dd43fb5

Please sign in to comment.