Re-added Unit Tests

This commit is contained in:
Sven Kubiak 2020-07-27 13:35:49 +02:00
parent bfd2288b1a
commit a73034b00d
6 changed files with 414 additions and 26 deletions

12
pom.xml
View File

@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.svenkubiak</groupId> <groupId>de.svenkubiak</groupId>
<artifactId>jpushover</artifactId> <artifactId>jpushover</artifactId>
@ -178,6 +180,14 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement> <distributionManagement>
<snapshotRepository> <snapshotRepository>
<id>ossrh</id> <id>ossrh</id>

View File

@ -25,6 +25,9 @@ public class Glance implements API {
private String proxyHost; private String proxyHost;
private int proxyPort; private int proxyPort;
public Glance() {
}
/** /**
* Your application's API token * Your application's API token
* (required) * (required)
@ -32,7 +35,7 @@ public class Glance implements API {
* @param token The pushover API token * @param token The pushover API token
* @return Glance instance * @return Glance instance
*/ */
public API withToken(String token) { public Glance withToken(String token) {
Objects.requireNonNull(token, "token can not be null"); Objects.requireNonNull(token, "token can not be null");
body.put(Param.TOKEN.toString(), token); body.put(Param.TOKEN.toString(), token);
@ -47,7 +50,7 @@ public class Glance implements API {
* @param user The username * @param user The username
* @return Glance instance * @return Glance instance
*/ */
public API withUser(String user) { public Glance withUser(String user) {
Objects.requireNonNull(user, "user can not be null"); Objects.requireNonNull(user, "user can not be null");
body.put(Param.USER.toString(), user); body.put(Param.USER.toString(), user);
@ -62,7 +65,7 @@ public class Glance implements API {
* @param device The device name * @param device The device name
* @return Glance instance * @return Glance instance
*/ */
public API withDevice(String device) { public Glance withDevice(String device) {
Objects.requireNonNull(device, "device can not be null"); Objects.requireNonNull(device, "device can not be null");
body.put(Param.DEVICE.toString(), device); body.put(Param.DEVICE.toString(), device);
@ -75,7 +78,7 @@ public class Glance implements API {
* @param title the title to use * @param title the title to use
* @return Glance instance * @return Glance instance
*/ */
public API withTitle(String title) { public Glance withTitle(String title) {
Objects.requireNonNull(title, "title can not be null"); Objects.requireNonNull(title, "title can not be null");
Validate.checkArgument(title.length() <= 100, "Title must not exceed a length of 100 characters"); 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 * @param text the text to use
* @return Glance instance * @return Glance instance
*/ */
public API withText(String text) { public Glance withText(String text) {
Objects.requireNonNull(text, "text can not be null"); Objects.requireNonNull(text, "text can not be null");
Validate.checkArgument(text.length() <= 100, "Text must not exceed a length of 100 characters"); 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 * @param subtext the subtext to use
* @return Glance instance * @return Glance instance
*/ */
public API withSubtext(String subtext) { public Glance withSubtext(String subtext) {
Objects.requireNonNull(subtext, "subtext can not be null"); Objects.requireNonNull(subtext, "subtext can not be null");
Validate.checkArgument(subtext.length() <= 100, "Subtext must not exceed a length of 100 characters"); 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 * @param count the count to use
* @return Glance instance * @return Glance instance
*/ */
public API withCount(int count) { public Glance withCount(int count) {
body.put(Param.COUNT.toString(), String.valueOf(count)); body.put(Param.COUNT.toString(), String.valueOf(count));
return this; return this;
} }
@ -128,7 +131,7 @@ public class Glance implements API {
* @param percent the percent to use * @param percent the percent to use
* @return GLance instance * @return GLance instance
*/ */
public API withPercent(int percent) { public Glance withPercent(int percent) {
body.put(Param.PERCENT.toString(), String.valueOf(percent)); body.put(Param.PERCENT.toString(), String.valueOf(percent));
return this; return this;
} }
@ -164,4 +167,10 @@ public class Glance implements API {
return AsyncService.getInstance().execute(new AsyncExecutor(this)); return AsyncService.getInstance().execute(new AsyncExecutor(this));
} }
public String getValue(String param) {
Objects.requireNonNull(param, "param can not be null");
return body.get(param);
}
} }

View File

@ -39,7 +39,7 @@ public class Message implements API {
* @param token The pushover API token * @param token The pushover API token
* @return Message instance * @return Message instance
*/ */
public final Message withToken(String token) { public Message withToken(String token) {
body.put(Param.TOKEN.toString(), token); body.put(Param.TOKEN.toString(), token);
return this; return this;
} }
@ -52,7 +52,7 @@ public class Message implements API {
* @param user The username * @param user The username
* @return Message instance * @return Message instance
*/ */
public final Message withUser(String user) { public Message withUser(String user) {
body.put(Param.USER.toString(), user); body.put(Param.USER.toString(), user);
return this; return this;
} }
@ -65,7 +65,7 @@ public class Message implements API {
* @param retry Number of seconds * @param retry Number of seconds
* @return Message instance * @return Message instance
*/ */
public final Message withRetry(int retry) { public Message withRetry(int retry) {
body.put(Param.RETRY.toString(), String.valueOf(retry)); body.put(Param.RETRY.toString(), String.valueOf(retry));
return this; return this;
} }
@ -78,7 +78,7 @@ public class Message implements API {
* @param expire Number of seconds * @param expire Number of seconds
* @return Message instance * @return Message instance
*/ */
public final Message withExpire(int expire) { public Message withExpire(int expire) {
body.put(Param.EXPIRE.toString(), String.valueOf(expire)); body.put(Param.EXPIRE.toString(), String.valueOf(expire));
return this; return this;
} }
@ -90,7 +90,7 @@ public class Message implements API {
* @param message The message to sent * @param message The message to sent
* @return Message instance * @return Message instance
*/ */
public final Message withMessage(String message) { public Message withMessage(String message) {
body.put(Param.MESSAGE.toString(), message); body.put(Param.MESSAGE.toString(), message);
return this; return this;
} }
@ -103,7 +103,7 @@ public class Message implements API {
* @param device The device name * @param device The device name
* @return Message instance * @return Message instance
*/ */
public final Message withDevice(String device) { public Message withDevice(String device) {
body.put(Param.DEVICE.toString(), device); body.put(Param.DEVICE.toString(), device);
return this; return this;
} }
@ -115,7 +115,7 @@ public class Message implements API {
* @param title The title * @param title The title
* @return Message instance * @return Message instance
*/ */
public final Message withTitle(String title) { public Message withTitle(String title) {
body.put(Param.TITLE.toString(), title); body.put(Param.TITLE.toString(), title);
return this; return this;
} }
@ -127,7 +127,7 @@ public class Message implements API {
* @param url The url * @param url The url
* @return Message instance * @return Message instance
*/ */
public final Message withUrl(String url) { public Message withUrl(String url) {
body.put(Param.URL.toString(), url); body.put(Param.URL.toString(), url);
body.put(Param.URL_TITLE.toString(), url); body.put(Param.URL_TITLE.toString(), url);
return this; return this;
@ -140,7 +140,7 @@ public class Message implements API {
* *
* @return Message instance * @return Message instance
*/ */
public final Message enableMonospace() { public Message enableMonospace() {
body.put(Param.MONOSPACE.toString(), "1"); body.put(Param.MONOSPACE.toString(), "1");
body.put(Param.HTML.toString(), "0"); body.put(Param.HTML.toString(), "0");
return this; return this;
@ -153,7 +153,7 @@ public class Message implements API {
* *
* @return Message instance * @return Message instance
*/ */
public final Message enableHtml() { public Message enableHtml() {
body.put(Param.MONOSPACE.toString(), "0"); body.put(Param.MONOSPACE.toString(), "0");
body.put(Param.HTML.toString(), "1"); body.put(Param.HTML.toString(), "1");
return this; return this;
@ -166,7 +166,7 @@ public class Message implements API {
* @param urlTitle The url title * @param urlTitle The url title
* @return Message instance * @return Message instance
*/ */
public final Message withUrlTitle(String urlTitle) { public Message withUrlTitle(String urlTitle) {
body.put(Param.URL_TITLE.toString(), urlTitle); body.put(Param.URL_TITLE.toString(), urlTitle);
return this; return this;
} }
@ -179,7 +179,7 @@ public class Message implements API {
* @param timestamp The Unix timestamp * @param timestamp The Unix timestamp
* @return Message instance * @return Message instance
*/ */
public final Message withTimestamp(int timestamp) { public Message withTimestamp(int timestamp) {
body.put(Param.TIMESTAMP.toString(), String.valueOf(timestamp)); body.put(Param.TIMESTAMP.toString(), String.valueOf(timestamp));
return this; return this;
} }
@ -191,7 +191,7 @@ public class Message implements API {
* @param priority The priority enum * @param priority The priority enum
* @return Message instance * @return Message instance
*/ */
public final Message withPriority(Priority priority) { public Message withPriority(Priority priority) {
body.put(Param.PRIORITY.toString(), priority.toString()); body.put(Param.PRIORITY.toString(), priority.toString());
return this; return this;
} }
@ -204,7 +204,7 @@ public class Message implements API {
* @param sound THe sound enum * @param sound THe sound enum
* @return Message instance * @return Message instance
*/ */
public final Message withSound(Sound sound) { public Message withSound(Sound sound) {
body.put(Param.SOUND.toString(), sound.toString()); body.put(Param.SOUND.toString(), sound.toString());
return this; return this;
} }
@ -219,7 +219,7 @@ public class Message implements API {
* @param callback The callback URL * @param callback The callback URL
* @return Message instance * @return Message instance
*/ */
public final Message withCallback(String callback) { public Message withCallback(String callback) {
body.put(Param.CALLBACK.toString(), callback); body.put(Param.CALLBACK.toString(), callback);
return this; return this;
} }
@ -231,7 +231,7 @@ public class Message implements API {
* @param proxyPort The port that should be used for the Proxy * @param proxyPort The port that should be used for the Proxy
* @return Message instance * @return Message instance
*/ */
public final Message withProxy(String proxyHost, int proxyPort) { public Message withProxy(String proxyHost, int proxyPort) {
this.proxyHost = proxyHost; this.proxyHost = proxyHost;
this.proxyPort = proxyPort; this.proxyPort = proxyPort;
return this; return this;
@ -276,7 +276,7 @@ public class Message implements API {
* @throws InterruptedException if sending the message fails * @throws InterruptedException if sending the message fails
*/ */
@Override @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.TOKEN.toString()), "Token is required for validation");
Objects.requireNonNull(body.get(Param.USER.toString()), "User 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"); 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)); return AsyncService.getInstance().execute(new AsyncExecutor(this));
} }
public String getValue(String param) {
Objects.requireNonNull(param, "param can not be null");
return body.get(param);
}
} }

View File

@ -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)));
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}