-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refs #6] - WEb socket integration init
- Loading branch information
1 parent
d6cb2a6
commit 57a4b70
Showing
5 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 27 additions & 9 deletions
36
src/main/java/com/fm/assignment/websocket/NotificationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
110
src/test/java/com/fm/assignment/api/WebSocketEndpointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters