JsTopicInterceptor implements Serializable { Object proceedAndSendMessage(InvocationContext ctx, String topic, boolean jsonPayload) throws Exception { MessageToClient messageToClient = new MessageToClient(); messageToClient.setId(topic); Object result = ctx.proceed(); if(jsonPayload) { if(!String.class.isInstance(result)) { throw new UnsupportedOperationException("Method annotated JsTopic(jsonPayload=true) must return String type and correct Json."); } messageToClient.setJson((String) result); } else { messageToClient.setResponse(result); } wsEvent.fire(messageToClient); return result; }  @AroundInvoke Object processJsTopic(InvocationContext ctx);  }
@Test public void testProceedAndSendMessage() throws Exception { System.out.println("proceedAndSendMessage"); InvocationContext ctx = mock(InvocationContext.class); when(ctx.proceed()).thenReturn("RESULT"); String topic = ""; Object expResult = null; Object result = instance.proceedAndSendMessage(ctx, "TOPIC", false); ArgumentCaptor<MessageToClient> argument = ArgumentCaptor.forClass(MessageToClient.class); verify(wsEvent).fire(argument.capture()); assertThat(result).isEqualTo("RESULT"); MessageToClient mtc = argument.getValue(); assertThat(mtc.getId()).isEqualTo("TOPIC"); assertThat(mtc.getResponse()).isEqualTo("RESULT"); }
@Test public void testProceedAndSendMessageJson() throws Exception { System.out.println("proceedAndSendMessage"); InvocationContext ctx = mock(InvocationContext.class); when(ctx.proceed()).thenReturn("{\"json\":true}"); String topic = ""; Object expResult = null; Object result = instance.proceedAndSendMessage(ctx, "TOPIC", true); ArgumentCaptor<MessageToClient> argument = ArgumentCaptor.forClass(MessageToClient.class); verify(wsEvent).fire(argument.capture()); assertThat(result).isEqualTo("{\"json\":true}"); MessageToClient mtc = argument.getValue(); assertThat(mtc.getId()).isEqualTo("TOPIC"); assertThat(mtc.getJson()).isEqualTo("{\"json\":true}"); }
@Test(expected = UnsupportedOperationException.class) public void testProceedAndSendMessageFailed() throws Exception { System.out.println("proceedAndSendMessage"); InvocationContext ctx = mock(InvocationContext.class); when(ctx.proceed()).thenReturn(5); String topic = ""; Object expResult = null; Object result = instance.proceedAndSendMessage(ctx, "TOPIC", true); }