SpringBootExplainableResource { @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> predict(List<PredictInput> inputs) { try { return ResponseEntity.ok(explainabilityService.processRequest(application, inputs)); } catch (Exception e) { LOGGER.warn("An Exception occurred processing the predict request", e); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } } @Autowired  SpringBootExplainableResource(Application application); @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) ResponseEntity<Object> predict(List<PredictInput> inputs);  }
@Test @SuppressWarnings("unchecked") void explainServiceTest() { List<PredictInput> inputs = singletonList(createInput(40)); List<PredictOutput> outputs = (List<PredictOutput>) resource.predict(inputs).getBody(); assertNotNull(outputs); assertEquals(1, outputs.size()); PredictOutput output = outputs.get(0); assertNotNull(output.getResult()); assertNotNull(output.getModelIdentifier()); Map<String, Object> result = output.getResult(); assertTrue(result.containsKey("Should the driver be suspended?")); assertEquals("Yes", result.get("Should the driver be suspended?")); assertTrue(result.containsKey("Fine")); Map<String, Object> expectedFine = new HashMap<>(); expectedFine.put("Points", BigDecimal.valueOf(7)); expectedFine.put("Amount", BigDecimal.valueOf(1000)); assertEquals(expectedFine.get("Points"), ((Map<String, Object>)result.get("Fine")).get("Points")); assertEquals(expectedFine.get("Amount"), ((Map<String, Object>)result.get("Fine")).get("Amount")); }
@Test @SuppressWarnings("unchecked") void explainServiceTestMultipleInputs() { List<PredictInput> inputs = asList(createInput(40), createInput(120)); List<PredictOutput> outputs = (List<PredictOutput>) resource.predict(inputs).getBody(); assertNotNull(outputs); assertEquals(2, outputs.size()); PredictOutput output = outputs.get(1); assertNotNull(output); assertNotNull(output.getResult()); assertNotNull(output.getModelIdentifier()); Map<String, Object> result = output.getResult(); assertTrue(result.containsKey("Should the driver be suspended?")); assertEquals("No", result.get("Should the driver be suspended?")); assertNull(result.get("Fine")); }
@Test @SuppressWarnings("unchecked") void explainServiceTestNoInputs() { List<PredictOutput> outputs = (List<PredictOutput>) resource.predict(emptyList()).getBody(); assertNotNull(outputs); assertEquals(0, outputs.size()); }
@Test void explainServiceFail() { String unknownwResourceId = "unknown:model"; PredictInput input = createInput(10); input.getModelIdentifier().setResourceId(unknownwResourceId); ResponseEntity<Object> responseEntity = resource.predict(singletonList(input)); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals("Model " + unknownwResourceId + " not found.", responseEntity.getBody()); }