Skip to content

Commit

Permalink
Merge pull request #33 from bcgov/JRCC-Exceptions
Browse files Browse the repository at this point in the history
Adding a base Exception to extend into specific exceptions.
  • Loading branch information
alexjoybc authored Jul 15, 2019
2 parents 36dd9b2 + c42d17e commit 0ed1dc1
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void run(String... args) throws Exception {
Scanner scanner = new Scanner(System.in);

while(scanner.hasNext()) {
documentReadyHandler.Handle(scanner.nextLine(), appName);
documentReadyHandler.handle(scanner.nextLine(), appName);
}

scanner.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import ca.bc.gov.open.api.model.DocumentReceivedResponse;
import ca.bc.gov.open.api.model.Error;
import ca.gov.bc.open.jrccaccess.autoconfigure.services.DocumentReadyHandler;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.DocumentMessageException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* The document controller provides an endpoint to submit a document.
Expand Down Expand Up @@ -59,20 +60,23 @@ public ResponseEntity<DocumentReceivedResponse> postDocument(@NotNull @Valid Str
response.setAcknowledge(true);

try {
documentReadyHandler.Handle(getContent(body.getInputStream()), sender);
} catch (IOException e) {
// TODO Auto-generated catch block

Error error = new Error();
error.setCode(Integer.toString(HttpStatus.INTERNAL_SERVER_ERROR.value()));
error.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);
documentReadyHandler.handle(getContent(body.getInputStream()), sender);

} catch (ServiceUnavailableException e) {

Error error = new Error();
error.setCode(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()));
error.setMessage(e.getMessage());
return new ResponseEntity(error, HttpStatus.SERVICE_UNAVAILABLE);

} catch (IOException | DocumentMessageException e) {
// TODO Auto-generated catch block

Error error = new Error();
error.setCode(Integer.toString(HttpStatus.INTERNAL_SERVER_ERROR.value()));
error.setMessage(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR);

}

return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import ca.gov.bc.open.jrccaccess.autoconfigure.services.DocumentReadyHandler;
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyMessage;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.DocumentMessageException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;

/**
* The RabbitMqDocumentInput handles document from the rabbitMq message listener
Expand Down Expand Up @@ -71,23 +72,7 @@ public void receiveMessage(DocumentReadyMessage documentReadyMessage,

try {

DocumentStorageProperties storageProperties = documentReadyMessage.getDocumentStorageProperties();

String key = storageProperties.getKey();
String digest = storageProperties.getMD5();

if(logger.isDebugEnabled()) {
logger.debug("Request Document: key=" + key + ", digest=" + digest);
}

String content = this.redisStorageService.getString(key, digest);

if(logger.isDebugEnabled()) {
logger.debug(content);
}


this.documentReadyHandler.Handle(content,
this.documentReadyHandler.handle("not implemented yet",
documentReadyMessage.getTransactionInfo().getSender());
logger.info("message successfully acknowledged");

Expand All @@ -96,6 +81,10 @@ public void receiveMessage(DocumentReadyMessage documentReadyMessage,
logger.warn("Service unavailable exception, message will be put into the dead letter queue.");
throw new AmqpRejectAndDontRequeueException(e.getCause());

} catch (DocumentMessageException e) {

logger.warn("Service unavailable exception, message will be put into the dead letter queue.");
throw new AmqpRejectAndDontRequeueException(e.getCause());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyMessage;
import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.TransactionInfo;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* The rabbitMqDocumentOutput provides service to send document ready message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ca.gov.bc.open.jrccaccess.autoconfigure.AccessProperties;
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyMessage;
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyService;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* The RabbitMqDocumentReadyService provides services to interact with rabbitMq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.StorageService;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* The redisStorageService provides services to interact with Redis cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import ca.gov.bc.open.jrccaccess.libs.DocumentOutput;
import ca.gov.bc.open.jrccaccess.libs.TransactionInfo;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.DocumentMessageException;

/**
* The document ready handler is the global handler for incoming documents
Expand Down Expand Up @@ -37,7 +38,7 @@ public DocumentReadyHandler(DocumentOutput documentOutput) {
* @param inputStream
* @param sender
*/
public void Handle(String message, String sender) {
public void handle(String message, String sender) throws DocumentMessageException {

logger.debug("New document in {}", this.getClass().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyMessage;
import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.TransactionInfo;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

public class RabbitMqDocumentInputTester {

Expand Down Expand Up @@ -53,7 +53,7 @@ public class RabbitMqDocumentInputTester {
private RedisStorageService storageService;

@Before
public void init() {
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
Mockito.doNothing().when(this.documentReadyService).Publish(Mockito.any());
Mockito.doNothing().when(documentReadyHandlerMock).Handle(Mockito.anyString(), Mockito.anyString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ca.bc.gov.open.api.model.DocumentReceivedResponse;
import ca.gov.bc.open.jrccaccess.autoconfigure.plugins.http.DocumentController;
import ca.gov.bc.open.jrccaccess.autoconfigure.services.DocumentReadyHandler;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

public class DocumentControllerTester {

Expand All @@ -40,11 +40,11 @@ public class DocumentControllerTester {


@Before
public void init() throws IOException {
public void init() throws Exception {

MockitoAnnotations.initMocks(this);
Mockito.doNothing().when(this.documentReadyHandler).Handle(Mockito.anyString(), Mockito.eq(VALID));
Mockito.doThrow(new ServiceUnavailableException(SERVICE_UNAVAILABLE)).when(this.documentReadyHandler).Handle(Mockito.anyString(), Mockito.eq(SERVICE_UNAVAILABLE));
Mockito.doNothing().when(this.documentReadyHandler).handle(Mockito.anyString(), Mockito.eq(VALID));
Mockito.doThrow(new ServiceUnavailableException(SERVICE_UNAVAILABLE)).when(this.documentReadyHandler).handle(Mockito.anyString(), Mockito.eq(SERVICE_UNAVAILABLE));
Mockito.when(this.resourceWithException.getInputStream()).thenThrow(IOException.class);
sut = new DocumentController(this.documentReadyHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DocumentReadyHandlerTester {
private TransactionInfo transactionInfoMock;

@Before
public void init() {
public void init() throws Exception {

MockitoAnnotations.initMocks(this);
Mockito.doNothing().when(this.documentOutput).send(Mockito.anyString(), Mockito.any());
Expand All @@ -34,10 +34,10 @@ public void init() {


@Test
public void send_with_valid_input_should_process() {
public void send_with_valid_input_should_process() throws Exception {


sut.Handle("awesome content", "bcgov");
sut.handle("awesome content", "bcgov");


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class RabbitMqDocumentOutputTester {
private RedisStorageService storageService;

@Before
public void init() {
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
Mockito.doNothing().when(this.documentReadyService).Publish(Mockito.any());
Mockito.when(this.storageService.putString(Mockito.anyString())).thenReturn(new DocumentStorageProperties("key", "A1"));
Expand All @@ -40,7 +40,7 @@ public void init() {
}

@Test
public void send_with_valid_input_should_store_and_publish_a_message() {
public void send_with_valid_input_should_store_and_publish_a_message() throws Exception {
String content = "my awesome content";
TransactionInfo transactionInfo = new TransactionInfo("testfile.txt", "me", LocalDateTime.now());
this.sut.send(content, transactionInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import ca.gov.bc.open.jrccaccess.libs.DocumentReadyMessage;
import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.TransactionInfo;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

@RunWith(SpringRunner.class)
@SpringBootTest(
Expand Down Expand Up @@ -60,21 +60,21 @@ public void init() {
}

@Test
public void publish_with_valid_input_shoud_publish() {
public void publish_with_valid_input_shoud_publish() throws Exception {

sut.Publish(MESSAGE_1);

}

@Test(expected = ServiceUnavailableException.class)
public void publish_with_AmqpConnectException_shoud_throw_ServiceUnavailableException() {
public void publish_with_AmqpConnectException_shoud_throw_ServiceUnavailableException() throws Exception {

sut.Publish(MESSAGE_2);

}

@Test(expected = ServiceUnavailableException.class)
public void publish_with_AmqpIOException_shoud_throw_ServiceUnavailableException() {
public void publish_with_AmqpIOException_shoud_throw_ServiceUnavailableException() throws Exception {

sut.Publish(MESSAGE_3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import ca.gov.bc.open.jrccaccess.autoconfigure.plugins.rabbitmq.RedisStorageService;
import ca.gov.bc.open.jrccaccess.libs.DocumentStorageProperties;
import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

public class RedisStorageServiceTester {

Expand Down Expand Up @@ -47,7 +47,7 @@ public void Init() {


@Test
public void with_valid_content_should_return_document_properties() {
public void with_valid_content_should_return_document_properties() throws Exception {

String content = VALID;
String myHash = "9F7D0EE82B6A6CA7DDEAE841F3253059";
Expand All @@ -69,7 +69,7 @@ public void with_valid_content_should_return_document_properties() {


@Test(expected = ServiceUnavailableException.class)
public void with_RedisConnectionFailureException_should_throw_ServiceUnavailableException() {
public void with_RedisConnectionFailureException_should_throw_ServiceUnavailableException() throws Exception {

String content = REDIS_CONNECTION_FAILURE_EXCEPTION;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ca.gov.bc.open.jrccaccess.libs;

import ca.gov.bc.open.jrccaccess.libs.services.exceptions.DocumentMessageException;

/**
* Specification for the document output services
* @author alexjoybc
Expand All @@ -13,7 +15,7 @@ public interface DocumentOutput {
* @param documentInfo The document informations
* @param transactionInfo The transaction Informations
*/
void send(String content, TransactionInfo transactionInfo);
void send(String content, TransactionInfo transactionInfo) throws DocumentMessageException;


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.gov.bc.open.jrccaccess.libs;

import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* Represents a service to manipulate document ready message
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.gov.bc.open.jrccaccess.libs;

import ca.gov.bc.open.jrccaccess.libs.services.ServiceUnavailableException;
import ca.gov.bc.open.jrccaccess.libs.services.exceptions.ServiceUnavailableException;

/**
* The StorageService interface provides implementation details for the service.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ca.gov.bc.open.jrccaccess.libs.services.exceptions;

public class DocumentMessageException extends Exception {

/**
*
*/
private static final long serialVersionUID = -5684913345261766887L;

public DocumentMessageException() {
super();
}

public DocumentMessageException(String message) {
super(message);
}

public DocumentMessageException(String message, Throwable cause) {
super(message, cause);
}



}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ca.gov.bc.open.jrccaccess.libs.services;
package ca.gov.bc.open.jrccaccess.libs.services.exceptions;

/**
* The {@link RuntimeException} extension to indicate that a service (server)
* is temporary not available.
* @author alexjoybc
* @since 0.1.0
*/
public class ServiceUnavailableException extends RuntimeException {
public class ServiceUnavailableException extends DocumentMessageException {

private static final long serialVersionUID = 1L;

Expand Down

0 comments on commit 0ed1dc1

Please sign in to comment.