Added support for Glances API and completly reworked API

This commit is contained in:
Sven Kubiak
2018-11-22 14:45:16 +01:00
parent e70aa6ccb6
commit 69632b331f
17 changed files with 1023 additions and 392 deletions

View File

@ -0,0 +1,23 @@
package jpushover;
import static org.junit.Assert.assertTrue;
import de.svenkubiak.jpushover.JPushover;
import de.svenkubiak.jpushover.apis.Glance;
import de.svenkubiak.jpushover.apis.Message;
/**
*
* @author svenkubiak
*
*/
public class JPushoverTest {
public void testNewGlance() {
assertTrue(JPushover.newGlance() instanceof Glance);
}
public void testNewMessage() {
assertTrue(JPushover.newMessage() instanceof Message);
}
}

View File

@ -0,0 +1,29 @@
package jpushover;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import com.github.tomakehurst.wiremock.WireMockServer;
/**
*
* @author svenkubiak
*
*/
public final class MockServer {
private static WireMockServer wireMockServer;
private static boolean started;
public MockServer() {
}
public static void start() {
if (!started) {
System.setProperty("mode", "test");
wireMockServer = new WireMockServer(options()
.bindAddress("127.0.0.1")
);
wireMockServer.start();
started = true;
}
}
}

View File

@ -0,0 +1,77 @@
package jpushover.apis;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import de.svenkubiak.jpushover.JPushover;
import de.svenkubiak.jpushover.http.PushoverResponse;
import jpushover.MockServer;
/**
*
* @author svenkubiak
*
*/
public class GlanceTest {
private static final String APPLICATION_JSON = "application/json; charset=utf-8";
private static final String CONTENT_TYPE = "Content-Type";
private GlanceTest () {
MockServer.start();
}
@Test()
public void testTokenRequired() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/glances.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
Assertions.assertThrows(NullPointerException.class, () -> {
JPushover.newGlance().push();
});
}
@Test()
public void testUserRequired() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/glances.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
Assertions.assertThrows(NullPointerException.class, () -> {
JPushover.newGlance().withToken("token").push();
});
}
@Test()
public void testPushWithoutContent() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/glances.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
PushoverResponse response = JPushover.newGlance().withToken("token").withUser("user").push();
assertFalse(response.isSuccessful());
}
@Test
public void testPush() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/glances.json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
PushoverResponse response = JPushover.newGlance().withToken("foo").withUser("bla").withText("foobar").push();
assertTrue(response.isSuccessful());
}
}

View File

@ -0,0 +1,89 @@
package jpushover.apis;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import de.svenkubiak.jpushover.JPushover;
import de.svenkubiak.jpushover.http.PushoverResponse;
import jpushover.MockServer;
/**
*
* @author svenkubiak
*
*/
public class MessageTest {
private static final String APPLICATION_JSON = "application/json; charset=utf-8";
private static final String CONTENT_TYPE = "Content-Type";
private MessageTest () {
MockServer.start();
}
@Test()
public void testTokenRequired() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/messages.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
Assertions.assertThrows(NullPointerException.class, () -> {
JPushover.newMessage().push();
});
}
@Test()
public void testUserRequired() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/messages.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
Assertions.assertThrows(NullPointerException.class, () -> {
JPushover.newMessage().withToken("token").push();
});
}
@Test()
public void testMessageRequired() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/messages.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
Assertions.assertThrows(NullPointerException.class, () -> {
JPushover.newMessage().withToken("token").withUser("user").push();
});
}
@Test()
public void testPushWithoutContent() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/messages.json"))
.willReturn(aResponse()
.withStatus(400)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
PushoverResponse response = JPushover.newMessage().withToken("token").withUser("user").withMessage("").push();
assertFalse(response.isSuccessful());
}
@Test
public void testPush() throws IOException, InterruptedException {
stubFor(post(urlEqualTo("/1/messages.json"))
.willReturn(aResponse()
.withStatus(200)
.withHeader(CONTENT_TYPE, APPLICATION_JSON)));
PushoverResponse response = JPushover.newMessage().withToken("foo").withUser("bla").withMessage("foobar").push();
assertTrue(response.isSuccessful());
}
}

View File

@ -0,0 +1,26 @@
package jpushover.utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import de.svenkubiak.jpushover.utils.Validate;
/**
*
* @author svenkubiak
*
*/
public class ValidateTest {
@Test
public void testTrue() {
Validate.checkArgument(true, "foo");
}
@Test
public void testFalse() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
Validate.checkArgument(false, "bar");
});
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>