@Test public void test5() throws Exception { byte[] testContent = "Bla".getBytes(); SqlLobValue lob = new SqlLobValue(new ByteArrayInputStream(testContent), 3, handler); lob.setTypeValue(preparedStatement, 1, Types.CLOB, "test"); verify(creator).setClobAsAsciiStream(eq(preparedStatement), eq(1), inputStreamCaptor.capture(), eq(3)); byte[] bytes = new byte[3]; inputStreamCaptor.getValue().read(bytes); assertThat(bytes, equalTo(testContent)); }
@Test public void convertAndSendPayloadAndHeadersName() throws JMSException { Map<String, Object> headers = new HashMap<>(); headers.put("foo", "bar"); this.messagingTemplate.convertAndSend("myQueue", "Hello", headers); verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); // see createTextMessage }
@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 convertAndSendPayload() throws JMSException { Destination destination = new Destination() {}; this.messagingTemplate.convertAndSend(destination, "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 convertAndSendPayloadName() throws JMSException { this.messagingTemplate.convertAndSend("myQueue", "my Payload"); verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture()); TextMessage textMessage = createTextMessage(this.messageCreator.getValue()); assertEquals("my Payload", textMessage.getText()); }
@Test public void send() { Destination destination = new Destination() {}; Message<String> message = createTextMessage(); this.messagingTemplate.send(destination, message); verify(this.jmsTemplate).send(eq(destination), this.messageCreator.capture()); assertTextMessage(this.messageCreator.getValue()); }
private void invokeMessageCreator() { willAnswer(invocation -> { MessageCreator messageCreator = (MessageCreator) invocation.getArguments()[1]; messageCreator.createMessage(null); return null; }).given(this.jmsTemplate).send(eq("myQueue"), any()); }
private void mockResponseBody(String expectedBody, MediaType mediaType) throws Exception { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(mediaType); responseHeaders.setContentLength(expectedBody.length()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes())); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expectedBody); }
@Test public void render() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("modelAttribute", "modelValue"); view.render(model, request, response); assertEquals("modelValue", request.getAttribute("modelAttribute")); verify(renderer).render(eq(VIEW_PATH), isA(Request.class)); }
@Test public void noSubProtocolOneHandler() throws Exception { this.webSocketHandler.setProtocolHandlers(Arrays.asList(stompHandler)); this.webSocketHandler.afterConnectionEstablished(session); verify(this.stompHandler).afterSessionStarted( isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel)); }
@Test public void handleReturnValueProduces() throws Exception { String body = "Foo"; servletRequest.addHeader("Accept", "text/*"); servletRequest.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML)); given(stringMessageConverter.canWrite(String.class, MediaType.TEXT_HTML)).willReturn(true); processor.handleReturnValue(body, returnTypeStringProduces, mavContainer, webRequest); assertTrue(mavContainer.isRequestHandled()); verify(stringMessageConverter).write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class)); }
@Test public void handleReturnTypeResourceByteRange() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(MediaType.APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(times(1)).write( anyCollection(), eq(MediaType.APPLICATION_OCTET_STREAM), argThat(outputMessage -> "bytes".equals(outputMessage.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)))); assertEquals(206, servletResponse.getStatus()); }
@Test public void handleReturnTypeResourceIllegalByteRange() throws Exception { Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); servletRequest.addHeader("Range", "illegal"); given(resourceRegionMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceRegionMessageConverter.canWrite(any(), eq(MediaType.APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResource, mavContainer, webRequest); then(resourceRegionMessageConverter).should(never()).write( anyCollection(), eq(MediaType.APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(416, servletResponse.getStatus()); }
@Test public void sendAndReceiveName() { Message<String> request = createTextMessage(); javax.jms.Message replyJmsMessage = createJmsTextMessage(); given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); Message<?> actual = this.messagingTemplate.sendAndReceive("myQueue", request); verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertTextMessage(actual); }
@Test public void resolveArgumentViaDefaultConstructor() throws Exception { WebDataBinder dataBinder = new WebRequestDataBinder(null); WebDataBinderFactory factory = mock(WebDataBinderFactory.class); given(factory.createBinder(any(), notNull(), eq("attrName"))).willReturn(dataBinder); this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory); verify(factory).createBinder(any(), notNull(), eq("attrName")); }
@Test public void convertSendAndReceiveDefaultDestinationName() throws JMSException { this.messagingTemplate.setDefaultDestinationName("myQueue"); javax.jms.Message replyJmsMessage = createJmsTextMessage("My reply"); given(this.jmsTemplate.sendAndReceive(eq("myQueue"), any())).willReturn(replyJmsMessage); String reply = this.messagingTemplate.convertSendAndReceive("my Payload", String.class); verify(this.jmsTemplate, times(1)).sendAndReceive(eq("myQueue"), any()); assertEquals("My reply", reply); }
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); @SuppressWarnings("unchecked") HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true); given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(beanConverter)); processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory()); }
@Test public void connectReceiveAndClose() throws Exception { String body = "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; ClientHttpResponse response = response(HttpStatus.OK, body); connect(response); verify(this.webSocketHandler).afterConnectionEstablished(any()); verify(this.webSocketHandler).handleMessage(any(), eq(new TextMessage("foo"))); verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!"))); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void convertDestinationResolutionExceptionOnSend() { Destination destination = new Destination() {}; willThrow(DestinationResolutionException.class).given(this.jmsTemplate).send(eq(destination), any()); this.thrown.expect(org.springframework.messaging.core.DestinationResolutionException.class); this.messagingTemplate.send(destination, createTextMessage()); }
@Test public void nullSubProtocol() throws Exception { this.webSocketHandler.setDefaultProtocolHandler(defaultHandler); this.webSocketHandler.afterConnectionEstablished(session); verify(this.defaultHandler).afterSessionStarted( isA(ConcurrentWebSocketSessionDecorator.class), eq(this.inClientChannel)); verify(this.stompHandler, times(0)).afterSessionStarted(session, this.inClientChannel); verify(this.mqttHandler, times(0)).afterSessionStarted(session, this.inClientChannel); }