-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-- Tests have been added for unit tests, but not the direct test on `DataServlet` that Michael recommended because of how brittle such a test would be.
- Loading branch information
Andrew Cholewa
committed
Jul 18, 2018
1 parent
4b354af
commit f6e0efd
Showing
9 changed files
with
490 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...e/src/test/groovy/com/yahoo/bard/webservice/exception/FiliDataExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright 2018 Oath Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.exception | ||
|
||
import com.fasterxml.jackson.databind.ObjectWriter | ||
import com.yahoo.bard.webservice.application.ObjectMappersSuite | ||
import com.yahoo.bard.webservice.data.dimension.TimeoutException | ||
import com.yahoo.bard.webservice.table.resolver.NoMatchFoundException | ||
import com.yahoo.bard.webservice.util.JsonSlurper | ||
import com.yahoo.bard.webservice.web.RequestValidationException | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import javax.ws.rs.container.AsyncResponse | ||
import javax.ws.rs.core.Response | ||
|
||
class FiliDataExceptionHandlerSpec extends Specification { | ||
|
||
@Shared | ||
ObjectMappersSuite MAPPERS = new ObjectMappersSuite() | ||
@Shared | ||
ObjectWriter writer = MAPPERS.getMapper().writer() | ||
@Shared | ||
JsonSlurper json = new JsonSlurper() | ||
|
||
AsyncResponse response = Mock(AsyncResponse) | ||
|
||
FiliDataExceptionHandler dataExceptionHandler = new FiliDataExceptionHandler() | ||
|
||
@Unroll | ||
def "The handler forwards the #status from a RequestValidationException as the status of the request"() { | ||
given: "The ResponseValidationException to validate" | ||
RequestValidationException exception = new RequestValidationException( | ||
status, | ||
"Error", | ||
"Error" | ||
) | ||
and: "A mock AsyncResponse to resume" | ||
|
||
when: "We handle the exception" | ||
dataExceptionHandler.handleThrowable(exception, response, Optional.empty(), null, null, writer) | ||
|
||
then: "The response is resumed" | ||
1 * response.resume(_) >> { | ||
assert it[0].status == status.statusCode | ||
assert json.parseText(it[0].entity).description == "Error" | ||
return true | ||
} | ||
where: | ||
status << [Response.Status.BAD_REQUEST, Response.Status.NOT_IMPLEMENTED] | ||
} | ||
|
||
def "NoMatchFoundException returns an Internal Server Error"() { | ||
given: | ||
NoMatchFoundException exception = new NoMatchFoundException("NoMatch") | ||
|
||
when: | ||
dataExceptionHandler.handleThrowable(exception, response, Optional.empty(), null, null, writer) | ||
|
||
then: | ||
1 * response.resume(_) >> { | ||
assert it[0].status == Response.Status.INTERNAL_SERVER_ERROR.statusCode | ||
assert json.parseText(it[0].entity).description == "NoMatch" | ||
return true | ||
} | ||
} | ||
|
||
def "TimeoutException returns a Gateway Timeout"() { | ||
given: | ||
TimeoutException exception = new TimeoutException("Timeout") | ||
|
||
when: | ||
dataExceptionHandler.handleThrowable(exception, response, Optional.empty(), null, null, writer) | ||
|
||
then: | ||
1 * response.resume(_) >> { | ||
assert it[0].status == Response.Status.GATEWAY_TIMEOUT.statusCode | ||
assert json.parseText(it[0].entity).description == "Timeout" | ||
return true | ||
} | ||
} | ||
|
||
def "A Throwable returns a Bad Request"() { | ||
given: | ||
Throwable throwable = new Throwable("Throw") | ||
|
||
when: | ||
dataExceptionHandler.handleThrowable(throwable, response, Optional.empty(), null, null, writer) | ||
|
||
then: | ||
1 * response.resume(_) >> { | ||
assert it[0].status == Response.Status.BAD_REQUEST.statusCode | ||
assert json.parseText(it[0].entity).description == "Throw" | ||
return true | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
.../test/groovy/com/yahoo/bard/webservice/exception/FiliDimensionExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2018 Oath Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.exception | ||
|
||
import com.yahoo.bard.webservice.data.dimension.Dimension | ||
import com.yahoo.bard.webservice.data.dimension.DimensionDictionary | ||
import com.yahoo.bard.webservice.web.ErrorMessageFormat | ||
import com.yahoo.bard.webservice.web.RequestValidationException | ||
import com.yahoo.bard.webservice.web.ResponseCode | ||
import com.yahoo.bard.webservice.web.RowLimitReachedException | ||
import com.yahoo.bard.webservice.web.apirequest.DimensionsApiRequest | ||
import com.yahoo.bard.webservice.web.apirequest.DimensionsApiRequestImpl | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException | ||
|
||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import javax.ws.rs.core.Response | ||
|
||
class FiliDimensionExceptionHandlerSpec extends Specification { | ||
|
||
FiliDimensionExceptionHandler dimensionExceptionHandler = new FiliDimensionExceptionHandler() | ||
|
||
Dimension dimension = Stub(Dimension) { | ||
getApiName() >> "dimension" | ||
toString() >> "dimension" | ||
} | ||
|
||
Optional<DimensionsApiRequest> request = Optional.of(new DimensionsApiRequestImpl( | ||
"dimension", | ||
"", | ||
"json", | ||
"", | ||
"", | ||
new DimensionDictionary([dimension] as Set), | ||
null | ||
)) | ||
|
||
@Unroll | ||
def "The handler forwards the #status from a RequestValidationException as the status of the request"() { | ||
given: "The ResponseValidationException to validate" | ||
RequestValidationException exception = new RequestValidationException(status, "Error", "Error") | ||
|
||
when: | ||
Response response = dimensionExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == status.statusCode | ||
response.entity == "Error" | ||
|
||
where: | ||
status << [Response.Status.BAD_REQUEST, Response.Status.NOT_IMPLEMENTED] | ||
} | ||
|
||
def "RowLimitReached returns an Insufficient Storage"() { | ||
given: | ||
RowLimitReachedException exception = new RowLimitReachedException("RowLimit") | ||
|
||
when: | ||
Response response = dimensionExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == ResponseCode.INSUFFICIENT_STORAGE.statusCode | ||
response.entity == "Row limit exceeded for dimension dimension: RowLimit" | ||
} | ||
|
||
def "JsonProcessing returns an Internal Server Error"() { | ||
given: | ||
JsonProcessingException exception = new JsonProcessingException("JsonError") | ||
|
||
when: | ||
Response response = dimensionExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == Response.Status.INTERNAL_SERVER_ERROR.statusCode | ||
response.entity == ErrorMessageFormat.INTERNAL_SERVER_ERROR_ON_JSON_PROCESSING.format("JsonError") | ||
} | ||
|
||
def "A Throwable returns a Bad Request"() { | ||
given: | ||
Throwable throwable = new Throwable("Throw") | ||
|
||
when: | ||
Response response = dimensionExceptionHandler.handleThrowable(throwable, request, null, null) | ||
|
||
then: | ||
response.status == Response.Status.BAD_REQUEST.statusCode | ||
response.entity == ErrorMessageFormat.REQUEST_PROCESSING_EXCEPTION.format("Throw") | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...e/src/test/groovy/com/yahoo/bard/webservice/exception/FiliJobsExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2018 Oath Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.exception | ||
|
||
import com.yahoo.bard.webservice.application.ObjectMappersSuite | ||
import com.yahoo.bard.webservice.util.JsonSlurper | ||
import com.yahoo.bard.webservice.web.RequestValidationException | ||
import com.yahoo.bard.webservice.web.apirequest.JobsApiRequest | ||
import com.yahoo.bard.webservice.web.apirequest.JobsApiRequestImpl | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import javax.ws.rs.core.Response | ||
|
||
class FiliJobsExceptionHandlerSpec extends Specification { | ||
|
||
@Shared | ||
ObjectMappersSuite objectMappersSuite = new ObjectMappersSuite() | ||
|
||
@Shared | ||
JsonSlurper json = new JsonSlurper() | ||
|
||
FiliJobsExceptionHandler jobsExceptionHandler = new FiliJobsExceptionHandler(objectMappersSuite) | ||
|
||
Optional<JobsApiRequest> request = Optional.of( | ||
new JobsApiRequestImpl( | ||
"json", | ||
"500", | ||
"", | ||
"", | ||
"", | ||
null, | ||
null, | ||
null | ||
) | ||
) | ||
|
||
@Unroll | ||
def "The handler forwards the #status from a RequestValidationException as the status of the request"() { | ||
given: "The ResponseValidationException to validate" | ||
RequestValidationException exception = new RequestValidationException(status, "Error", "Error") | ||
|
||
when: | ||
Response response = jobsExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == status.statusCode | ||
json.parseText(response.entity).description == "Error" | ||
|
||
where: | ||
status << [Response.Status.BAD_REQUEST, Response.Status.NOT_IMPLEMENTED] | ||
} | ||
|
||
def "A Throwable returns an Internal Server Error"() { | ||
given: | ||
Throwable throwable = new Throwable("Throw") | ||
|
||
when: | ||
Response response = jobsExceptionHandler.handleThrowable(throwable, request, null, null) | ||
|
||
then: | ||
response.status == Response.Status.INTERNAL_SERVER_ERROR.statusCode | ||
response.entity == "Throw" | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...src/test/groovy/com/yahoo/bard/webservice/exception/FiliMetricExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2018 Oath Inc. | ||
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms. | ||
package com.yahoo.bard.webservice.exception | ||
|
||
import com.yahoo.bard.webservice.data.metric.LogicalMetric | ||
import com.yahoo.bard.webservice.data.metric.LogicalMetricInfo | ||
import com.yahoo.bard.webservice.data.metric.MetricDictionary | ||
import com.yahoo.bard.webservice.data.metric.TemplateDruidQuery | ||
import com.yahoo.bard.webservice.data.metric.mappers.NoOpResultSetMapper | ||
import com.yahoo.bard.webservice.web.ErrorMessageFormat | ||
import com.yahoo.bard.webservice.web.RequestValidationException | ||
import com.yahoo.bard.webservice.web.apirequest.MetricsApiRequest | ||
import com.yahoo.bard.webservice.web.apirequest.MetricsApiRequestImpl | ||
import spock.lang.Specification | ||
import spock.lang.Unroll | ||
|
||
import javax.ws.rs.core.Response | ||
|
||
class FiliMetricExceptionHandlerSpec extends Specification { | ||
|
||
FiliMetricExceptionHandler metricsExceptionHandler = new FiliMetricExceptionHandler() | ||
|
||
LogicalMetric metric = new LogicalMetric( | ||
Stub(TemplateDruidQuery), | ||
new NoOpResultSetMapper(), | ||
new LogicalMetricInfo("metric") | ||
) | ||
|
||
MetricDictionary dictionary = new MetricDictionary() | ||
|
||
Optional<MetricsApiRequest> request | ||
|
||
def setup() { | ||
dictionary.put("metric", metric) | ||
request = Optional.of(new MetricsApiRequestImpl( | ||
"metric", | ||
"json", | ||
"", | ||
"", | ||
dictionary, | ||
null | ||
)) | ||
} | ||
|
||
|
||
@Unroll | ||
def "The handler forwards the #status from a RequestValidationException as the status of the request"() { | ||
given: "The ResponseValidationException to validate" | ||
RequestValidationException exception = new RequestValidationException(status, "Error", "Error") | ||
|
||
when: | ||
Response response = metricsExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == status.statusCode | ||
response.entity == "Error" | ||
|
||
where: | ||
status << [Response.Status.BAD_REQUEST, Response.Status.NOT_IMPLEMENTED] | ||
} | ||
|
||
def "IOException returns an Internal Server Errror"() { | ||
given: | ||
IOException exception = new IOException("IOException") | ||
|
||
when: | ||
Response response = metricsExceptionHandler.handleThrowable(exception, request, null, null) | ||
|
||
then: | ||
response.status == Response.Status.INTERNAL_SERVER_ERROR.statusCode | ||
response.entity == "Internal server error. IOException : $exception.message" | ||
} | ||
|
||
def "A Throwable returns a Bad Request"() { | ||
given: | ||
Throwable throwable = new Throwable("Throw") | ||
|
||
when: | ||
Response response = metricsExceptionHandler.handleThrowable(throwable, request, null, null) | ||
|
||
then: | ||
response.status == Response.Status.BAD_REQUEST.statusCode | ||
response.entity == ErrorMessageFormat.REQUEST_PROCESSING_EXCEPTION.format("Throw") | ||
} | ||
} |
Oops, something went wrong.