| Eliza Margaretha | 38a9466 | 2014-11-20 13:48:00 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | import de.ids_mannheim.korap.response.Messages; |
| 6 | import de.ids_mannheim.korap.response.Notifications; |
| 7 | |
| 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | import com.fasterxml.jackson.databind.JsonNode; |
| 10 | |
| 11 | import static org.junit.Assert.*; |
| 12 | import org.junit.Test; |
| 13 | import org.junit.Ignore; |
| 14 | import org.junit.runner.RunWith; |
| 15 | import org.junit.runners.JUnit4; |
| 16 | |
| 17 | @RunWith(JUnit4.class) |
| 18 | public class TestNotifications { |
| 19 | |
| 20 | ObjectMapper mapper = new ObjectMapper(); |
| 21 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 22 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 23 | @Test |
| 24 | public void testNotification () { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 25 | Notifications notes = new Notifications(); |
| 26 | assertEquals("{}", notes.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 29 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 30 | @Test |
| 31 | public void testNotificationWarnings () throws IOException { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 32 | Notifications notes = new Notifications(); |
| 33 | assertFalse(notes.hasWarnings()); |
| 34 | assertFalse(notes.hasMessages()); |
| 35 | assertFalse(notes.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 36 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 37 | notes.addWarning(613, "Foo"); |
| 38 | notes.addWarning(614, "Bar", "Spiel"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 39 | |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 40 | assertEquals( |
| 41 | "{\"warnings\":[[613,\"Foo\"],[614,\"Bar\"," + "\"Spiel\"]]}", |
| 42 | notes.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 43 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 44 | assertTrue(notes.hasWarnings()); |
| 45 | assertFalse(notes.hasMessages()); |
| 46 | assertFalse(notes.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 47 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 48 | notes.addError(412, "Test"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 49 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 50 | assertTrue(notes.hasWarnings()); |
| 51 | assertFalse(notes.hasMessages()); |
| 52 | assertTrue(notes.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 53 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 54 | JsonNode noteJson = mapper.readTree(notes.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 55 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 56 | // {"warnings":[[613,"Foo"],[614,"Bar","Spiel"]],"errors":[[412,"Test"]]} |
| 57 | assertEquals(613, noteJson.at("/warnings/0/0").asInt()); |
| 58 | assertEquals("Foo", noteJson.at("/warnings/0/1").asText()); |
| 59 | assertEquals(614, noteJson.at("/warnings/1/0").asInt()); |
| 60 | assertEquals("Bar", noteJson.at("/warnings/1/1").asText()); |
| 61 | assertEquals("Spiel", noteJson.at("/warnings/1/2").asText()); |
| 62 | assertEquals(412, noteJson.at("/errors/0/0").asInt()); |
| 63 | assertEquals("Test", noteJson.at("/errors/0/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 64 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 65 | notes.addMessage(567, "Probe", "huhu", "hihi"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 66 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 67 | assertTrue(notes.hasWarnings()); |
| 68 | assertTrue(notes.hasMessages()); |
| 69 | assertTrue(notes.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 70 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 71 | noteJson = mapper.readTree(notes.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 72 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 73 | // {"warnings":[[613,"Foo"],[614,"Bar","Spiel"]], |
| 74 | // "errors":[[412,"Test"]]} |
| 75 | assertEquals(613, noteJson.at("/warnings/0/0").asInt()); |
| 76 | assertEquals("Foo", noteJson.at("/warnings/0/1").asText()); |
| 77 | assertEquals(614, noteJson.at("/warnings/1/0").asInt()); |
| 78 | assertEquals("Bar", noteJson.at("/warnings/1/1").asText()); |
| 79 | assertEquals("Spiel", noteJson.at("/warnings/1/2").asText()); |
| 80 | assertEquals(412, noteJson.at("/errors/0/0").asInt()); |
| 81 | assertEquals("Test", noteJson.at("/errors/0/1").asText()); |
| 82 | assertEquals(567, noteJson.at("/messages/0/0").asInt()); |
| 83 | assertEquals("Probe", noteJson.at("/messages/0/1").asText()); |
| 84 | assertEquals("huhu", noteJson.at("/messages/0/2").asText()); |
| 85 | assertEquals("hihi", noteJson.at("/messages/0/3").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 86 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 87 | // Todo: Check how to check for missing node |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 88 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 89 | Messages msgs = notes.getWarnings(); |
| 90 | assertEquals("[[613,\"Foo\"],[614,\"Bar\",\"Spiel\"]]", |
| 91 | msgs.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | |
| 95 | @Test |
| 96 | public void testNotificationCopy () throws IOException { |
| 97 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 98 | Notifications notes1 = new Notifications(); |
| 99 | notes1.addWarning(1, "Foo"); |
| 100 | notes1.addWarning(2, "Bar", "Test"); |
| 101 | notes1.addError(3, "Probe"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 102 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 103 | Notifications notes2 = new Notifications(); |
| 104 | notes2.addMessage(4, "Krah"); |
| 105 | notes2.addWarning(5, "Wu", "Niegel"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 106 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 107 | assertTrue(notes1.hasWarnings()); |
| 108 | assertFalse(notes1.hasMessages()); |
| 109 | assertTrue(notes1.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 110 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 111 | assertTrue(notes2.hasWarnings()); |
| 112 | assertTrue(notes2.hasMessages()); |
| 113 | assertFalse(notes2.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 114 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 115 | // Copy notations |
| 116 | notes1.copyNotificationsFrom(notes2); |
| 117 | assertTrue(notes1.hasWarnings()); |
| 118 | assertTrue(notes1.hasMessages()); |
| 119 | assertTrue(notes1.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 120 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 121 | JsonNode noteJson = mapper.readTree(notes1.toJsonString()); |
| 122 | assertEquals(1, noteJson.at("/warnings/0/0").asInt()); |
| 123 | assertEquals("Foo", noteJson.at("/warnings/0/1").asText()); |
| 124 | assertEquals(2, noteJson.at("/warnings/1/0").asInt()); |
| 125 | assertEquals("Bar", noteJson.at("/warnings/1/1").asText()); |
| 126 | assertEquals("Test", noteJson.at("/warnings/1/2").asText()); |
| 127 | assertEquals(5, noteJson.at("/warnings/2/0").asInt()); |
| 128 | assertEquals("Wu", noteJson.at("/warnings/2/1").asText()); |
| 129 | assertEquals("Niegel", noteJson.at("/warnings/2/2").asText()); |
| 130 | assertEquals(4, noteJson.at("/messages/0/0").asInt()); |
| 131 | assertEquals("Krah", noteJson.at("/messages/0/1").asText()); |
| 132 | assertEquals(3, noteJson.at("/errors/0/0").asInt()); |
| 133 | assertEquals("Probe", noteJson.at("/errors/0/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 136 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 137 | @Test |
| 138 | public void testNotificationJSONCopy () throws IOException { |
| 139 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 140 | Notifications notes1 = new Notifications(); |
| 141 | notes1.addWarning(1, "Foo"); |
| 142 | notes1.addWarning(2, "Bar", "Test"); |
| 143 | notes1.addError(3, "Probe"); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 144 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 145 | assertTrue(notes1.hasWarnings()); |
| 146 | assertFalse(notes1.hasMessages()); |
| 147 | assertTrue(notes1.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 148 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 149 | JsonNode noteJson = mapper |
| 150 | .readTree("{\"warnings\":[[5,\"Wu\",\"Niegel\"]]," |
| 151 | + "\"messages\":[[4,\"Krah\"]]}"); |
| 152 | notes1.copyNotificationsFrom(noteJson); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 153 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 154 | assertTrue(notes1.hasWarnings()); |
| 155 | assertTrue(notes1.hasMessages()); |
| 156 | assertTrue(notes1.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 157 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 158 | noteJson = mapper.readTree(notes1.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 159 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 160 | assertEquals(1, noteJson.at("/warnings/0/0").asInt()); |
| 161 | assertEquals("Foo", noteJson.at("/warnings/0/1").asText()); |
| 162 | assertEquals(2, noteJson.at("/warnings/1/0").asInt()); |
| 163 | assertEquals("Bar", noteJson.at("/warnings/1/1").asText()); |
| 164 | assertEquals("Test", noteJson.at("/warnings/1/2").asText()); |
| 165 | assertEquals(5, noteJson.at("/warnings/2/0").asInt()); |
| 166 | assertEquals("Wu", noteJson.at("/warnings/2/1").asText()); |
| 167 | assertEquals("Niegel", noteJson.at("/warnings/2/2").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 168 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 169 | assertEquals(4, noteJson.at("/messages/0/0").asInt()); |
| 170 | assertEquals("Krah", noteJson.at("/messages/0/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 171 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 172 | assertEquals(3, noteJson.at("/errors/0/0").asInt()); |
| 173 | assertEquals("Probe", noteJson.at("/errors/0/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 174 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 175 | noteJson = mapper |
| 176 | .readTree("{\"warnings\":[[8, \"Tanja\", \"Gaby\"]],\"errors\":" |
| 177 | + "[[\"Klößchen\"],[9,\"Karl\"]]}"); |
| 178 | notes1.copyNotificationsFrom(noteJson); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 179 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 180 | assertTrue(notes1.hasWarnings()); |
| 181 | assertTrue(notes1.hasMessages()); |
| 182 | assertTrue(notes1.hasErrors()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 183 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 184 | noteJson = mapper.readTree(notes1.toJsonString()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 185 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 186 | assertEquals(1, noteJson.at("/warnings/0/0").asInt()); |
| 187 | assertEquals("Foo", noteJson.at("/warnings/0/1").asText()); |
| 188 | assertEquals(2, noteJson.at("/warnings/1/0").asInt()); |
| 189 | assertEquals("Bar", noteJson.at("/warnings/1/1").asText()); |
| 190 | assertEquals("Test", noteJson.at("/warnings/1/2").asText()); |
| 191 | assertEquals(5, noteJson.at("/warnings/2/0").asInt()); |
| 192 | assertEquals("Wu", noteJson.at("/warnings/2/1").asText()); |
| 193 | assertEquals("Niegel", noteJson.at("/warnings/2/2").asText()); |
| 194 | assertEquals(8, noteJson.at("/warnings/3/0").asInt()); |
| 195 | assertEquals("Tanja", noteJson.at("/warnings/3/1").asText()); |
| 196 | assertEquals("Gaby", noteJson.at("/warnings/3/2").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 197 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 198 | assertEquals(4, noteJson.at("/messages/0/0").asInt()); |
| 199 | assertEquals("Krah", noteJson.at("/messages/0/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 200 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 201 | assertEquals(3, noteJson.at("/errors/0/0").asInt()); |
| 202 | assertEquals("Probe", noteJson.at("/errors/0/1").asText()); |
| 203 | assertEquals("Klößchen", noteJson.at("/errors/1/0").asText()); |
| 204 | assertEquals(9, noteJson.at("/errors/2/0").asInt()); |
| 205 | assertEquals("Karl", noteJson.at("/errors/2/1").asText()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 206 | }; |
| 207 | }; |