Skip to content

Commit 872ef1d

Browse files
author
Chris Wilson
committed
Adds recipient list unit test to verify issue #57
1 parent 562dca3 commit 872ef1d

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed

libs/sparkpost-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>org.apache.httpcomponents</groupId>
2727
<artifactId>httpclient</artifactId>
28-
<version>4.5.2</version>
28+
<version>4.5.3</version>
2929
</dependency>
3030

3131
<!-- the following two dependencies are required for correct jmockit operation
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
package com.sparkpost.resources;
3+
4+
import org.junit.Assert;
5+
6+
import com.sparkpost.testhelpers.StubRestConnection;
7+
8+
public class BaseResourceTest {
9+
10+
protected void verifyWasGet(StubRestConnection conn) {
11+
Assert.assertTrue(conn.wasGet());
12+
Assert.assertFalse(conn.wasDelete());
13+
Assert.assertFalse(conn.wasPut());
14+
Assert.assertFalse(conn.wasPost());
15+
}
16+
17+
protected void verifyWasPut(StubRestConnection conn) {
18+
Assert.assertFalse(conn.wasGet());
19+
Assert.assertFalse(conn.wasDelete());
20+
Assert.assertTrue(conn.wasPut());
21+
Assert.assertFalse(conn.wasPost());
22+
}
23+
24+
protected void verifyWasPost(StubRestConnection conn) {
25+
Assert.assertFalse(conn.wasGet());
26+
Assert.assertFalse(conn.wasDelete());
27+
Assert.assertFalse(conn.wasPut());
28+
Assert.assertTrue(conn.wasPost());
29+
}
30+
31+
protected void verifyWasDelete(StubRestConnection conn) {
32+
Assert.assertFalse(conn.wasGet());
33+
Assert.assertTrue(conn.wasDelete());
34+
Assert.assertFalse(conn.wasPut());
35+
Assert.assertFalse(conn.wasPost());
36+
}
37+
38+
}

libs/sparkpost-lib/src/test/java/com/sparkpost/resources/ResourceMetricTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.sparkpost.model.responses.Response;
1818
import com.sparkpost.testhelpers.StubRestConnection;
1919

20-
public class ResourceMetricTests {
20+
public class ResourceMetricTests extends BaseResourceTest {
2121

2222
@BeforeClass
2323
public static void setUpClass() {
@@ -49,13 +49,6 @@ private StubRestConnection buildStubConnection() {
4949
return conn;
5050
}
5151

52-
private void verifyWasGet(StubRestConnection conn) {
53-
Assert.assertTrue(conn.wasGet());
54-
Assert.assertFalse(conn.wasDelete());
55-
Assert.assertFalse(conn.wasPut());
56-
Assert.assertFalse(conn.wasPost());
57-
}
58-
5952
@Test
6053
public void testGetDiscoverabilityLinks() throws SparkPostException {
6154
StubRestConnection conn = buildStubConnection();
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
2+
package com.sparkpost.resources;
3+
4+
import java.lang.reflect.Type;
5+
6+
import org.apache.log4j.Level;
7+
import org.apache.log4j.Logger;
8+
import org.junit.After;
9+
import org.junit.AfterClass;
10+
import org.junit.Assert;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import com.sparkpost.exception.SparkPostException;
16+
import com.sparkpost.model.RecipientList;
17+
import com.sparkpost.model.responses.RecipientListRetrieveResponse;
18+
import com.sparkpost.model.responses.RecipientListsListAllResponse;
19+
import com.sparkpost.model.responses.Response;
20+
import com.sparkpost.testhelpers.StubRestConnection;
21+
22+
public class ResourceRecipientListsTests extends BaseResourceTest {
23+
24+
@BeforeClass
25+
public static void setUpClass() {
26+
Logger.getRootLogger().setLevel(Level.DEBUG);
27+
}
28+
29+
@AfterClass
30+
public static void tearDownClass() {
31+
}
32+
33+
@Before
34+
public void setUp() {
35+
}
36+
37+
@After
38+
public void tearDown() {
39+
}
40+
41+
private static final class StubResponse extends Response {
42+
43+
public static Response decode(Response response, Type typeOfT) {
44+
return new StubResponse();
45+
}
46+
}
47+
48+
private static final class StubRecipientListRetrieveResponse extends RecipientListRetrieveResponse {
49+
50+
public static RecipientListRetrieveResponse decode(Response response, Type typeOfT) {
51+
return new StubRecipientListRetrieveResponse();
52+
}
53+
}
54+
55+
private static final class StubRecipientListsListAllResponse extends RecipientListsListAllResponse {
56+
57+
public static RecipientListsListAllResponse decode(Response response, Type typeOfT) {
58+
return new StubRecipientListsListAllResponse();
59+
}
60+
}
61+
62+
private StubRestConnection buildStubConnection(Response response) {
63+
StubRestConnection conn = new StubRestConnection(response);
64+
return conn;
65+
}
66+
67+
@Test
68+
public void testCreate() throws SparkPostException {
69+
Integer maxNumberOfRecipientErrors = 0;
70+
RecipientList recipientList = new RecipientList();
71+
72+
StubRestConnection conn = buildStubConnection(new StubResponse());
73+
Response response = ResourceRecipientLists.create(conn, maxNumberOfRecipientErrors, recipientList);
74+
Assert.assertNotNull(response);
75+
76+
Assert.assertEquals(conn.getPath(), "/recipient-lists?num_rcpt_errors=0");
77+
verifyWasPost(conn);
78+
}
79+
80+
@Test
81+
public void testRetrieve() throws SparkPostException {
82+
String recipientListId = "recipientListId";
83+
84+
StubRestConnection conn = buildStubConnection(new StubRecipientListRetrieveResponse());
85+
Response response = ResourceRecipientLists.retrieve(conn, recipientListId, Boolean.TRUE);
86+
Assert.assertNotNull(response);
87+
88+
Assert.assertEquals(conn.getPath(), "/recipient-lists/recipientListId?show_recipients=true");
89+
verifyWasGet(conn);
90+
}
91+
92+
@Test
93+
public void testListAll() throws SparkPostException {
94+
StubRestConnection conn = buildStubConnection(new StubRecipientListsListAllResponse());
95+
Response response = ResourceRecipientLists.listAll(conn);
96+
Assert.assertNotNull(response);
97+
98+
Assert.assertEquals(conn.getPath(), "/recipient-lists");
99+
verifyWasGet(conn);
100+
}
101+
102+
@Test
103+
public void testDelete() throws SparkPostException {
104+
String recipientListId = "recipientListId";
105+
106+
StubRestConnection conn = buildStubConnection(new StubRecipientListsListAllResponse());
107+
Response response = ResourceRecipientLists.delete(conn, recipientListId);
108+
Assert.assertNotNull(response);
109+
110+
Assert.assertEquals(conn.getPath(), "/recipient-lists/recipientListId");
111+
verifyWasDelete(conn);
112+
}
113+
114+
}

0 commit comments

Comments
 (0)