Skip to content
This repository has been archived by the owner on Dec 5, 2020. It is now read-only.

Fixed index checking logic + unit tests #173

Merged
merged 1 commit into from
Jan 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/amihaiemil/charles/aws/AmazonEsRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.http.HttpResponse;
import com.amihaiemil.charles.DataExportException;
import com.amihaiemil.charles.WebPage;
Expand Down Expand Up @@ -103,7 +107,15 @@ public boolean exists() {
)
)
);
return head.perform();
boolean exists = false;
try {
exists = head.perform();
} catch (AmazonServiceException ex) {
if (!(ex.getStatusCode() == HttpStatus.SC_NOT_FOUND)) {
throw ex;
}
}
return exists;
}
/**
* Delete the elasticsearch index.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
*/
package com.amihaiemil.charles.aws;

import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.io.IOUtils;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.http.HttpResponse;
import com.amazonaws.http.HttpResponseHandler;
Expand Down Expand Up @@ -58,7 +63,15 @@ public HttpResponse handle(HttpResponse response) {

int status = response.getStatusCode();
if(status < 200 || status >= 300) {
AmazonServiceException ase = new AmazonServiceException("Unexpected status: " + status);
String content;
final StringWriter writer = new StringWriter();
try {
IOUtils.copy(response.getContent(), writer, "UTF-8");
content = writer.toString();
} catch (final IOException e) {
content = "Couldn't get response content!";
}
AmazonServiceException ase = new AmazonServiceException(content);
ase.setStatusCode(status);
throw ase;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/amihaiemil/charles/github/Brain.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public Steps understand(Command com) throws IOException {
}
return new Steps(
steps,
this.logger,
new SendReply(
new TextReply(
com,
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/amihaiemil/charles/github/Steps.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package com.amihaiemil.charles.github;

import org.slf4j.Logger;


/**
* Steps taken to fulfill a command.
Expand All @@ -45,13 +47,20 @@ public class Steps implements Step {
*/
private SendReply failureMessage;

/**
* Action's logger.
*/
private Logger logger;

/**
* Constructor.
* @param steps Given steps.
* @param log Given logger.
* @param fm failure message in case any step fails.
*/
public Steps(Step steps, SendReply fm) {
public Steps(Step steps, Logger log, SendReply fm) {
this.steps = steps;
this.logger = log;
this.failureMessage = fm;
}

Expand All @@ -71,6 +80,7 @@ public void perform() {
try {
this.steps.perform();
} catch (RuntimeException ex) {
logger.error("A runtime exception occured", ex);
this.failureMessage.perform();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/responses_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ denied.deleteindex.comment=@%s the repository's name in a delete command has to
and it has to match the actual reponame exactly (case sensitive) \n\n\
A valid delete command here is ``@%s delete `%s` index``.

index.missing.comment=@%s there is no index for this repo, so your command\n\
cannot be fulfilled. Either the index was removed already\n\
index.missing.comment=@%s there is no index for this repo, so your command \
cannot be fulfilled. Either the index was removed already \
following a ``delete`` command, or it has never existed.\n\n\
Check the [logs](%s) for more details.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.amihaiemil.charles.aws;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -33,6 +34,7 @@
import java.util.HashSet;
import java.util.List;

import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Test;

Expand Down Expand Up @@ -209,6 +211,60 @@ public void sendsDeleteRequestToAwsEs() throws Exception {
}
}

/**
* AmazonEsRespository can tell if an index exists or not.
* @throws Exception If something goes wrong.
*/
@Test
public void tellsIfIndexExists() throws Exception {
System.setProperty("aws.accessKeyId", "access_key");
System.setProperty("aws.secretKey", "secret_key");
System.setProperty("aws.es.region", "ro");

int port = this.port();
System.setProperty("aws.es.endpoint", "http://localhost:" + port + "/es");

MkContainer server = new MkGrizzlyContainer()
.next(new MkAnswer.Simple(HttpStatus.SC_OK))
.start(port);
try {
boolean exists = new AmazonEsRepository("present.index").exists();
assertTrue(exists);
MkQuery request = server.take();
assertTrue("/es/present.index/".equals(request.uri().toString()));
assertTrue("HEAD".equals(request.method()));
} finally {
server.stop();
}
}

/**
* AmazonEsRespository can tell if an index exists or not.
* @throws Exception If something goes wrong.
*/
@Test
public void tellsIfIndexDoesNotExist() throws Exception {
System.setProperty("aws.accessKeyId", "access_key");
System.setProperty("aws.secretKey", "secret_key");
System.setProperty("aws.es.region", "ro");

int port = this.port();
System.setProperty("aws.es.endpoint", "http://localhost:" + port + "/es");

MkContainer server = new MkGrizzlyContainer()
.next(new MkAnswer.Simple(HttpStatus.SC_NOT_FOUND))
.start(port);
try {
boolean exists = new AmazonEsRepository("missing.index").exists();
assertFalse(exists);
MkQuery request = server.take();
assertTrue("/es/missing.index/".equals(request.uri().toString()));
assertTrue("HEAD".equals(request.method()));
} finally {
server.stop();
}
}

/**
* Clear system properties after each test.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ public void responseStatusInRange() throws Exception {
public void throwsAwsExceptionOnBadStatus() {
HttpResponse response = Mockito.mock(HttpResponse.class);
Mockito.when(response.getStatusCode()).thenReturn(HttpURLConnection.HTTP_BAD_METHOD);
String content = "bad request message";
Mockito.when(response.getContent())
.thenReturn(
new ByteArrayInputStream(content.getBytes())
);
try {
new SimpleAwsResponseHandler(true).handle(response);
fail("AmazonServiceException should have been thrown!");
} catch (AmazonServiceException awsEx) {
assertTrue(awsEx.getErrorMessage().equals("Unexpected status: 405"));
assertTrue(awsEx.getErrorMessage().equals(content));
assertTrue(awsEx.getStatusCode() == HttpURLConnection.HTTP_BAD_METHOD);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public class StepsTestCase {
*/
@Test
public void stepsPerformOk() {
Steps steps = new Steps(Mockito.mock(Step.class), Mockito.mock(SendReply.class));
Steps steps = new Steps(
Mockito.mock(Step.class),
Mockito.mock(Logger.class),
Mockito.mock(SendReply.class)
);
steps.perform();
}

Expand All @@ -78,7 +82,7 @@ public void stepsFail() throws Exception {
Mockito.doThrow(new IllegalStateException("for test"))
.when(s).perform();

Steps steps = new Steps(s, sr);
Steps steps = new Steps(s, Mockito.mock(Logger.class), sr);
steps.perform();

List<Comment> comments = Lists.newArrayList(com.issue().comments().iterate());
Expand Down