View Javadoc
1   /*
2    * Copyright (c) 2013-2014, Dienst Landelijk Gebied - Ministerie van Economische Zaken
3    * 
4    * Gepubliceerd onder de BSD 2-clause licentie, zie https://github.com/MinELenI/CBSviewer/blob/master/LICENSE.md voor de volledige licentie.
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   * Testcases voor jsp's in het project.
34   * 
35   * @author mprins
36   */
37  public abstract class JSPIntegrationTest extends IntegrationTestConstants {
38  	/**
39  	 * test client.
40  	 */
41  	protected static CloseableHttpClient client;
42  
43  	private static final Logger LOGGER = LoggerFactory
44  			.getLogger(JSPIntegrationTest.class);
45  
46  	/**
47  	 * validation string.
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  	/** http client voor communicatie met de validator. */
53  	private static CloseableHttpClient validatorclient;
54  
55  	/**
56  	 * close http connecties.
57  	 * 
58  	 * @throws Exception
59  	 *             als er een fout optreed tijdens afsluiten connecties.
60  	 */
61  	@AfterClass
62  	public static void disconnect() throws Exception {
63  		client.close();
64  		validatorclient.close();
65  	}
66  
67  	/**
68  	 * init XMLUnit.
69  	 * 
70  	 * @throws Exception
71  	 */
72  	@BeforeClass
73  	public static void setUpClass() throws Exception {
74  		// XMLUnit.setIgnoreWhitespace(false);
75  		// XMLUnit.setIgnoreAttributeOrder(true);
76  		// XMLUnit.setIgnoreComments(true);
77  		// XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
78  
79  		client = HttpClientBuilder.create().build();
80  		validatorclient = HttpClients.createSystem();
81  	}
82  
83  	/**
84  	 * test response.
85  	 */
86  	protected HttpResponse response;
87  
88  	/**
89  	 * response validatie test tegen validator.nu
90  	 * 
91  	 * @param response
92  	 *            test object
93  	 * @throws Exception
94  	 *             als er een fout optreedt tijdens de test.
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 		// online validation
106 		final HttpPost validatorrequest = new HttpPost(
107 		/* "http://html5.validator.nu/" */
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 				// .addTextBody("parser", "html5", ContentType.DEFAULT_TEXT)
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 		// controle op succes paragraaf in valadator response
134 		assertTrue("(X)HTML is niet geldig.",
135 				validatorbody.contains("<p class=\"success\">"));
136 	}
137 
138 	/**
139 	 * http verbindingen sluiten na afloop testcases.
140 	 * 
141 	 * @throws Exception
142 	 *             als er een fout optreedt bij het afsluiten.
143 	 */
144 	@After
145 	public void closeConnection() throws Exception {
146 
147 	}
148 
149 	/**
150 	 * voorbereidingen voor testcases.
151 	 */
152 	@Before
153 	public void prepareTestCase() {
154 
155 	}
156 
157 	/**
158 	 * test of de response geldig is.
159 	 * 
160 	 * @throws Exception
161 	 *             als er een fout optreedt tijdens de test.
162 	 */
163 	public abstract void testIfValidResponse() throws Exception;
164 }