1
2
3
4
5
6 package nl.mineleni.cbsviewer.jsp;
7
8 import static javax.servlet.http.HttpServletResponse.SC_OK;
9 import static org.hamcrest.Matchers.equalTo;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.assertTrue;
13 import nl.mineleni.cbsviewer.IntegrationTestConstants;
14
15 import org.apache.http.HttpEntity;
16 import org.apache.http.HttpResponse;
17 import org.apache.http.client.methods.HttpPost;
18 import org.apache.http.entity.ContentType;
19 import org.apache.http.entity.mime.HttpMultipartMode;
20 import org.apache.http.entity.mime.MultipartEntityBuilder;
21 import org.apache.http.impl.client.CloseableHttpClient;
22 import org.apache.http.impl.client.HttpClientBuilder;
23 import org.apache.http.impl.client.HttpClients;
24 import org.apache.http.util.EntityUtils;
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37 public abstract class JSPIntegrationTest extends IntegrationTestConstants {
38
39
40
41 protected static CloseableHttpClient client;
42
43 private static final Logger LOGGER = LoggerFactory
44 .getLogger(JSPIntegrationTest.class);
45
46
47
48
49 public static final String RESPONSEPROLOG = "<!DOCTYPE html SYSTEM \"about:legacy-compat\">"
50 + "\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"nl\" xml:lang=\"nl\">";;
51
52
53 private static CloseableHttpClient validatorclient;
54
55
56
57
58
59
60
61 @AfterClass
62 public static void disconnect() throws Exception {
63 client.close();
64 validatorclient.close();
65 }
66
67
68
69
70
71
72 @BeforeClass
73 public static void setUpClass() throws Exception {
74
75
76
77
78
79 client = HttpClientBuilder.create().build();
80 validatorclient = HttpClients.createSystem();
81 }
82
83
84
85
86 protected HttpResponse response;
87
88
89
90
91
92
93
94
95
96 protected void boilerplateValidationTests(final HttpResponse response)
97 throws Exception {
98
99 final String body = new String(EntityUtils.toByteArray(response
100 .getEntity()), "UTF-8");
101 assertNotNull("De response body mag geen null zijn.", body);
102 assertTrue("Response body dient met juiste prolog te starten.",
103 body.startsWith(RESPONSEPROLOG));
104
105
106 final HttpPost validatorrequest = new HttpPost(
107
108 "https://validator.nu/");
109 final HttpEntity entity = MultipartEntityBuilder
110 .create()
111 .setMode(HttpMultipartMode.STRICT)
112 .addTextBody("content", body, ContentType.APPLICATION_XHTML_XML)
113 .addTextBody(
114 "schema",
115 "http://s.validator.nu/xhtml5-rdfalite.rnc http://s.validator.nu/html5/assertions.sch http://c.validator.nu/all/",
116 ContentType.DEFAULT_TEXT)
117 .addTextBody("level", "error", ContentType.DEFAULT_TEXT)
118 .addTextBody("parser", "xml", ContentType.DEFAULT_TEXT)
119
120 .addTextBody("out", "json", ContentType.DEFAULT_TEXT).build();
121 validatorrequest.setEntity(entity);
122 final HttpResponse validatorresponse = validatorclient
123 .execute(validatorrequest);
124
125 assertThat("Validator response code.",
126 Integer.valueOf(validatorresponse.getStatusLine()
127 .getStatusCode()), equalTo(SC_OK));
128
129 final String validatorbody = new String(
130 EntityUtils.toByteArray(validatorresponse.getEntity()), "UTF-8");
131 LOGGER.debug("validator body:\n" + validatorbody);
132
133
134 assertTrue("(X)HTML is niet geldig.",
135 validatorbody.contains("<p class=\"success\">"));
136 }
137
138
139
140
141
142
143
144 @After
145 public void closeConnection() throws Exception {
146
147 }
148
149
150
151
152 @Before
153 public void prepareTestCase() {
154
155 }
156
157
158
159
160
161
162
163 public abstract void testIfValidResponse() throws Exception;
164 }