@Test public void createEventTest() throws Exception { final String jsonString = createdEventJson(); new Expectations() { { mockHttpServletRequest.getHeader(Headers.X_VOL_TENANT_DOMAIN); result=TENANT_URL; } { mockHttpServletRequest.getHeader(Headers.X_VOL_SITE_DOMAIN); result=SITE_URL; } { mockHttpServletRequest.getIntHeader(Headers.X_VOL_TENANT); result=TENANT_ID; } { mockHttpServletRequest.getIntHeader(Headers.X_VOL_SITE); result=SITE_ID; } { mockHttpServletRequest.getHeader(Headers.X_VOL_CORRELATION); result=CORRELATION_ID; } { mockHttpServletRequest.getHeader(Headers.X_VOL_HMAC_SHA256); result = HMAC_SHA_256; } { mockHttpServletRequest.getIntHeader(Headers.X_VOL_MASTER_CATALOG); result=MASTER_CATALOG_ID; } { mockHttpServletRequest.getIntHeader(Headers.X_VOL_CATALOG); result=CATALOG_ID; } { AppAuthenticator.isUseSSL(); result=false; times=2; } { mockHttpServletRequest.getHeader(Headers.DATE); result=HEADER_DATE; } { mockHttpServletRequest.getInputStream(); result=mockServletInputStream; } { IOUtils.toString(mockServletInputStream); result=jsonString; } { Crypto.isRequestValid((ApiContext)any, (String)any); result=true; } }; EventService eventService = new EventService(); EventHandlerStatus status = eventService.dispatchEvent(mockHttpServletRequest); assertEquals(HttpServletResponse.SC_OK, status.getStatus()); }
public EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest) { ApiContext apiContext = new MozuApiContext(httpRequest); Event event = null; try { String body = IOUtils.toString(httpRequest.getInputStream()); logger.debug("Event body: " + body); event = mapper.readValue(body, Event.class); logger.info("Dispatching Event. Correlation ID: " + event.getCorrelationId()); if (!Crypto.isRequestValid(apiContext, body)) { StringBuilder msg = new StringBuilder ("Event is not authorized."); logger.warn(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_UNAUTHORIZED)); } } catch (IOException exception) { StringBuilder msg = new StringBuilder ("Unable to read event: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } try { invokeHandler(event, apiContext); } catch (Exception exception) { StringBuilder msg = new StringBuilder ("Unable to process event with correlation id ").append(event.getCorrelationId()).append(". Message: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } return( new EventHandlerStatus(null, HttpServletResponse.SC_OK)); }
EventService { public EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest) { ApiContext apiContext = new MozuApiContext(httpRequest); Event event = null; try { String body = IOUtils.toString(httpRequest.getInputStream()); logger.debug("Event body: " + body); event = mapper.readValue(body, Event.class); logger.info("Dispatching Event. Correlation ID: " + event.getCorrelationId()); if (!Crypto.isRequestValid(apiContext, body)) { StringBuilder msg = new StringBuilder ("Event is not authorized."); logger.warn(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_UNAUTHORIZED)); } } catch (IOException exception) { StringBuilder msg = new StringBuilder ("Unable to read event: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } try { invokeHandler(event, apiContext); } catch (Exception exception) { StringBuilder msg = new StringBuilder ("Unable to process event with correlation id ").append(event.getCorrelationId()).append(". Message: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } return( new EventHandlerStatus(null, HttpServletResponse.SC_OK)); } }
EventService { public EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest) { ApiContext apiContext = new MozuApiContext(httpRequest); Event event = null; try { String body = IOUtils.toString(httpRequest.getInputStream()); logger.debug("Event body: " + body); event = mapper.readValue(body, Event.class); logger.info("Dispatching Event. Correlation ID: " + event.getCorrelationId()); if (!Crypto.isRequestValid(apiContext, body)) { StringBuilder msg = new StringBuilder ("Event is not authorized."); logger.warn(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_UNAUTHORIZED)); } } catch (IOException exception) { StringBuilder msg = new StringBuilder ("Unable to read event: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } try { invokeHandler(event, apiContext); } catch (Exception exception) { StringBuilder msg = new StringBuilder ("Unable to process event with correlation id ").append(event.getCorrelationId()).append(". Message: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } return( new EventHandlerStatus(null, HttpServletResponse.SC_OK)); }  }
EventService { public EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest) { ApiContext apiContext = new MozuApiContext(httpRequest); Event event = null; try { String body = IOUtils.toString(httpRequest.getInputStream()); logger.debug("Event body: " + body); event = mapper.readValue(body, Event.class); logger.info("Dispatching Event. Correlation ID: " + event.getCorrelationId()); if (!Crypto.isRequestValid(apiContext, body)) { StringBuilder msg = new StringBuilder ("Event is not authorized."); logger.warn(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_UNAUTHORIZED)); } } catch (IOException exception) { StringBuilder msg = new StringBuilder ("Unable to read event: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } try { invokeHandler(event, apiContext); } catch (Exception exception) { StringBuilder msg = new StringBuilder ("Unable to process event with correlation id ").append(event.getCorrelationId()).append(". Message: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } return( new EventHandlerStatus(null, HttpServletResponse.SC_OK)); }  EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest); }
EventService { public EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest) { ApiContext apiContext = new MozuApiContext(httpRequest); Event event = null; try { String body = IOUtils.toString(httpRequest.getInputStream()); logger.debug("Event body: " + body); event = mapper.readValue(body, Event.class); logger.info("Dispatching Event. Correlation ID: " + event.getCorrelationId()); if (!Crypto.isRequestValid(apiContext, body)) { StringBuilder msg = new StringBuilder ("Event is not authorized."); logger.warn(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_UNAUTHORIZED)); } } catch (IOException exception) { StringBuilder msg = new StringBuilder ("Unable to read event: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } try { invokeHandler(event, apiContext); } catch (Exception exception) { StringBuilder msg = new StringBuilder ("Unable to process event with correlation id ").append(event.getCorrelationId()).append(". Message: ").append(exception.getMessage()); logger.error(msg.toString()); return( new EventHandlerStatus(msg.toString(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } return( new EventHandlerStatus(null, HttpServletResponse.SC_OK)); }  EventHandlerStatus dispatchEvent(HttpServletRequest httpRequest);  }