|
| 1 | +package test_locally.app_backend.events.payload; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonParseException; |
| 5 | +import com.slack.api.SlackConfig; |
| 6 | +import com.slack.api.app_backend.events.payload.EventsApiPayload; |
| 7 | +import com.slack.api.util.json.GsonFactory; |
| 8 | +import config.SlackTestConfig; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.List; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | +import static java.util.stream.Collectors.joining; |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | + |
| 24 | +public class SamplePayloadParsingTest { |
| 25 | + |
| 26 | + static String readWholeAsString(Path path) throws IOException { |
| 27 | + return Files.readAllLines(path, UTF_8).stream().collect(joining()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void readAll() throws Exception { |
| 32 | + SlackConfig testConfig = SlackTestConfig.get(); |
| 33 | + Gson gson = GsonFactory.createSnakeCase(testConfig); |
| 34 | + List<Path> files = Files.list(Paths.get("../json-logs/samples/events")).collect(Collectors.toList()); |
| 35 | + for (Path jsonFile : files) { |
| 36 | + String json = readWholeAsString(jsonFile); |
| 37 | + String className = jsonFile.getFileName().toString().replaceFirst("\\.json$", ""); |
| 38 | + String fqdn = "com.slack.api.app_backend.events.payload." + className; |
| 39 | + Class<EventsApiPayload<?>> clazz = (Class<EventsApiPayload<?>>) Class.forName(fqdn); |
| 40 | + try { |
| 41 | + EventsApiPayload<?> payload = gson.fromJson(json, clazz); |
| 42 | + assertThat(payload, is(notNullValue())); |
| 43 | + } catch (JsonParseException e) { |
| 44 | + String message = "Check " + fqdn + " : " + e.getMessage(); |
| 45 | + throw new RuntimeException(message, e); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | +} |
0 commit comments