|
| 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.TemplateAttributes; |
| 17 | +import com.sparkpost.model.TemplateSubstitutionData; |
| 18 | +import com.sparkpost.model.responses.Response; |
| 19 | +import com.sparkpost.model.responses.TemplateCreateResponse; |
| 20 | +import com.sparkpost.model.responses.TemplateListResponse; |
| 21 | +import com.sparkpost.model.responses.TemplatePreviewResponse; |
| 22 | +import com.sparkpost.model.responses.TemplateRetrieveResponse; |
| 23 | +import com.sparkpost.testhelpers.StubRestConnection; |
| 24 | + |
| 25 | +public class ResourceTemplatesTests extends BaseResourceTest { |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void setUpClass() { |
| 29 | + Logger.getRootLogger().setLevel(Level.DEBUG); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterClass |
| 33 | + public static void tearDownClass() { |
| 34 | + } |
| 35 | + |
| 36 | + @Before |
| 37 | + public void setUp() { |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void tearDown() { |
| 42 | + } |
| 43 | + |
| 44 | + private static final class StubResponse extends Response { |
| 45 | + |
| 46 | + public static Response decode(Response response, Type typeOfT) { |
| 47 | + return new StubResponse(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static final class StubTemplateCreateResponse extends TemplateCreateResponse { |
| 52 | + |
| 53 | + public static StubTemplateCreateResponse decode(Response response, Type typeOfT) { |
| 54 | + return new StubTemplateCreateResponse(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static final class StubTemplateListResponse extends TemplateListResponse { |
| 59 | + |
| 60 | + public static StubTemplateListResponse decode(Response response, Type typeOfT) { |
| 61 | + return new StubTemplateListResponse(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static final class StubTemplatePreviewResponse extends TemplatePreviewResponse { |
| 66 | + |
| 67 | + public static StubTemplatePreviewResponse decode(Response response, Type typeOfT) { |
| 68 | + return new StubTemplatePreviewResponse(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static final class StubStubTemplateListResponse extends TemplateRetrieveResponse { |
| 73 | + |
| 74 | + public static TemplateRetrieveResponse decode(Response response, Type typeOfT) { |
| 75 | + return new TemplateRetrieveResponse(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private StubRestConnection buildStubConnection(Response response) { |
| 80 | + StubRestConnection conn = new StubRestConnection(response); |
| 81 | + return conn; |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testInsertOrUpdate() throws SparkPostException { |
| 86 | + TemplateAttributes tpl = new TemplateAttributes(); |
| 87 | + |
| 88 | + StubRestConnection conn = buildStubConnection(new StubTemplateCreateResponse()); |
| 89 | + Response response = ResourceTemplates.create(conn, tpl); |
| 90 | + Assert.assertNotNull(response); |
| 91 | + |
| 92 | + Assert.assertEquals(conn.getPath(), "/templates"); |
| 93 | + verifyWasPost(conn); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testRetrieve() throws SparkPostException { |
| 98 | + String id = "id"; |
| 99 | + Boolean draft = Boolean.TRUE; |
| 100 | + |
| 101 | + StubRestConnection conn = buildStubConnection(new StubStubTemplateListResponse()); |
| 102 | + Response response = ResourceTemplates.retrieve(conn, id, draft); |
| 103 | + Assert.assertNotNull(response); |
| 104 | + |
| 105 | + Assert.assertEquals(conn.getPath(), "/templates/id?draft=true"); |
| 106 | + verifyWasGet(conn); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testListAll() throws SparkPostException { |
| 111 | + |
| 112 | + StubRestConnection conn = buildStubConnection(new StubTemplateListResponse()); |
| 113 | + Response response = ResourceTemplates.listAll(conn); |
| 114 | + Assert.assertNotNull(response); |
| 115 | + |
| 116 | + Assert.assertEquals(conn.getPath(), "/templates/"); |
| 117 | + verifyWasGet(conn); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void tesUpdate() throws SparkPostException { |
| 122 | + String id = "id"; |
| 123 | + Boolean updatePublished = Boolean.TRUE; |
| 124 | + TemplateAttributes tpl = new TemplateAttributes(); |
| 125 | + |
| 126 | + StubRestConnection conn = buildStubConnection(new StubResponse()); |
| 127 | + Response response = ResourceTemplates.update(conn, id, updatePublished, tpl); |
| 128 | + Assert.assertNotNull(response); |
| 129 | + |
| 130 | + Assert.assertEquals(conn.getPath(), "/templates/id?update_published=true"); |
| 131 | + verifyWasPut(conn); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void tesPreview() throws SparkPostException { |
| 136 | + String id = "id"; |
| 137 | + Boolean draft = Boolean.TRUE; |
| 138 | + TemplateSubstitutionData subst = new TemplateSubstitutionData(); |
| 139 | + |
| 140 | + StubRestConnection conn = buildStubConnection(new StubTemplatePreviewResponse()); |
| 141 | + Response response = ResourceTemplates.preview(conn, id, draft, subst); |
| 142 | + Assert.assertNotNull(response); |
| 143 | + |
| 144 | + Assert.assertEquals(conn.getPath(), "/templates/" + id + "/preview?draft=true"); |
| 145 | + verifyWasPost(conn); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void tesDelete() throws SparkPostException { |
| 150 | + String id = "id"; |
| 151 | + |
| 152 | + StubRestConnection conn = buildStubConnection(new StubResponse()); |
| 153 | + Response response = ResourceTemplates.delete(conn, id); |
| 154 | + Assert.assertNotNull(response); |
| 155 | + |
| 156 | + System.out.println(conn.getPath()); |
| 157 | + Assert.assertEquals(conn.getPath(), "/templates/" + id); |
| 158 | + verifyWasDelete(conn); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments