-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding a unit test handling exception
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
engine/src/test/java/io/seldon/engine/api/rest/TestRestClientControllerErrorHandling.java
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,103 @@ | ||
package io.seldon.engine.api.rest; | ||
|
||
import io.seldon.engine.filters.XSSFilter; | ||
import io.seldon.engine.predictors.EnginePredictor; | ||
import io.seldon.engine.service.InternalPredictionService; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.HttpServerErrorException; | ||
import org.springframework.web.client.HttpStatusCodeException; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static io.seldon.engine.util.TestUtils.readFile; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ActiveProfiles("test") | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@AutoConfigureMockMvc | ||
public class TestRestClientControllerErrorHandling | ||
{ | ||
|
||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
@Autowired private EnginePredictor enginePredictor; | ||
|
||
// @Autowired | ||
private MockMvc mvc; | ||
|
||
@Autowired private RestClientController restController; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
|
||
mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(new XSSFilter()).build(); | ||
} | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@Autowired private TestRestTemplate testRestTemplate; | ||
|
||
@Autowired private InternalPredictionService internalPredictionService; | ||
|
||
@Test | ||
public void testModelPredictionNon200Response() throws Exception { | ||
String jsonStr = readFile("src/test/resources/model_simple.json", StandardCharsets.UTF_8); | ||
String responseStr = | ||
readFile("src/test/resources/response_status.json", StandardCharsets.UTF_8); | ||
io.seldon.protos.DeploymentProtos.PredictorSpec.Builder PredictorSpecBuilder = io.seldon.protos.DeploymentProtos.PredictorSpec.newBuilder(); | ||
EnginePredictor.updateMessageBuilderFromJson(PredictorSpecBuilder, jsonStr); | ||
io.seldon.protos.DeploymentProtos.PredictorSpec predictorSpec = PredictorSpecBuilder.build(); | ||
final String predictJson = "{" + "\"binData\": \"MTIz\"" + "}"; | ||
ReflectionTestUtils.setField(enginePredictor, "predictorSpec", predictorSpec); | ||
|
||
HttpStatusCodeException exception = HttpServerErrorException.InternalServerError | ||
.create(HttpStatus.BAD_REQUEST, "status text", HttpHeaders.EMPTY, responseStr.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8); | ||
|
||
Mockito.when( | ||
testRestTemplate | ||
.getRestTemplate() | ||
.postForEntity( | ||
Matchers.<URI>any(), | ||
Matchers.<HttpEntity<MultiValueMap<String, String>>>any(), | ||
Matchers.<Class<String>>any())) | ||
.thenThrow(exception); | ||
|
||
MvcResult res = | ||
mvc.perform( | ||
MockMvcRequestBuilders.post("/api/v0.1/predictions") | ||
.accept(MediaType.APPLICATION_JSON_UTF8) | ||
.content(predictJson) | ||
.contentType(MediaType.APPLICATION_JSON_UTF8)) | ||
.andReturn(); | ||
|
||
// Check for returned response that wraps the ApiException into SeldonMessage | ||
Assert.assertEquals(200, res.getResponse().getStatus()); | ||
Assert.assertEquals(responseStr, res.getResponse().getContentAsString()); | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"status": { | ||
"code": 400, | ||
"info": "test error message", | ||
"reason": "exception in prediction", | ||
"status": "FAILURE" | ||
} | ||
} |