diff --git a/src/main/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePlugin.java b/src/main/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePlugin.java index 4dfb1c0..2976ea4 100644 --- a/src/main/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePlugin.java +++ b/src/main/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePlugin.java @@ -115,11 +115,11 @@ private Language getLanguage(@NotNull final JSONObject singleSession) { final JSONArray categoryItems = category.getJSONArray("categoryItems"); final JSONObject firstCategoryItem = categoryItems.getJSONObject(0); final int languageId = firstCategoryItem.getInt("id"); - switch (languageId) { - case LANGUAGE_ID_ENGLISH: return Language.EN; - case LANGUAGE_ID_GERMAN: return Language.DE; - default: continue; - } + return switch (languageId) { + case LANGUAGE_ID_ENGLISH -> Language.EN; + case LANGUAGE_ID_GERMAN -> Language.DE; + default -> throw new JSONException("Unknown language ID: " + languageId); + }; } } return Language.UNKNOWN; diff --git a/src/test/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePluginTest.java b/src/test/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePluginTest.java new file mode 100644 index 0000000..d3ca008 --- /dev/null +++ b/src/test/java/swiss/fihlon/apus/plugin/event/sessionize/SessionizePluginTest.java @@ -0,0 +1,97 @@ +/* + * Apus - A social wall for conferences with additional features. + * Copyright (C) Marcus Fihlon and the individual contributors to Apus. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package swiss.fihlon.apus.plugin.event.sessionize; + +import org.junit.jupiter.api.Test; +import swiss.fihlon.apus.configuration.Configuration; +import swiss.fihlon.apus.event.Language; +import swiss.fihlon.apus.event.Room; +import swiss.fihlon.apus.event.Session; +import swiss.fihlon.apus.event.SessionImportException; +import swiss.fihlon.apus.event.Speaker; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class SessionizePluginTest { + + @Test + void isEnabled() { + final var configuration = mock(Configuration.class); + final var sessionizeConfig = new SessionizeConfig("1", ""); + when(configuration.getSessionize()).thenReturn(sessionizeConfig); + + final var sessionizePlugin = new SessionizePlugin(configuration); + assertTrue(sessionizePlugin.isEnabled()); + } + + @Test + void isDisabled() { + final var configuration = mock(Configuration.class); + final var sessionizeConfig = new SessionizeConfig("0", ""); + when(configuration.getSessionize()).thenReturn(sessionizeConfig); + + final var sessionizePlugin = new SessionizePlugin(configuration); + assertFalse(sessionizePlugin.isEnabled()); + } + + @Test + void getSessions() { + final var configuration = mock(Configuration.class); + final var sessionizeConfig = new SessionizeConfig("BBAD", "file:src/test/resources/sessionize.json?eventId=%s"); + when(configuration.getSessionize()).thenReturn(sessionizeConfig); + + final var sessionizePlugin = new SessionizePlugin(configuration); + final var sessions = sessionizePlugin.getSessions(); + assertEquals(8, sessions.size()); + + final var sessionIds = sessions.stream().map(Session::id).toList(); + for (int counter = 1; counter <= 8; counter++) { + final var sessionId = String.format("BBAD:%d", counter); + assertTrue(sessionIds.contains(sessionId)); + } + + // full check of session with ID "BBAD:5" + final var session = sessions.get(4); + assertEquals("BBAD:5", session.id()); + assertEquals(LocalDateTime.of(2024, 1, 3, 19, 0), session.startDate()); + assertEquals(LocalDateTime.of(2024, 1, 3, 19, 45), session.endDate()); + assertEquals(new Room("Room A"), session.room()); + assertEquals("Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat", session.title()); + assertEquals(2, session.speakers().size()); + assertEquals(new Speaker("Saul Goodman"), session.speakers().get(0)); + assertEquals(new Speaker("Mike Ehrmantraut"), session.speakers().get(1)); + assertEquals(Language.DE, session.language()); + } + + @Test + void parseExceptionHandling() { + final var configuration = mock(Configuration.class); + final var sessionizeConfig = new SessionizeConfig("1", "file:src/test/resources/sessionize-broken.json?eventId=%s"); + when(configuration.getSessionize()).thenReturn(sessionizeConfig); + + final var sessionizePlugin = new SessionizePlugin(configuration); + assertThrows(SessionImportException.class, sessionizePlugin::getSessions); + } +} diff --git a/src/test/resources/sessionize-broken.json b/src/test/resources/sessionize-broken.json new file mode 100644 index 0000000..d1d1b36 --- /dev/null +++ b/src/test/resources/sessionize-broken.json @@ -0,0 +1,35 @@ +[ + { + "groupId": null, + "groupName": "All", + "sessions": [ + { + "id": "1", + "title": "Lorem ipsum dolor sit amet", + "startsAt": "2024-01-01T09:45:00", + "endsAt": "2024-01-01T10:00:00", + "speakers": [ + { + "name": "Walter White" + }, + { + "name": "Jesse Pinkman" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 123456, + "name": "Foobar" + } + ] + } + ], + "room": "Room A" + } + ] + } +] diff --git a/src/test/resources/sessionize.json b/src/test/resources/sessionize.json new file mode 100644 index 0000000..327a961 --- /dev/null +++ b/src/test/resources/sessionize.json @@ -0,0 +1,209 @@ +[ + { + "groupId": null, + "groupName": "All", + "sessions": [ + { + "id": "1", + "title": "Lorem ipsum dolor sit amet", + "startsAt": "2024-01-01T09:45:00", + "endsAt": "2024-01-01T10:00:00", + "speakers": [ + { + "name": "Walter White" + }, + { + "name": "Jesse Pinkman" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242156, + "name": "German" + } + ] + } + ], + "room": "Room A" + }, + { + "id": "2", + "title": "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat", + "startsAt": "2024-01-01T10:00:00", + "endsAt": "2024-01-01T10:30:00", + "speakers": [ + { + "name": "Walter White" + }, + { + "name": "Jesse Pinkman" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242155, + "name": "English" + } + ] + } + ], + "room": "Room A" + }, + { + "id": "3", + "title": "Ut wisi enim ad minim veniam", + "startsAt": "2024-01-02T19:00:00", + "endsAt": "2024-01-02T19:45:00", + "speakers": [ + { + "name": "Gustavo Fring" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242156, + "name": "German" + } + ] + } + ], + "room": "Room A" + }, + { + "id": "4", + "title": "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum", + "startsAt": "2024-01-02T11:00:00", + "endsAt": "2024-01-02T11:45:00", + "speakers": [ + { + "name": "Hank Schrader" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242155, + "name": "English" + } + ] + } + ], + "room": "Room B" + }, + { + "id": "5", + "title": "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat", + "startsAt": "2024-01-03T19:00:00", + "endsAt": "2024-01-03T19:45:00", + "speakers": [ + { + "name": "Saul Goodman" + }, + { + "name": "Mike Ehrmantraut" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242156, + "name": "German" + } + ] + } + ], + "room": "Room A" + }, + { + "id": "6", + "title": "At vero eos et accusam et justo duo dolores et ea rebum", + "startsAt": "2024-01-03T11:00:00", + "endsAt": "2024-01-03T12:45:00", + "speakers": [ + { + "name": "Skyler White" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242155, + "name": "English" + } + ] + } + ], + "room": "Room B" + }, + { + "id": "7", + "title": "Consetetur sadipscing elitr", + "startsAt": "2024-01-04T10:00:00", + "endsAt": "2024-01-04T12:45:00", + "speakers": [ + { + "name": "Marie Schrader" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242155, + "name": "English" + } + ] + } + ], + "room": "Room A" + }, + { + "id": "8", + "title": "Consetetur sadipscing elitr", + "startsAt": "2024-01-04T13:00:00", + "endsAt": "2024-01-04T14:45:00", + "speakers": [ + { + "name": "Lydia Rodarte-Quayle" + } + ], + "categories": [ + { + "id": 68911, + "name": "Language", + "categoryItems": [ + { + "id": 242156, + "name": "German" + } + ] + } + ], + "room": "Room A" + } + ] + } +]