@Test public void convertAndSendPayloadAndHeaders() throws JMSException { Destination destination = new Destination() {}; Map<String, Object> headers = new HashMap<>(); headers.put("foo", "bar"); this.messagingTemplate.convertAndSend(destination, "Hello", headers); verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); // see createTextMessage }
@Test public void mapNullTemplateVariable() throws Exception { mockSentRequest(GET, "http://example.com/-foo"); mockResponseStatus(HttpStatus.OK); Map<String, String> vars = new HashMap<>(2); vars.put("first", null); vars.put("last", "foo"); template.execute("http://example.com/{first}-{last}", GET, null, null, vars); verify(response).close(); }
@Test public void removeSessionScopedAttributeDoesNotForceCreationOfSession() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); ServletRequestAttributes attrs = new ServletRequestAttributes(request); attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION); verify(request).getSession(false); }
@Test public void replyJacksonMessageAndJsonView() throws JMSException { TextMessage reply = testReplyWithJackson("replyJacksonMessageAndJsonView", "{\"name\":\"Response\"}"); verify(reply).setObjectProperty("foo", "bar"); }
@Test public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedJoinedAndPropagationSupports() { given(manager.isOpen()).willReturn(true); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); tt.execute(status -> { bean.extendedEntityManagerUnsynchronized.joinTransaction(); bean.extendedEntityManagerUnsynchronized.flush(); return null; }); verify(manager).flush(); }
@Test public void convertAndSendDefaultDestination() throws JMSException { Destination destination = new Destination() {}; this.messagingTemplate.setDefaultDestination(destination); this.messagingTemplate.convertAndSend("my Payload"); verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture()); TextMessage textMessage = createTextMessage(this.messageCreator.getValue()); assertEquals("my Payload", textMessage.getText()); }
@Test public void sendName() { Message<String> message = createTextMessage(); this.messagingTemplate.send("myQueue", message); verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); }
@Test // SPR-14238 public void sendToUserWithSendToDefaultOverride() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); Class<?> clazz = SendToUserWithSendToOverrideTestBean.class; Method method = clazz.getDeclaredMethod("handleAndSendToDefaultDestination"); MethodParameter parameter = new SynthesizingMethodParameter(method, -1); String sessionId = "sess1"; Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); assertResponse(parameter, sessionId, 0, "/user/sess1/dest-default"); }
@Test public void mapTemplateVariables() throws Exception { mockSentRequest(GET, "http://example.com/hotels/42/bookings/42"); mockResponseStatus(HttpStatus.OK); Map<String, String> vars = Collections.singletonMap("hotel", "42"); template.execute("http://example.com/hotels/{hotel}/bookings/{hotel}", GET, null, null, vars); verify(response).close(); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); verify(this.chain, never()).resolveResource(this.exchange, null, this.locations); }
@Test public void exceptionAfterPreHandle() throws Exception { Exception ex = new Exception(""); given(this.interceptor1.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor2.preHandle(this.request, this.response, this.handler)).willReturn(true); given(this.interceptor3.preHandle(this.request, this.response, this.handler)).willReturn(true); this.chain.applyPreHandle(request, response); this.chain.triggerAfterCompletion(this.request, this.response, ex); verify(this.interceptor3).afterCompletion(this.request, this.response, this.handler, ex); verify(this.interceptor2).afterCompletion(this.request, this.response, this.handler, ex); verify(this.interceptor1).afterCompletion(this.request, this.response, this.handler, ex); }
@Test public void postForLocationNull() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); template.postForLocation("http://example.com", null); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void putNull() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(PUT, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); template.put("http://example.com", null); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void delete() throws Exception { mockSentRequest(DELETE, "http://example.com"); mockResponseStatus(HttpStatus.OK); template.delete("http://example.com"); verify(response).close(); }
@Test public void put() throws Exception { mockTextPlainHttpMessageConverter(); mockSentRequest(PUT, "http://example.com"); mockResponseStatus(HttpStatus.OK); template.put("http://example.com", "Hello World"); verify(response).close(); }
@Test public void shouldHandleReturnValue() throws Exception { String body = "Foo"; ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK); MediaType accepted = TEXT_PLAIN; servletRequest.addHeader("Accept", accepted.toString()); initStringMessageConversion(accepted); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); assertTrue(mavContainer.isRequestHandled()); verify(stringHttpMessageConverter).write(eq(body), eq(accepted), isA(HttpOutputMessage.class)); }
@Test public void receiveAndConvert() { Destination destination = new Destination() {}; javax.jms.Message jmsMessage = createJmsTextMessage("my Payload"); given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage); String payload = this.messagingTemplate.receiveAndConvert(destination, String.class); assertEquals("my Payload", payload); verify(this.jmsTemplate).receive(destination); }
@Test public void interceptorWithModifiedMessage() { Message<?> expected = mock(Message.class); BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor(); interceptor.setMessageToReturn(expected); this.channel.addInterceptor(interceptor); this.channel.subscribe(this.handler); this.channel.send(this.message); verify(this.handler).handleMessage(expected); assertEquals(1, interceptor.getCounter().get()); assertTrue(interceptor.wasAfterHandledInvoked()); }
@Test public void receiveDefaultDestination() { Destination destination = new Destination() {}; this.messagingTemplate.setDefaultDestination(destination); javax.jms.Message jmsMessage = createJmsTextMessage(); given(this.jmsTemplate.receive(destination)).willReturn(jmsMessage); Message<?> message = this.messagingTemplate.receive(); verify(this.jmsTemplate).receive(destination); assertTextMessage(message); }