Skip to content

Commit

Permalink
[refs #6] - WEb socket integration init
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjoy-sust committed Sep 14, 2018
1 parent d6cb2a6 commit 57a4b70
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 14 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/fm/assignment/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fm.assignment.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
Expand All @@ -15,13 +16,20 @@ public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/notify").setAllowedOrigins("*");
registry.addEndpoint("/notify").setAllowedOrigins("*").withSockJS();
registry.addEndpoint("/freight").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/freight");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/fm/assignment/mail/EmailScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class EmailScheduler {
@Autowired
private MailBoxService mailBoxService;

@Scheduled(fixedDelay = 9000)
@Scheduled(fixedDelay = 90000)
public void sendEmailSchedule(){
log.info("Email schedule started at {}",new Date().getTime());
List<MailBoxParam> mailBoxParams = mailBoxService.getMailBoxByStatus(MailStatusEnum.PENDING);
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/com/fm/assignment/websocket/NotificationHandler.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
package com.fm.assignment.websocket;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


/**
* Created by Lenovo on 14/02/2018.
*/
@Controller
@Slf4j
public class NotificationHandler {

@MessageMapping("/notify")
@SendTo("/topic/messages")
public Notification send(String message) throws Exception {
String time = new SimpleDateFormat("HH:mm").format(new Date());
private NotificationHandler notificationHandler;

@Autowired
public void setGameService(NotificationHandler notificationHandler) {
this.notificationHandler = notificationHandler;
}

@MessageMapping("/create/{message}")
@SendTo("/topic/board/{message}")
public Notification createNotification(@DestinationVariable String message) {
Notification notification = new Notification();
notification.setSender("Sanju");
notification.setMessage(message);
return notification;
}

@MessageMapping("/notify/{message}")
@SendTo("/topic/notify/{message}")
public Notification takeNotification(@DestinationVariable String message) throws IllegalArgumentException {
Notification notification = new Notification();
notification.setSender("Flopcoder");
notification.setMessage("Welcome to my freight management project".concat(message));
notification.setSender("Sanju Moving");
notification.setMessage(message);
return notification;
}
}
110 changes: 110 additions & 0 deletions src/test/java/com/fm/assignment/api/WebSocketEndpointTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.fm.assignment.api;

/**
* Created by Lenovo on 11/09/2018.
*/

import com.fm.assignment.websocket.Notification;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;

import java.lang.reflect.Type;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebSocketEndpointTest {
@Value("${local.server.port}")
private int port;
private String URL;

private static final String SEND_CREATE_BOARD_ENDPOINT = "/app/create/";
private static final String SEND_MOVE_ENDPOINT = "/app/notify/";
private static final String SUBSCRIBE_CREATE_BOARD_ENDPOINT = "/topic/board/";
private static final String SUBSCRIBE_MOVE_ENDPOINT = "/topic/notify/";


private CompletableFuture<Notification> completableFuture;

@Before
public void setup() {
completableFuture = new CompletableFuture<>();
URL = "ws://localhost:" + port + "/freight";
}

@Test
public void testCreateGameEndpoint() throws URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
String uuid = UUID.randomUUID().toString();

WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

StompSession stompSession = stompClient.connect(URL, new StompSessionHandlerAdapter() {
}).get(1, TimeUnit.SECONDS);

stompSession.subscribe(SUBSCRIBE_CREATE_BOARD_ENDPOINT + uuid, new CustomStompFrameHandler());
stompSession.send(SEND_CREATE_BOARD_ENDPOINT + uuid, null);

Notification notification = completableFuture.get(10, TimeUnit.SECONDS);

Assert.assertNotNull(notification);
}

@Test
public void testMakeMoveEndpoint() throws InterruptedException, ExecutionException, TimeoutException {
String uuid = UUID.randomUUID().toString();

WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

StompSession stompSession = stompClient.connect(URL, new StompSessionHandlerAdapter() {
}).get(1, TimeUnit.SECONDS);

stompSession.subscribe(SUBSCRIBE_MOVE_ENDPOINT + uuid, new CustomStompFrameHandler());
stompSession.send(SEND_MOVE_ENDPOINT + uuid,TimeUnit.SECONDS);
Notification gameStateAfterMove = completableFuture.get(5, TimeUnit.SECONDS);

Assert.assertNotNull(gameStateAfterMove);
}

private List<Transport> createTransportClient() {
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
return transports;
}


private class CustomStompFrameHandler implements StompFrameHandler {
@Override
public Type getPayloadType(StompHeaders stompHeaders) {
return Notification.class;
}

@Override
public void handleFrame(StompHeaders stompHeaders, Object o) {
completableFuture.complete((Notification) o);
}
}
}
2 changes: 1 addition & 1 deletion src/test/resource/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ spring.datasource.username=root
spring.datasource.password=admin
endpoints.actuator.enabled=true
endpoints.info.enabled=true

local.server.port=8080
spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect
spring.jpa.database-platform = org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect

0 comments on commit 57a4b70

Please sign in to comment.