@Test public void testHandleRequest() { InjectHandler handler = new InjectHandler(); APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent(); handler = new InjectHandler(); input = new APIGatewayProxyRequestEvent(); input.setBody("invalid : json"); APIGatewayProxyResponseEvent output = handler.handleRequest(input, null); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); assertTrue(output.getBody().startsWith("Parameter must be a JSON object: ")); Container container = mock(Container.class); doReturn(null).when(container).fault(anyString(), anyString()); Prodigy.container(container); handler = new InjectHandler(); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"fault-1\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); assertThat(output.getBody(), is("Fault cannot be instantiated with name [fault-1] and params [{}]")); Scheduler scheduler = mock(Scheduler.class); doThrow(new IllegalStateException("hello")).when(scheduler).schedule(ArgumentMatchers.any(Fault.class)); container = mock(Container.class); doReturn(new Validator2()).when(container).fault(anyString(), anyString()); doReturn(scheduler).when(container).scheduler(); Prodigy.container(container); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"fault-2\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(500)); assertThat(output.getBody(), is("hello")); scheduler = mock(Scheduler.class); doReturn(true).when(scheduler).schedule(ArgumentMatchers.any(Fault.class)); container = mock(Container.class); Fault fault = new Validator2(); fault.id("fault-id-2"); Fault result = fault; doAnswer(i -> { assertThat(i.getArgument(1), is("{}")); return result; }).when(container).fault(anyString(), anyString()); doReturn(scheduler).when(container).scheduler(); Prodigy.container(container); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"fault-2\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(200)); assertThat(output.getBody(), is("{\"id\":\"fault-id-2\"}")); scheduler = mock(Scheduler.class); doReturn(true).when(scheduler).schedule(ArgumentMatchers.any(Fault.class)); container = mock(Container.class); fault = new Validator2(); fault.id("fault-id-2"); doReturn(fault).when(container).fault(anyString(), anyString()); doReturn(scheduler).when(container).scheduler(); Prodigy.container(container); input = new APIGatewayProxyRequestEvent(); input.setBody("{\"name\" : \"fault-2\", \"params\" : \"{\\\"key-1\\\" : \\\"value-1\\\"}\"}"); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(200)); assertThat(output.getBody(), is("{\"id\":\"fault-id-2\"}")); }
@Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); ObjectMapper mapper = new ObjectMapper(); try { Map<String, String> request = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); String name = request.get("name"); String params = request.get("params"); String id = Prodigy.inject(name, params); Map<String, Object> response = new LinkedHashMap<>(); response.put("id", id); return output.withStatusCode(200).withBody(mapper.writeValueAsString(response)); } catch (JsonParseException e) { String message = "Parameter must be a JSON object: " + e.getMessage(); log.warn(message, e); return output.withStatusCode(400).withBody(message); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }
InjectHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); ObjectMapper mapper = new ObjectMapper(); try { Map<String, String> request = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); String name = request.get("name"); String params = request.get("params"); String id = Prodigy.inject(name, params); Map<String, Object> response = new LinkedHashMap<>(); response.put("id", id); return output.withStatusCode(200).withBody(mapper.writeValueAsString(response)); } catch (JsonParseException e) { String message = "Parameter must be a JSON object: " + e.getMessage(); log.warn(message, e); return output.withStatusCode(400).withBody(message); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } } }
InjectHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); ObjectMapper mapper = new ObjectMapper(); try { Map<String, String> request = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); String name = request.get("name"); String params = request.get("params"); String id = Prodigy.inject(name, params); Map<String, Object> response = new LinkedHashMap<>(); response.put("id", id); return output.withStatusCode(200).withBody(mapper.writeValueAsString(response)); } catch (JsonParseException e) { String message = "Parameter must be a JSON object: " + e.getMessage(); log.warn(message, e); return output.withStatusCode(400).withBody(message); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  }
InjectHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); ObjectMapper mapper = new ObjectMapper(); try { Map<String, String> request = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); String name = request.get("name"); String params = request.get("params"); String id = Prodigy.inject(name, params); Map<String, Object> response = new LinkedHashMap<>(); response.put("id", id); return output.withStatusCode(200).withBody(mapper.writeValueAsString(response)); } catch (JsonParseException e) { String message = "Parameter must be a JSON object: " + e.getMessage(); log.warn(message, e); return output.withStatusCode(400).withBody(message); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context); }
InjectHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); ObjectMapper mapper = new ObjectMapper(); try { Map<String, String> request = mapper.readValue(input.getBody(), new TypeReference<Map<String, String>>() {}); String name = request.get("name"); String params = request.get("params"); String id = Prodigy.inject(name, params); Map<String, Object> response = new LinkedHashMap<>(); response.put("id", id); return output.withStatusCode(200).withBody(mapper.writeValueAsString(response)); } catch (JsonParseException e) { String message = "Parameter must be a JSON object: " + e.getMessage(); log.warn(message, e); return output.withStatusCode(400).withBody(message); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context);  }