diff --git a/pom.xml b/pom.xml index 9231c8e..dff2edd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,6 @@ - + 4.0.0 de.svenkubiak jpushover @@ -178,6 +180,14 @@ + + + org.junit.jupiter + junit-jupiter-engine + 5.6.2 + test + + ossrh diff --git a/src/main/java/de/svenkubiak/jpushover/apis/Glance.java b/src/main/java/de/svenkubiak/jpushover/apis/Glance.java index 9eb8aa2..9d008b5 100644 --- a/src/main/java/de/svenkubiak/jpushover/apis/Glance.java +++ b/src/main/java/de/svenkubiak/jpushover/apis/Glance.java @@ -25,6 +25,9 @@ public class Glance implements API { private String proxyHost; private int proxyPort; + public Glance() { + } + /** * Your application's API token * (required) @@ -32,7 +35,7 @@ public class Glance implements API { * @param token The pushover API token * @return Glance instance */ - public API withToken(String token) { + public Glance withToken(String token) { Objects.requireNonNull(token, "token can not be null"); body.put(Param.TOKEN.toString(), token); @@ -47,7 +50,7 @@ public class Glance implements API { * @param user The username * @return Glance instance */ - public API withUser(String user) { + public Glance withUser(String user) { Objects.requireNonNull(user, "user can not be null"); body.put(Param.USER.toString(), user); @@ -62,7 +65,7 @@ public class Glance implements API { * @param device The device name * @return Glance instance */ - public API withDevice(String device) { + public Glance withDevice(String device) { Objects.requireNonNull(device, "device can not be null"); body.put(Param.DEVICE.toString(), device); @@ -75,7 +78,7 @@ public class Glance implements API { * @param title the title to use * @return Glance instance */ - public API withTitle(String title) { + public Glance withTitle(String title) { Objects.requireNonNull(title, "title can not be null"); Validate.checkArgument(title.length() <= 100, "Title must not exceed a length of 100 characters"); @@ -89,7 +92,7 @@ public class Glance implements API { * @param text the text to use * @return Glance instance */ - public API withText(String text) { + public Glance withText(String text) { Objects.requireNonNull(text, "text can not be null"); Validate.checkArgument(text.length() <= 100, "Text must not exceed a length of 100 characters"); @@ -103,7 +106,7 @@ public class Glance implements API { * @param subtext the subtext to use * @return Glance instance */ - public API withSubtext(String subtext) { + public Glance withSubtext(String subtext) { Objects.requireNonNull(subtext, "subtext can not be null"); Validate.checkArgument(subtext.length() <= 100, "Subtext must not exceed a length of 100 characters"); @@ -117,7 +120,7 @@ public class Glance implements API { * @param count the count to use * @return Glance instance */ - public API withCount(int count) { + public Glance withCount(int count) { body.put(Param.COUNT.toString(), String.valueOf(count)); return this; } @@ -128,7 +131,7 @@ public class Glance implements API { * @param percent the percent to use * @return GLance instance */ - public API withPercent(int percent) { + public Glance withPercent(int percent) { body.put(Param.PERCENT.toString(), String.valueOf(percent)); return this; } @@ -164,4 +167,10 @@ public class Glance implements API { return AsyncService.getInstance().execute(new AsyncExecutor(this)); } + + public String getValue(String param) { + Objects.requireNonNull(param, "param can not be null"); + + return body.get(param); + } } \ No newline at end of file diff --git a/src/main/java/de/svenkubiak/jpushover/apis/Message.java b/src/main/java/de/svenkubiak/jpushover/apis/Message.java index 7d24326..6a1953a 100644 --- a/src/main/java/de/svenkubiak/jpushover/apis/Message.java +++ b/src/main/java/de/svenkubiak/jpushover/apis/Message.java @@ -39,7 +39,7 @@ public class Message implements API { * @param token The pushover API token * @return Message instance */ - public final Message withToken(String token) { + public Message withToken(String token) { body.put(Param.TOKEN.toString(), token); return this; } @@ -52,7 +52,7 @@ public class Message implements API { * @param user The username * @return Message instance */ - public final Message withUser(String user) { + public Message withUser(String user) { body.put(Param.USER.toString(), user); return this; } @@ -65,7 +65,7 @@ public class Message implements API { * @param retry Number of seconds * @return Message instance */ - public final Message withRetry(int retry) { + public Message withRetry(int retry) { body.put(Param.RETRY.toString(), String.valueOf(retry)); return this; } @@ -78,7 +78,7 @@ public class Message implements API { * @param expire Number of seconds * @return Message instance */ - public final Message withExpire(int expire) { + public Message withExpire(int expire) { body.put(Param.EXPIRE.toString(), String.valueOf(expire)); return this; } @@ -90,7 +90,7 @@ public class Message implements API { * @param message The message to sent * @return Message instance */ - public final Message withMessage(String message) { + public Message withMessage(String message) { body.put(Param.MESSAGE.toString(), message); return this; } @@ -103,7 +103,7 @@ public class Message implements API { * @param device The device name * @return Message instance */ - public final Message withDevice(String device) { + public Message withDevice(String device) { body.put(Param.DEVICE.toString(), device); return this; } @@ -115,7 +115,7 @@ public class Message implements API { * @param title The title * @return Message instance */ - public final Message withTitle(String title) { + public Message withTitle(String title) { body.put(Param.TITLE.toString(), title); return this; } @@ -127,7 +127,7 @@ public class Message implements API { * @param url The url * @return Message instance */ - public final Message withUrl(String url) { + public Message withUrl(String url) { body.put(Param.URL.toString(), url); body.put(Param.URL_TITLE.toString(), url); return this; @@ -140,7 +140,7 @@ public class Message implements API { * * @return Message instance */ - public final Message enableMonospace() { + public Message enableMonospace() { body.put(Param.MONOSPACE.toString(), "1"); body.put(Param.HTML.toString(), "0"); return this; @@ -153,7 +153,7 @@ public class Message implements API { * * @return Message instance */ - public final Message enableHtml() { + public Message enableHtml() { body.put(Param.MONOSPACE.toString(), "0"); body.put(Param.HTML.toString(), "1"); return this; @@ -166,7 +166,7 @@ public class Message implements API { * @param urlTitle The url title * @return Message instance */ - public final Message withUrlTitle(String urlTitle) { + public Message withUrlTitle(String urlTitle) { body.put(Param.URL_TITLE.toString(), urlTitle); return this; } @@ -179,7 +179,7 @@ public class Message implements API { * @param timestamp The Unix timestamp * @return Message instance */ - public final Message withTimestamp(int timestamp) { + public Message withTimestamp(int timestamp) { body.put(Param.TIMESTAMP.toString(), String.valueOf(timestamp)); return this; } @@ -191,7 +191,7 @@ public class Message implements API { * @param priority The priority enum * @return Message instance */ - public final Message withPriority(Priority priority) { + public Message withPriority(Priority priority) { body.put(Param.PRIORITY.toString(), priority.toString()); return this; } @@ -204,7 +204,7 @@ public class Message implements API { * @param sound THe sound enum * @return Message instance */ - public final Message withSound(Sound sound) { + public Message withSound(Sound sound) { body.put(Param.SOUND.toString(), sound.toString()); return this; } @@ -219,7 +219,7 @@ public class Message implements API { * @param callback The callback URL * @return Message instance */ - public final Message withCallback(String callback) { + public Message withCallback(String callback) { body.put(Param.CALLBACK.toString(), callback); return this; } @@ -231,7 +231,7 @@ public class Message implements API { * @param proxyPort The port that should be used for the Proxy * @return Message instance */ - public final Message withProxy(String proxyHost, int proxyPort) { + public Message withProxy(String proxyHost, int proxyPort) { this.proxyHost = proxyHost; this.proxyPort = proxyPort; return this; @@ -276,7 +276,7 @@ public class Message implements API { * @throws InterruptedException if sending the message fails */ @Override - public final PushoverResponse push() throws IOException, InterruptedException { + public PushoverResponse push() throws IOException, InterruptedException { Objects.requireNonNull(body.get(Param.TOKEN.toString()), "Token is required for validation"); Objects.requireNonNull(body.get(Param.USER.toString()), "User is required for validation"); Objects.requireNonNull(body.get(Param.MESSAGE.toString()), "Message is required for a message"); @@ -322,4 +322,10 @@ public class Message implements API { return AsyncService.getInstance().execute(new AsyncExecutor(this)); } + + public String getValue(String param) { + Objects.requireNonNull(param, "param can not be null"); + + return body.get(param); + } } \ No newline at end of file diff --git a/src/test/java/jpushover/apis/GlanceTests.java b/src/test/java/jpushover/apis/GlanceTests.java new file mode 100644 index 0000000..a923ec9 --- /dev/null +++ b/src/test/java/jpushover/apis/GlanceTests.java @@ -0,0 +1,117 @@ +package jpushover.apis; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import de.svenkubiak.jpushover.JPushover; +import de.svenkubiak.jpushover.apis.Glance; +import de.svenkubiak.jpushover.enums.Param; + +public class GlanceTests { + + @Test + void testConstruct() { + //given + Glance glance = JPushover.newGlance(); + + //then + assertTrue(glance instanceof Glance); + } + + @Test + void testWithToken() { + //given + String value = "myToken"; + + //when + Glance glance = JPushover.newGlance().withToken(value); + + //then + assertTrue(glance.getValue(Param.TOKEN.toString()).equals(value)); + } + + @Test + void testWithUser() { + //given + String value = "myUser"; + + //when + Glance glance = JPushover.newGlance().withUser(value); + + //then + assertTrue(glance.getValue(Param.USER.toString()).equals(value)); + } + + @Test + void testWithDevice() { + //given + String value = "myDevice"; + + //when + Glance glance = JPushover.newGlance().withDevice(value); + + //then + assertTrue(glance.getValue(Param.DEVICE.toString()).equals(value)); + } + + @Test + void testWithTitle() { + //given + String value = "myTitle"; + + //when + Glance glance = JPushover.newGlance().withTitle(value); + + //then + assertTrue(glance.getValue(Param.TITLE.toString()).equals(value)); + } + + @Test + void testWithText() { + //given + String value = "myText"; + + //when + Glance glance = JPushover.newGlance().withText(value); + + //then + assertTrue(glance.getValue(Param.TEXT.toString()).equals(value)); + } + + @Test + void testWithSubtext() { + //given + String value = "mySubtext"; + + //when + Glance glance = JPushover.newGlance().withSubtext(value); + + //then + assertTrue(glance.getValue(Param.SUBTEXT.toString()).equals(value)); + } + + @Test + void testWithCount() { + //given + int value = 23; + + //when + Glance glance = JPushover.newGlance().withCount(value); + + //then + assertTrue(glance.getValue(Param.COUNT.toString()).equals(String.valueOf(value))); + } + + @Test + void testWithPercentage() { + //given + int value = 42; + + //when + Glance glance = JPushover.newGlance().withPercent(value); + + //then + assertTrue(glance.getValue(Param.PERCENT.toString()).equals(String.valueOf(value))); + } +} diff --git a/src/test/java/jpushover/apis/MessageTests.java b/src/test/java/jpushover/apis/MessageTests.java new file mode 100644 index 0000000..96fff44 --- /dev/null +++ b/src/test/java/jpushover/apis/MessageTests.java @@ -0,0 +1,209 @@ +package jpushover.apis; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import de.svenkubiak.jpushover.JPushover; +import de.svenkubiak.jpushover.apis.Message; +import de.svenkubiak.jpushover.enums.Param; +import de.svenkubiak.jpushover.enums.Priority; +import de.svenkubiak.jpushover.enums.Sound; + +public class MessageTests { + + @Test + void testConstruct() { + //given + Message message = JPushover.newMessage(); + + //then + assertTrue(message instanceof Message); + } + + @Test + void testDefaults() { + //given + Message message = JPushover.newMessage(); + + //then + assertTrue(message.getValue(Param.PRIORITY.toString()).equals(Priority.NORMAL.toString())); + assertTrue(message.getValue(Param.SOUND.toString()).equals(Sound.PUSHOVER.toString())); + } + + @Test + void testWithToken() { + //given + String value = "myToken"; + + //when + Message message = JPushover.newMessage().withToken(value); + + //then + assertTrue(message.getValue(Param.TOKEN.toString()).equals(value)); + } + + @Test + void testWithUser() { + //given + String value = "myUser"; + + //when + Message message = JPushover.newMessage().withUser(value); + + //then + assertTrue(message.getValue(Param.USER.toString()).equals(value)); + } + + @Test + void testWithRetry() { + //given + int value = 3; + + //when + Message message = JPushover.newMessage().withRetry(value); + + //then + assertTrue(message.getValue(Param.RETRY.toString()).equals(String.valueOf(value))); + } + + @Test + void testWithExpire() { + //given + int value = 5; + + //when + Message message = JPushover.newMessage().withExpire(value); + + //then + assertTrue(message.getValue(Param.EXPIRE.toString()).equals(String.valueOf(value))); + } + + @Test + void testWithMessage() { + //given + String value = "myMessage"; + + //when + Message message = JPushover.newMessage().withMessage(value); + + //then + assertTrue(message.getValue(Param.MESSAGE.toString()).equals(String.valueOf(value))); + } + + @Test + void testWithDevice() { + //given + String value = "myDevice"; + + //when + Message message = JPushover.newMessage().withDevice(value); + + //then + assertTrue(message.getValue(Param.DEVICE.toString()).equals(value)); + } + + @Test + void testWithTitle() { + //given + String value = "myTitle"; + + //when + Message message = JPushover.newMessage().withTitle(value); + + //then + assertTrue(message.getValue(Param.TITLE.toString()).equals(value)); + } + + @Test + void testWithUrl() { + //given + String value = "myUrl"; + + //when + Message message = JPushover.newMessage().withUrl(value); + + //then + assertTrue(message.getValue(Param.URL.toString()).equals(value)); + } + + @Test + void testWithUrlTitle() { + //given + String value = "myUrlTitle"; + + //when + Message message = JPushover.newMessage().withUrlTitle(value); + + //then + assertTrue(message.getValue(Param.URL_TITLE.toString()).equals(value)); + } + + @Test + void testEnableMonospace() { + //when + Message message = JPushover.newMessage().enableMonospace(); + + //then + assertTrue(message.getValue(Param.MONOSPACE.toString()).equals("1")); + assertTrue(message.getValue(Param.HTML.toString()).equals("0")); + } + + @Test + void testEnableHtml() { + //when + Message message = JPushover.newMessage().enableHtml(); + + //then + assertTrue(message.getValue(Param.MONOSPACE.toString()).equals("0")); + assertTrue(message.getValue(Param.HTML.toString()).equals("1")); + } + + @Test + void testWithTimestamp() { + //given + int value = 555; + + //when + Message message = JPushover.newMessage().withTimestamp(value); + + //then + assertTrue(message.getValue(Param.TIMESTAMP.toString()).equals(String.valueOf(value))); + } + + @Test + void testWithPriority() { + //given + Priority value = Priority.EMERGENCY; + + //when + Message message = JPushover.newMessage().withPriority(value); + + //then + assertTrue(message.getValue(Param.PRIORITY.toString()).equals(value.toString())); + } + + @Test + void testWithSound() { + //given + Sound value = Sound.BUGLE; + + //when + Message message = JPushover.newMessage().withSound(value); + + //then + assertTrue(message.getValue(Param.SOUND.toString()).equals(value.toString())); + } + + @Test + void testWithCallback() { + //given + String value = "myCallback"; + + //when + Message message = JPushover.newMessage().withCallback(value); + + //then + assertTrue(message.getValue(Param.CALLBACK.toString()).equals(value)); + } +} diff --git a/src/test/java/jpushover/utils/ValidateTests.java b/src/test/java/jpushover/utils/ValidateTests.java new file mode 100644 index 0000000..7dca058 --- /dev/null +++ b/src/test/java/jpushover/utils/ValidateTests.java @@ -0,0 +1,37 @@ +package jpushover.utils; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import de.svenkubiak.jpushover.utils.Validate; + +public class ValidateTests { + private static final String THIS_IS_AN_ERROR_MESSAGE = "This is an error message"; + + @Test + void testValidateFalse() { + //given + String expectedMessage = THIS_IS_AN_ERROR_MESSAGE; + boolean value = false; + + //when + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + Validate.checkArgument(value, THIS_IS_AN_ERROR_MESSAGE); + }); + String actualMessage = exception.getMessage(); + + //then + assertTrue(actualMessage.equals(expectedMessage)); + } + + @Test + void testValidateTrue() { + //given + boolean value = true; + + //then + Validate.checkArgument(value, THIS_IS_AN_ERROR_MESSAGE); + } +} \ No newline at end of file