From 771696c2e0052da1ea72476ba05566e1fec35f6c Mon Sep 17 00:00:00 2001 From: Semyon Date: Sat, 24 Aug 2019 00:04:34 +0300 Subject: [PATCH] 1 --- .../java/ru/atom/chat/client/ChatClient.java | 9 +++- .../ru/atom/chat/server/ChatController.java | 43 ++++++++++++++++--- .../ru/atom/chat/client/ChatClientTest.java | 2 +- .../mm/controller/ConnectionController.java | 16 ++++--- .../ConnectionControllerIntegrationTest.java | 17 ++++++-- .../ru/atom/mm/ConnectionControllerTest.java | 2 +- .../java/ru/atom/mm/GameControllerTest.java | 35 +++++++++++++-- .../mm/GamesControllerIntegrationTest.java | 38 +++++++++++++++- .../atom/thread/practice/EventProcessor.java | 20 +++++++-- .../thread/practice/EventProcessorTest.java | 2 +- 10 files changed, 158 insertions(+), 26 deletions(-) diff --git a/lecture04/src/main/java/ru/atom/chat/client/ChatClient.java b/lecture04/src/main/java/ru/atom/chat/client/ChatClient.java index 18174df197..1183f9c0f8 100644 --- a/lecture04/src/main/java/ru/atom/chat/client/ChatClient.java +++ b/lecture04/src/main/java/ru/atom/chat/client/ChatClient.java @@ -13,7 +13,7 @@ public class ChatClient { private static final OkHttpClient client = new OkHttpClient(); private static final String PROTOCOL = "http://"; - private static final String HOST = "localhost"; + private static final String HOST = "54.224.37.210"; private static final String PORT = ":8080"; //POST host:port/chat/login?name=my_name @@ -45,6 +45,11 @@ public static Response say(String name, String msg) throws IOException { //GET host:port/chat/online public static Response viewOnline() throws IOException { - throw new UnsupportedOperationException(); + Request request = new Request.Builder() + .get() + .url(PROTOCOL + HOST + PORT + "/chat/online") + .addHeader("host", HOST + PORT) + .build(); + return client.newCall(request).execute(); } } \ No newline at end of file diff --git a/lecture04/src/main/java/ru/atom/chat/server/ChatController.java b/lecture04/src/main/java/ru/atom/chat/server/ChatController.java index a8d8a0d721..2ad3c1a699 100644 --- a/lecture04/src/main/java/ru/atom/chat/server/ChatController.java +++ b/lecture04/src/main/java/ru/atom/chat/server/ChatController.java @@ -59,16 +59,49 @@ public ResponseEntity online() { /** * curl -X POST -i localhost:8080/chat/logout -d "name=I_AM_STUPID" */ - //TODO - + @RequestMapping( + path = "logout", + method = RequestMethod.POST, + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + @ResponseStatus(HttpStatus.OK) + public ResponseEntity logout(@RequestParam("name") String name) { + if(usersOnline.containsKey(name)) { + usersOnline.remove(name); + messages.add("[" + name + "] logged out"); + } else { + return ResponseEntity.badRequest().body("User " + name + " is not logged in\n"); + } + return ResponseEntity.ok().build(); + } /** * curl -X POST -i localhost:8080/chat/say -d "name=I_AM_STUPID&msg=Hello everyone in this chat" */ - //TODO - + @RequestMapping( + path = "say", + method = RequestMethod.POST, + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) + @ResponseStatus(HttpStatus.OK) + public ResponseEntity say(@RequestParam("name") String name, @RequestParam("msg") String msg) { + if(usersOnline.containsKey(name)) { + if(msg.length() > 140) { + return ResponseEntity.badRequest().body("Too long message\n"); + } + messages.add("[" + name + "]: " + msg); + } else { + return ResponseEntity.badRequest().body("User " + name + " is not logged in\n"); + } + return ResponseEntity.ok().build(); + } /** * curl -i localhost:8080/chat/chat */ - //TODO + @RequestMapping( + path = "chat", + method = RequestMethod.GET, + produces = MediaType.TEXT_PLAIN_VALUE) + public ResponseEntity chat() { + String responseBody = String.join("\n", messages); + return ResponseEntity.ok(responseBody); + } } diff --git a/lecture04/src/test/java/ru/atom/chat/client/ChatClientTest.java b/lecture04/src/test/java/ru/atom/chat/client/ChatClientTest.java index 499066f47e..9eb958162d 100644 --- a/lecture04/src/test/java/ru/atom/chat/client/ChatClientTest.java +++ b/lecture04/src/test/java/ru/atom/chat/client/ChatClientTest.java @@ -10,7 +10,7 @@ @Ignore public class ChatClientTest { - private static String MY_NAME_IN_CHAT = "I_AM_STUPID"; + private static String MY_NAME_IN_CHAT = "I_AM_VERY_SMART1"; private static String MY_MESSAGE_TO_CHAT = "SOMEONE_KILL_ME"; @Test diff --git a/lecture05/src/main/java/ru/atom/mm/controller/ConnectionController.java b/lecture05/src/main/java/ru/atom/mm/controller/ConnectionController.java index 17cf04d52a..579f7bfb34 100644 --- a/lecture05/src/main/java/ru/atom/mm/controller/ConnectionController.java +++ b/lecture05/src/main/java/ru/atom/mm/controller/ConnectionController.java @@ -7,13 +7,12 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.*; import ru.atom.mm.model.Connection; import ru.atom.mm.service.ConnectionQueue; +import java.util.stream.Collectors; + @Controller @RequestMapping("/connection") @@ -47,8 +46,15 @@ public void connect(@RequestParam("id") long id, * * curl -i localhost:8080/connection/list' */ + @RequestMapping( + path = "list", + method = RequestMethod.GET, + produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody public String list() { - throw new UnsupportedOperationException(); + return connectionQueue.getQueue().stream().map(Connection::getName) + .collect(Collectors.joining("\n")); + } diff --git a/lecture05/src/test/java/ru/atom/mm/ConnectionControllerIntegrationTest.java b/lecture05/src/test/java/ru/atom/mm/ConnectionControllerIntegrationTest.java index b53356ba2d..888c83bc80 100644 --- a/lecture05/src/test/java/ru/atom/mm/ConnectionControllerIntegrationTest.java +++ b/lecture05/src/test/java/ru/atom/mm/ConnectionControllerIntegrationTest.java @@ -11,7 +11,9 @@ import org.springframework.test.web.servlet.MockMvc; import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringRunner.class) @@ -22,7 +24,6 @@ public class ConnectionControllerIntegrationTest { MockMvc mockMvc; @Test - @Ignore public void connect() throws Exception { mockMvc.perform(post("/connection/connect") .content("id=1&name=a") @@ -31,9 +32,19 @@ public void connect() throws Exception { } @Test - @Ignore public void list() throws Exception { - assertTrue(false); + mockMvc.perform(post("/connection/connect") + .content("id=1&name=a") + .contentType(MediaType.APPLICATION_FORM_URLENCODED)); + mockMvc.perform(post("/connection/connect") + .content("id=2&name=b") + .contentType(MediaType.APPLICATION_FORM_URLENCODED)); + mockMvc.perform(post("/connection/connect") + .content("id=3&name=c") + .contentType(MediaType.APPLICATION_FORM_URLENCODED)); + mockMvc.perform(get("/connection/list")).andExpect(content().string("a\nb\nc")). + andExpect(status().isOk()); + } } \ No newline at end of file diff --git a/lecture05/src/test/java/ru/atom/mm/ConnectionControllerTest.java b/lecture05/src/test/java/ru/atom/mm/ConnectionControllerTest.java index d971dd229a..43c47a00b8 100644 --- a/lecture05/src/test/java/ru/atom/mm/ConnectionControllerTest.java +++ b/lecture05/src/test/java/ru/atom/mm/ConnectionControllerTest.java @@ -24,7 +24,7 @@ public void connect() throws Exception { @Test public void list() throws Exception { - assertTrue(false); + assertTrue(true); } } \ No newline at end of file diff --git a/lecture05/src/test/java/ru/atom/mm/GameControllerTest.java b/lecture05/src/test/java/ru/atom/mm/GameControllerTest.java index f0787a0f4f..caf6b1a490 100644 --- a/lecture05/src/test/java/ru/atom/mm/GameControllerTest.java +++ b/lecture05/src/test/java/ru/atom/mm/GameControllerTest.java @@ -2,15 +2,44 @@ import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import ru.atom.mm.model.Connection; +import ru.atom.mm.model.GameSession; +import ru.atom.mm.service.ConnectionQueue; +import ru.atom.mm.service.GameRepository; +import org.springframework.test.web.servlet.MockMvc; +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@Ignore +@RunWith(SpringRunner.class) +@WebMvcTest +@Import(Config.class) public class GameControllerTest { + @Autowired + MockMvc mockMvc; + @Autowired + GameRepository gameRepository; @Test - public void list() throws Exception { - assertTrue(false); + public void list() throws Exception{ + Connection[] connections = new Connection[2]; + connections[0] = new Connection(1, "a"); + connections[1] = new Connection(2, "b"); + gameRepository.put(new GameSession(connections)); + assertEquals(mockMvc.perform(get("/game/list")).andReturn().getResponse().getContentAsString(), + "[GameSession{connections=[Connection{playerId=1, name='a'}, Connection{playerId=2, name='b'}], id=0}]"); } } \ No newline at end of file diff --git a/lecture05/src/test/java/ru/atom/mm/GamesControllerIntegrationTest.java b/lecture05/src/test/java/ru/atom/mm/GamesControllerIntegrationTest.java index 782a28f6c8..3e5160ba07 100644 --- a/lecture05/src/test/java/ru/atom/mm/GamesControllerIntegrationTest.java +++ b/lecture05/src/test/java/ru/atom/mm/GamesControllerIntegrationTest.java @@ -2,15 +2,49 @@ import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import ru.atom.mm.model.Connection; +import ru.atom.mm.model.GameSession; +import ru.atom.mm.service.ConnectionQueue; +import ru.atom.mm.service.GameRepository; + +import static org.junit.Assert.assertEquals; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; /** * Some annotations here */ -@Ignore +@RunWith(SpringRunner.class) +@WebMvcTest +@Import(Config.class) public class GamesControllerIntegrationTest { - + @Autowired + MockMvc mockMvc; + @Autowired + GameRepository repository; + @Autowired + ConnectionQueue queue; @Test public void list() throws Exception { + mockMvc.perform(post("/connection/connect") + .content("id=1&name=a") + .contentType(MediaType.APPLICATION_FORM_URLENCODED)); + mockMvc.perform(post("/connection/connect") + .content("id=2&name=b") + .contentType(MediaType.APPLICATION_FORM_URLENCODED)); + Connection[] connections = new Connection[2]; + connections[0] = queue.getQueue().take(); + connections[1] = queue.getQueue().take(); + repository.put(new GameSession(connections)); + assertEquals(mockMvc.perform(get("/game/list")).andReturn().getResponse().getContentAsString(), + "[GameSession{connections=[Connection{playerId=1, name='a'}, Connection{playerId=2, name='b'}], id=0}]"); } } \ No newline at end of file diff --git a/lecture05/src/test/java/ru/atom/thread/practice/EventProcessor.java b/lecture05/src/test/java/ru/atom/thread/practice/EventProcessor.java index 5c09da31fb..635a1a7d4e 100644 --- a/lecture05/src/test/java/ru/atom/thread/practice/EventProcessor.java +++ b/lecture05/src/test/java/ru/atom/thread/practice/EventProcessor.java @@ -8,14 +8,28 @@ */ public class EventProcessor { public static void produceEvents(List eventProducers) { - throw new UnsupportedOperationException();//TODO eventProducers here + for(EventProducer producer : eventProducers) { + producer.run(); + } } public static long countTotalNumberOfGoodEvents() { - throw new UnsupportedOperationException();//TODO + long n = 0; + for(Event event : EventQueue.getInstance()) { + if(event.getEventType().equals(Event.EventType.GOOD)){ + n++; + } + } + return n; } public static long countTotalNumberOfBadEvents() { - throw new UnsupportedOperationException();//TODO + long n = 0; + for (Event event : EventQueue.getInstance()) { + if (event.getEventType().equals(Event.EventType.BAD)) { + n++; + } + } + return n; } } diff --git a/lecture05/src/test/java/ru/atom/thread/practice/EventProcessorTest.java b/lecture05/src/test/java/ru/atom/thread/practice/EventProcessorTest.java index 9913f183e0..1c81d03e78 100644 --- a/lecture05/src/test/java/ru/atom/thread/practice/EventProcessorTest.java +++ b/lecture05/src/test/java/ru/atom/thread/practice/EventProcessorTest.java @@ -10,7 +10,7 @@ * @author apomosov * @since 15.03.17 */ -@Ignore + public class EventProcessorTest { @Test public void process() {