From d950b3cc0d26162c6ee0d154f28910d2350457dc Mon Sep 17 00:00:00 2001 From: amihaiemil Date: Fri, 27 Oct 2017 10:24:19 +0300 Subject: [PATCH] unit tests for Notifications --- .../charles/rest/model/Notifications.java | 18 ++-- .../rest/model/SimplifiedNotifications.java | 20 ++-- .../rest/model/WebhookNotifications.java | 18 ++-- .../IssueCommentNotificationTestCase.java | 95 +++++++++++++++++ .../SimplifiedNotificationsTestCase.java | 100 ++++++++++++++++++ .../model/WebhookNotificationsTestCase.java | 89 ++++++++++++++++ 6 files changed, 312 insertions(+), 28 deletions(-) create mode 100644 src/test/java/com/amihaiemil/charles/rest/model/IssueCommentNotificationTestCase.java create mode 100644 src/test/java/com/amihaiemil/charles/rest/model/SimplifiedNotificationsTestCase.java create mode 100644 src/test/java/com/amihaiemil/charles/rest/model/WebhookNotificationsTestCase.java diff --git a/src/main/java/com/amihaiemil/charles/rest/model/Notifications.java b/src/main/java/com/amihaiemil/charles/rest/model/Notifications.java index bbbe3d7..5cc61a5 100644 --- a/src/main/java/com/amihaiemil/charles/rest/model/Notifications.java +++ b/src/main/java/com/amihaiemil/charles/rest/model/Notifications.java @@ -39,14 +39,14 @@ */ public abstract class Notifications implements Iterable{ - /** - * All of them. - */ - protected final List notifications = new ArrayList<>(); - - @Override - public Iterator iterator() { - return this.notifications.iterator(); - } + /** + * All of them. + */ + protected final List notifications = new ArrayList<>(); + + @Override + public Iterator iterator() { + return this.notifications.iterator(); + } } diff --git a/src/main/java/com/amihaiemil/charles/rest/model/SimplifiedNotifications.java b/src/main/java/com/amihaiemil/charles/rest/model/SimplifiedNotifications.java index 78dfc20..f3aa439 100644 --- a/src/main/java/com/amihaiemil/charles/rest/model/SimplifiedNotifications.java +++ b/src/main/java/com/amihaiemil/charles/rest/model/SimplifiedNotifications.java @@ -39,14 +39,14 @@ * */ public final class SimplifiedNotifications extends Notifications { - /** - * Ctor. - * @param all All notifications as String. - */ - public SimplifiedNotifications(final String all) { - final JsonArray array = Json.createReader(new StringReader(all)).readArray(); - for(final JsonValue json : array) { - super.notifications.add(new SimpleJsonNotification((JsonObject) json)); - } - } + /** + * Ctor. + * @param all All notifications as String. + */ + public SimplifiedNotifications(final String all) { + final JsonArray array = Json.createReader(new StringReader(all)).readArray(); + for(final JsonValue json : array) { + super.notifications.add(new SimpleJsonNotification((JsonObject) json)); + } + } } diff --git a/src/main/java/com/amihaiemil/charles/rest/model/WebhookNotifications.java b/src/main/java/com/amihaiemil/charles/rest/model/WebhookNotifications.java index 133133f..03e4021 100644 --- a/src/main/java/com/amihaiemil/charles/rest/model/WebhookNotifications.java +++ b/src/main/java/com/amihaiemil/charles/rest/model/WebhookNotifications.java @@ -4,14 +4,14 @@ public final class WebhookNotifications extends Notifications { - /** - * Ctor. - * @param all All webhook notifications - */ - public WebhookNotifications(final JsonObject... all) { - for(final JsonObject json : all) { - super.notifications.add(new IssueCommentNotification(json)); - } - } + /** + * Ctor. + * @param all All webhook notifications + */ + public WebhookNotifications(final JsonObject... all) { + for(final JsonObject json : all) { + super.notifications.add(new IssueCommentNotification(json)); + } + } } diff --git a/src/test/java/com/amihaiemil/charles/rest/model/IssueCommentNotificationTestCase.java b/src/test/java/com/amihaiemil/charles/rest/model/IssueCommentNotificationTestCase.java new file mode 100644 index 0000000..ac914c7 --- /dev/null +++ b/src/test/java/com/amihaiemil/charles/rest/model/IssueCommentNotificationTestCase.java @@ -0,0 +1,95 @@ +/** + * Copyright (c) 2016-2017, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of charles-rest nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.amihaiemil.charles.rest.model; + +import javax.json.Json; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit tests for {@link IssueCommentNotification} + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.2 + * + */ +public final class IssueCommentNotificationTestCase { + + /** + * A IssueCommentNotification has an issue number. + */ + @Test + public void hasIssueNumber() { + final Notification notification = new IssueCommentNotification( + Json.createObjectBuilder() + .add( + "issue", + Json.createObjectBuilder().add("number", 456).build() + ) + .build() + ); + MatcherAssert.assertThat(notification.issueNumber(), Matchers.is(456)); + } + + /** + * A IssueCommentNotification has a repository fullname. + */ + @Test + public void hasRepoFullName() { + final Notification notification = new IssueCommentNotification( + Json.createObjectBuilder() + .add( + "repository", + Json.createObjectBuilder().add("full_name", "jeff/test").build() + ) + .build() + ); + MatcherAssert.assertThat(notification.repoFullName(), Matchers.equalTo("jeff/test")); + } + + /** + * A IssueCommentNotification has all its attributes. + */ + @Test + public void isComplete() { + final Notification notification = new IssueCommentNotification( + Json.createObjectBuilder() + .add( + "issue", + Json.createObjectBuilder().add("number", 899).build() + ) + .add( + "repository", + Json.createObjectBuilder().add("full_name", "jeff/test").build() + ) + .build() + ); + MatcherAssert.assertThat(notification.issueNumber(), Matchers.is(899)); + MatcherAssert.assertThat(notification.repoFullName(), Matchers.equalTo("jeff/test")); + } +} diff --git a/src/test/java/com/amihaiemil/charles/rest/model/SimplifiedNotificationsTestCase.java b/src/test/java/com/amihaiemil/charles/rest/model/SimplifiedNotificationsTestCase.java new file mode 100644 index 0000000..2256d53 --- /dev/null +++ b/src/test/java/com/amihaiemil/charles/rest/model/SimplifiedNotificationsTestCase.java @@ -0,0 +1,100 @@ +/** + * Copyright (c) 2016-2017, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of charles-rest nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.amihaiemil.charles.rest.model; + +import javax.json.Json; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit tests for {@link SimplifiedNotifications} + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.2 + * + */ +@SuppressWarnings("unused") +public final class SimplifiedNotificationsTestCase { + + + /** + * SimplifiedNotifications can iterate over all of them. + */ + @Test + public void iteratesOverAll() { + final Notifications notifs = new SimplifiedNotifications( + Json.createArrayBuilder() + .add(Json.createObjectBuilder().add("repoFullName", "jeff/test").add("issueNumber", 123).build()) + .add(Json.createObjectBuilder().add("repoFullName", "mary/tesla").add("issueNumber", 458).build()) + .add(Json.createObjectBuilder().add("repoFullName", "john/doe-repo").add("issueNumber", 142).build()) + .build() + .toString() + ); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(3)); + } + + /** + * SimplifiedNotifications can iterate over a single notification. + */ + @Test + public void iteratesOverOne() { + final Notifications notifs = new SimplifiedNotifications( + Json.createArrayBuilder() + .add(Json.createObjectBuilder().add("repoFullName", "mary/tesla").add("issueNumber", 458).build()) + .build() + .toString() + ); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(1)); + } + + /** + * SimplifiedNotifications can iterate over no notification (it is empty). + */ + @Test + public void iteratesOverNone() { + final Notifications notifs = new SimplifiedNotifications( + Json.createArrayBuilder() + .build() + .toString() + ); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(0)); + } + +} diff --git a/src/test/java/com/amihaiemil/charles/rest/model/WebhookNotificationsTestCase.java b/src/test/java/com/amihaiemil/charles/rest/model/WebhookNotificationsTestCase.java new file mode 100644 index 0000000..a3710b8 --- /dev/null +++ b/src/test/java/com/amihaiemil/charles/rest/model/WebhookNotificationsTestCase.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2016-2017, Mihai Emil Andronache + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1)Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2)Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3)Neither the name of charles-rest nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package com.amihaiemil.charles.rest.model; + +import javax.json.Json; +import javax.json.JsonObject; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Unit tests for {@link WebhookNotifications} + * @author Mihai Andronache (amihaiemil@gmail.com) + * @version $Id$ + * @since 1.0.2 + * + */ +@SuppressWarnings("unused") +public final class WebhookNotificationsTestCase { + + /** + * WebhookNotifications can iterate over all of them. + */ + @Test + public void iteratesOverAll() { + final Notifications notifs = new WebhookNotifications( + Json.createObjectBuilder().build(), + Json.createObjectBuilder().build(), + Json.createObjectBuilder().build(), + Json.createObjectBuilder().build() + ); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(4)); + } + + /** + * WebhookNotifications can iterate over a single notification. + */ + @Test + public void iteratesOverOne() { + final Notifications notifs = new WebhookNotifications( + Json.createObjectBuilder().build() + ); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(1)); + } + + /** + * WebhookNotifications can iterate over no notification (it is empty). + */ + @Test + public void iteratesOverNone() { + final Notifications notifs = new WebhookNotifications(new JsonObject[]{}); + int size = 0; + for(final Notification n : notifs) { + size++; + } + MatcherAssert.assertThat(size, Matchers.is(0)); + } +}