001package org.apache.turbine.services.urlmapper; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertNotNull; 023import static org.junit.jupiter.api.Assertions.assertTrue; 024import static org.mockito.Mockito.mock; 025 026import javax.servlet.ServletConfig; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029 030import org.apache.fulcrum.parser.ParameterParser; 031import org.apache.logging.log4j.LogManager; 032import org.apache.logging.log4j.Logger; 033import org.apache.turbine.Turbine; 034import org.apache.turbine.pipeline.PipelineData; 035import org.apache.turbine.services.TurbineServices; 036import org.apache.turbine.test.BaseTestCase; 037import org.apache.turbine.util.RunData; 038import org.apache.turbine.util.TurbineConfig; 039import org.apache.turbine.util.uri.TemplateURI; 040import org.junit.jupiter.api.AfterAll; 041import org.junit.jupiter.api.BeforeAll; 042import org.junit.jupiter.api.BeforeEach; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045import org.mockito.Mockito; 046 047@Tag("yaml") 048public class TurbineURLMapperYAMLServiceTest extends BaseTestCase 049{ 050 051 private static TurbineConfig tc = null; 052 053 private static URLMapperService urlMapper = null; 054 055 private RunData data; 056 057 Logger log = LogManager.getLogger(); 058 059 @BeforeAll 060 public static void setUp() throws Exception 061 { 062 tc = new TurbineConfig( ".", "/conf/test/TurbineURLMapperYAMLServiceTest.properties" ); 063 tc.initialize(); 064 065 urlMapper = (URLMapperService) TurbineServices.getInstance().getService( URLMapperService.SERVICE_NAME ); 066 } 067 068 @AfterAll 069 public static void tearDown() throws Exception 070 { 071 if (tc != null) 072 { 073 tc.dispose(); 074 } 075 } 076 077 @BeforeEach 078 public void init() throws Exception 079 { 080 081 ServletConfig config = tc.getTurbine().getServletConfig(); 082 // mock(ServletConfig.class); 083 HttpServletRequest request = getMockRequest(); 084 HttpServletResponse response = mock( HttpServletResponse.class ); 085 086 data = getRunData( request, response, config ); 087 088 Mockito.when( response.encodeURL( Mockito.anyString() ) ) 089 .thenAnswer( invocation -> invocation.getArgument( 0 ) ); 090 } 091 092 @Test 093 public void testMapToAnotherURL() throws Exception 094 { 095 096 PipelineData pipelineData = data; 097 098 assertNotNull( urlMapper ); 099 100 TemplateURI uri = new TemplateURI( pipelineData.getRunData() ); 101 uri.addPathInfo( "id", 1234 ); 102 uri.addPathInfo( "role", "guest" ); 103 uri.addPathInfo( "language", "de" ); 104 105 String unMappedURL = uri.getAbsoluteLink(); // scheme://bob/wow/damn2/id/1234/role/guest 106 log.info( unMappedURL ); 107 108 String expectedRawURL = "scheme://bob/wow/damn2/id/1234/role/guest/language/de"; 109 urlMapper.mapToURL( uri ); 110 urlMapper.mapToURL( uri ); // should be idempotent 111 // raw url 112 assertEquals( expectedRawURL, unMappedURL ); 113 114 String mappedLink = uri.getRelativeLink(); // wow/damn2/id/1234/role/guest 115 log.info( mappedLink ); 116 String expectedMappedURL = "/wow/1234/guest/de"; 117 assertEquals( expectedMappedURL, mappedLink ); 118 119 ParameterParser pp = pipelineData.get( Turbine.class, ParameterParser.class ); 120 assertNotNull( pp ); 121 assertTrue( pp.keySet().isEmpty() ); 122 urlMapper.mapFromURL( mappedLink, pp ); 123 124 assertEquals( 5, pp.keySet().size() ); 125 assertEquals( 1234, pp.getInt( "id" ) ); 126 assertEquals( "guest", pp.getString( "role" ) ); 127 assertEquals( "de", pp.getString( "language" ) ); 128 assertEquals( "html", pp.getString( "media-type" ) ); 129 130 TemplateURI uri2 = new TemplateURI( pipelineData.getRunData() ); 131 uri2.clearResponse(); 132 uri2.setTemplate( "default.vm" ); 133 uri2.addPathInfo( pp ); 134 // this is an artifical url 135 assertEquals( "scheme://bob/wow/damn2/template/default.vm/media-type/html/role/guest/id/1234/language/de", 136 uri2.getAbsoluteLink() ); 137 urlMapper.mapToURL( uri2 ); 138 assertEquals( expectedMappedURL, uri2.getRelativeLink() ); 139 } 140 141 @Test 142 public void testOverrideShortURL() throws Exception 143 { 144 145 PipelineData pipelineData = data; 146 147 assertNotNull( urlMapper ); 148 149 ParameterParser pp = pipelineData.get( Turbine.class, ParameterParser.class ); 150 assertNotNull( pp ); 151 assertTrue( pp.keySet().isEmpty() ); 152 153 pp.add( "role", "admin" ); // will not be overridden 154 urlMapper.mapFromURL( "/app/register", pp ); 155 156 log.info( "parameters: {}", pp ); 157 assertEquals( 4, pp.keySet().size() ); 158 assertEquals( "random-id-123-abc", pp.getString( "js_pane" ) ); 159 assertEquals( "admin", pp.getString( "role" ) ); 160 assertEquals( "html", pp.getString( "media-type" ) ); 161 assertEquals( "Registerone.vm", pp.getString( "template" ) ); 162 163 TemplateURI uri2 = new TemplateURI( pipelineData.getRunData() ); 164 uri2.clearResponse(); 165 uri2.setTemplate( "Registerone.vm" ); 166 pp.remove( "role" ); 167 pp.add( "role", "anon" ); 168 uri2.addPathInfo( pp ); 169 170 // this is an artifical url, as the exact sequence could not be reconstructed as 171 // ParameterParser uses expicitely a random access table 172 assertEquals( 173 "scheme://bob/wow/damn2/template/Registerone.vm/media-type/html/js_pane/random-id-123-abc/role/anon", 174 uri2.getAbsoluteLink() ); 175 urlMapper.mapToURL( uri2 ); 176 String expectedMappedURL = "/wow/damn2/register"; 177 assertEquals( expectedMappedURL, uri2.getRelativeLink() ); 178 179 pp.clear(); 180 pp.add( "role", "admin" );// will be overridden 181 urlMapper.mapFromURL( "/app/contact", pp ); 182 log.info( "parameters: {}", pp ); 183 assertEquals( 4, pp.keySet().size() ); 184 assertEquals( "anon", pp.getString( "role" ) ); 185 assertEquals( "another-random-id-876-dfg", pp.getString( "js_pane" ) ); 186 187 uri2 = new TemplateURI( pipelineData.getRunData() ); 188 uri2.clearResponse(); 189 uri2.addPathInfo( pp ); 190 191 // this is an artifical url 192 assertEquals( "scheme://bob/wow/damn2/page/Contact/media-type/html/js_pane/another-random-id-876-dfg/role/anon", 193 uri2.getAbsoluteLink() ); 194 urlMapper.mapToURL( uri2 ); 195 expectedMappedURL = "/wow/damn2/contact"; 196 assertEquals( expectedMappedURL, uri2.getRelativeLink() ); 197 198 } 199 200// /** 201// * Not implemented Test for MappedTemplateLink: 202// * - To work with <i>MappedTemplateLink</i>, we need access to the urlmapperservice in order to 203// * - simulate a request without pipeline (setting velocity context and initializing the service): 204// */ 205// @Test 206// public void testMappedURILink() { 207// MappedTemplateLink ml = MappedTemplateLink.class.getDeclaredConstructor().newInstance(); 208// assertNotNull(ml); 209// ml.setUrlMapperService(urlMapper); 210// ml.init(data); 211// } 212 213}