PageRenderTime 31ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/pentaho-pdi-platform/src/test/java/org/pentaho/platform/pdi/WebContextServletTest.java

https://gitlab.com/lgrill/pentaho-osgi-bundles
Java | 259 lines | 184 code | 57 blank | 18 comment | 4 complexity | 5829437cacf4d9689b28d32c8893f97d MD5 | raw file
  1. /*!
  2. * Copyright 2010 - 2018 Hitachi Vantara. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.pentaho.platform.pdi;
  18. import org.apache.commons.lang.StringEscapeUtils;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.mockito.runners.MockitoJUnitRunner;
  23. import org.pentaho.platform.api.engine.IPlatformWebResource;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.ServletOutputStream;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. import java.io.ByteArrayOutputStream;
  29. import java.io.IOException;
  30. import java.io.PrintWriter;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import static org.junit.Assert.assertEquals;
  34. import static org.junit.Assert.assertNotNull;
  35. import static org.junit.Assert.assertTrue;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.when;
  38. import static org.mockito.Mockito.spy;
  39. import static org.mockito.Mockito.doReturn;
  40. @RunWith( MockitoJUnitRunner.class )
  41. public class WebContextServletTest {
  42. private WebContextServlet webContextServlet;
  43. private IPlatformWebResource jsFile;
  44. private IPlatformWebResource txtFile;
  45. private HttpServletRequest httpRequest;
  46. private HttpServletResponse httpResponse;
  47. private ByteArrayOutputStream mockResponseOutputStream;
  48. @Before
  49. public void setUp() throws Exception {
  50. webContextServlet = spy( new WebContextServlet() );
  51. jsFile = new PlatformWebResource( "analyzer", "scripts/includeMe.js" );
  52. txtFile = new PlatformWebResource( "analyzer", "scripts/includeMe.txt" );
  53. HttpServletRequest mockRequest = mock( HttpServletRequest.class );
  54. when( mockRequest.getRequestURI() ).thenReturn( "fake/uri/" + WebContextServlet.WEB_CONTEXT_JS );
  55. when( mockRequest.getParameter( WebContextServlet.CONTEXT ) ).thenReturn( "testContext" );
  56. when( mockRequest.getParameter( WebContextServlet.APPLICATION ) ).thenReturn( "testApplication" );
  57. when( mockRequest.getParameter( WebContextServlet.LOCALE ) ).thenReturn( "xp_TO" );
  58. this.httpRequest = mockRequest;
  59. HttpServletResponse mockResponse = mock( HttpServletResponse.class );
  60. this.mockResponseOutputStream = new java.io.ByteArrayOutputStream();
  61. when( mockResponse.getOutputStream() ).thenReturn( new ServletOutputStream() {
  62. @Override
  63. public void write( int b ) throws IOException {
  64. WebContextServletTest.this.mockResponseOutputStream.write( b );
  65. }
  66. } );
  67. this.httpResponse = mockResponse;
  68. }
  69. @Test
  70. public void testGetWebResources_NoMatches() throws Exception {
  71. List<String> webResources = webContextServlet.getWebResources( "analyzer", ".*\\.js" );
  72. assertNotNull( webResources );
  73. assertEquals( 0, webResources.size() );
  74. }
  75. @Test
  76. public void testGetWebResources_Match() throws Exception {
  77. webContextServlet.addPlatformWebResource( jsFile );
  78. webContextServlet.addPlatformWebResource( txtFile );
  79. List<String> webResources = webContextServlet.getWebResources( "analyzer", ".*\\.js" );
  80. assertNotNull( webResources );
  81. assertEquals( 1, webResources.size() );
  82. assertEquals( "scripts/includeMe.js", webResources.get( 0 ) );
  83. }
  84. @Test
  85. public void testWriteWebResourcesJSToDoc() throws Exception {
  86. List<String> resources = new ArrayList<>();
  87. resources.add( "scripts/includeMe.js" );
  88. resources.add( "scripts/includeMeToo.js" );
  89. PrintWriter writer = new PrintWriter( this.mockResponseOutputStream );
  90. this.webContextServlet.writeWebResources( writer, resources );
  91. String response = getServletResponse( writer );
  92. resources.forEach( resource -> {
  93. String expected = getDocumentWriteExpected( resource );
  94. assertTrue( response.contains( expected ) );
  95. } );
  96. }
  97. @Test
  98. public void testWriteWebResourcesCssToDoc() throws Exception {
  99. List<String> resources = new ArrayList<>();
  100. resources.add( "styles/awesome.css" );
  101. PrintWriter writer = new PrintWriter( this.mockResponseOutputStream );
  102. this.webContextServlet.writeWebResources( writer, resources );
  103. String response = getServletResponse( writer );
  104. resources.forEach( resource -> {
  105. String expected = getDocumentWriteExpected( resource );
  106. assertTrue( response.contains( expected ) );
  107. } );
  108. }
  109. @Test
  110. public void testWebContextDefinesContextPath() throws ServletException, IOException {
  111. final String response = doGetWebContextServlet();
  112. String contextPath = WebContextServlet.CONTEXT_PATH;
  113. assertTrue( response.contains( getWebContextVarDefinition( "CONTEXT_PATH", contextPath ) ) );
  114. }
  115. @Test
  116. public void testWebContextDefinesSessionLocale() throws ServletException, IOException {
  117. String sessionLocale = "fo_BA";
  118. when( this.httpRequest.getParameter( "locale" ) ).thenReturn( sessionLocale );
  119. final String response = doGetWebContextServlet();
  120. assertTrue( response.contains( getWebContextVarDefinition( "SESSION_LOCALE", sessionLocale ) ) );
  121. }
  122. @Test
  123. public void testDoGetDefinesRequireCfg() throws ServletException, IOException {
  124. Integer waitTime = 1337;
  125. doReturn( waitTime ).when( this.webContextServlet ).getRequireWaitTime();
  126. String response = doGetWebContextServlet();
  127. String expected = "var requireCfg = {" +
  128. "\n waitSeconds: " + waitTime + "," +
  129. "\n paths: {}," +
  130. "\n shim: {}," +
  131. "\n map: { \"*\": {} }," +
  132. "\n bundles: {}," +
  133. "\n config: { \"pentaho/modules\": {} }," +
  134. "\n packages: []" +
  135. "\n}";
  136. assertTrue( response.contains( expected ) );
  137. }
  138. @Test
  139. public void testWebContextDefinesPentahoEnvironmentModuleConfig() throws ServletException, IOException {
  140. String mockRoot = "/root/";
  141. doReturn( mockRoot ).when( this.webContextServlet ).getServerRoot();
  142. String mockServerPackages = mockRoot + "osgi/";
  143. doReturn( mockServerPackages ).when( this.webContextServlet ).getServerPackages();
  144. String mockServices = mockRoot + "services/";
  145. doReturn( mockServices ).when( this.webContextServlet ).getServerServices();
  146. String serverRoot = escapeEnvironmentVariable( mockRoot );
  147. String serverPackages = escapeEnvironmentVariable( mockServerPackages );
  148. String serverServices = escapeEnvironmentVariable( mockServices );
  149. String sessionLocale = "fo_BA";
  150. when( this.httpRequest.getParameter( "locale" ) ).thenReturn( sessionLocale );
  151. String application = "testApplication";
  152. when( this.httpRequest.getParameter( "application" ) ).thenReturn( application );
  153. final String response = doGetWebContextServlet();
  154. String environmentModuleConfig = "\nrequireCfg.config[\"pentaho/environment\"] = {" +
  155. "\n application: \"" + application + "\"," +
  156. "\n theme: null," +
  157. "\n locale: \"" + sessionLocale + "\"," +
  158. "\n user: {" +
  159. "\n id: null," +
  160. "\n home: null" +
  161. "\n }," +
  162. "\n server: {" +
  163. "\n root: " + serverRoot + "," +
  164. "\n packages: " + serverPackages + "," +
  165. "\n services: " + serverServices +
  166. "\n }," +
  167. "\n reservedChars: null" +
  168. "\n}";
  169. assertTrue( response.contains( environmentModuleConfig ) );
  170. }
  171. // region Auxiliary Methods
  172. private String doGetWebContextServlet() throws ServletException, IOException {
  173. this.webContextServlet.doGet( this.httpRequest, this.httpResponse );
  174. return getServletResponse();
  175. }
  176. private String getServletResponse() throws IOException {
  177. return getServletResponse( null );
  178. }
  179. private String getServletResponse( PrintWriter writer ) throws IOException {
  180. if ( writer != null ) {
  181. writer.flush();
  182. }
  183. return this.mockResponseOutputStream.toString( "UTF-8" );
  184. }
  185. private String getWebContextVarDefinition( String variable, String value ) {
  186. String escapedValue = escapeEnvironmentVariable( value );
  187. return "\n/** @deprecated - use 'pentaho/environment' module's variable instead */" +
  188. "\nvar " + variable + " = " + escapedValue + ";";
  189. }
  190. private String getDocumentWriteExpected( String resource ) {
  191. String location = "'\" + CONTEXT_PATH + \"" + resource + "'";
  192. if ( resource.endsWith( ".js" ) ) {
  193. return "document.write(\"<script type='text/javascript' src=" + location + "></scr\" + \"ipt>\");\n";
  194. } else {
  195. return "document.write(\"<link rel='stylesheet' type='text/css' href=" + location + ">\");\n";
  196. }
  197. }
  198. private String escapeEnvironmentVariable( String value ) {
  199. return "\"" + StringEscapeUtils.escapeJavaScript( value ) + "\"";
  200. }
  201. // endregion
  202. }