You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractRestServicesTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package org.apache.archiva.web;/*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. import com.fasterxml.jackson.databind.ObjectMapper;
  19. import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
  20. import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
  21. import junit.framework.TestCase;
  22. import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
  23. import org.apache.archiva.redback.rest.api.model.User;
  24. import org.apache.archiva.redback.rest.api.services.LdapGroupMappingService;
  25. import org.apache.archiva.redback.rest.api.services.LoginService;
  26. import org.apache.archiva.redback.rest.api.services.RoleManagementService;
  27. import org.apache.archiva.redback.rest.api.services.UserService;
  28. import org.apache.archiva.redback.rest.api.services.v2.AuthenticationService;
  29. import org.apache.archiva.redback.rest.services.BaseSetup;
  30. import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
  31. import org.apache.commons.lang3.SystemUtils;
  32. import org.apache.cxf.common.util.Base64Utility;
  33. import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
  34. import org.apache.cxf.jaxrs.client.WebClient;
  35. import org.apache.cxf.transport.servlet.CXFServlet;
  36. import org.eclipse.jetty.server.HttpConnectionFactory;
  37. import org.eclipse.jetty.server.Server;
  38. import org.eclipse.jetty.server.ServerConnector;
  39. import org.eclipse.jetty.server.session.SessionHandler;
  40. import org.eclipse.jetty.servlet.ServletContextHandler;
  41. import org.eclipse.jetty.servlet.ServletHolder;
  42. import org.junit.After;
  43. import org.junit.Before;
  44. import org.junit.runner.RunWith;
  45. import org.junit.runners.JUnit4;
  46. import org.slf4j.Logger;
  47. import org.slf4j.LoggerFactory;
  48. import org.springframework.web.context.ContextLoaderListener;
  49. import javax.ws.rs.core.MediaType;
  50. import java.util.Collections;
  51. import java.util.concurrent.atomic.AtomicReference;
  52. /**
  53. * @author Olivier Lamy
  54. */
  55. @RunWith(JUnit4.class)
  56. public abstract class AbstractRestServicesTest
  57. extends TestCase
  58. {
  59. protected Logger log = LoggerFactory.getLogger( getClass() );
  60. private static AtomicReference<Server> server = new AtomicReference<>();
  61. private static AtomicReference<ServerConnector> serverConnector = new AtomicReference<>();
  62. public String authorizationHeader = getAdminAuthzHeader();
  63. /**
  64. * Returns the server that was started, or null if not initialized before.
  65. * @return
  66. */
  67. public Server getServer() {
  68. return this.server.get();
  69. }
  70. public int getServerPort() {
  71. ServerConnector connector = serverConnector.get();
  72. if (connector!=null) {
  73. return connector.getLocalPort();
  74. } else {
  75. return 0;
  76. }
  77. }
  78. JacksonJaxbJsonProvider getJsonProvider() {
  79. JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider( );
  80. ObjectMapper mapper = new ObjectMapper( );
  81. mapper.registerModule( new JavaTimeModule( ) );
  82. provider.setMapper( mapper );
  83. return provider;
  84. }
  85. /**
  86. * Returns true, if the server does exist and is running.
  87. * @return true, if server does exist and is running.
  88. */
  89. public boolean isServerRunning() {
  90. return this.server.get() != null && this.server.get().isRunning();
  91. }
  92. /**
  93. * Returns the timeout in ms for rest requests. The timeout can be set by
  94. * the system property <code>rest.test.timeout</code>.
  95. * @return The timeout value in ms.
  96. */
  97. public long getTimeout()
  98. {
  99. return Long.getLong( "rest.test.timeout", 1000000 );
  100. }
  101. public static String encode( String uid, String password )
  102. {
  103. return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
  104. }
  105. public static String getAdminAuthzHeader()
  106. {
  107. return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, BaseSetup.getAdminPwd() );
  108. }
  109. protected String getSpringConfigLocation()
  110. {
  111. return "classpath*:spring-context.xml,classpath*:META-INF/spring-context.xml";
  112. }
  113. protected String getRestServicesPath()
  114. {
  115. return "restServices";
  116. }
  117. @Before
  118. public void startServer()
  119. throws Exception
  120. {
  121. log.info("Starting server");
  122. log.info( "User config {}", System.getProperty( "archiva.user.configFileName" ) );
  123. log.info( "Appserver base {}", System.getProperty( "appserver.base" ) );
  124. Server myServer = new Server();
  125. this.server.set(myServer);
  126. this.serverConnector.set(new ServerConnector( myServer, new HttpConnectionFactory()));
  127. myServer.addConnector(serverConnector.get());
  128. ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
  129. ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  130. context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
  131. context.setSessionHandler( new SessionHandler( ) );
  132. context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
  133. context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
  134. context.addEventListener(new ContextLoaderListener());
  135. getServer().setHandler( context );
  136. getServer().start();
  137. if (log.isDebugEnabled())
  138. {
  139. log.debug( "Jetty dump: {}", getServer().dump() );
  140. }
  141. log.info( "Started server on port {}", getServerPort() );
  142. UserService userService = getUserService();
  143. User adminUser = new User();
  144. adminUser.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
  145. adminUser.setPassword( BaseSetup.getAdminPwd() );
  146. adminUser.setFullName( "the admin user" );
  147. adminUser.setEmail( "toto@toto.fr" );
  148. if( !userService.createAdminUser( adminUser ) ) {
  149. log.info( "Could not create admin user." );
  150. }
  151. FakeCreateAdminService fakeCreateAdminService = getFakeCreateAdminService();
  152. //assertTrue( res.booleanValue() );
  153. }
  154. protected FakeCreateAdminService getFakeCreateAdminService()
  155. {
  156. return JAXRSClientFactory.create(
  157. "http://localhost:" + getServerPort()+ "/" + getRestServicesPath() + "/fakeCreateAdminService/",
  158. FakeCreateAdminService.class, Collections.singletonList( getJsonProvider() ) );
  159. }
  160. @After
  161. public void stopServer()
  162. throws Exception
  163. {
  164. if ( getServer() != null )
  165. {
  166. log.info("Stopping server");
  167. getServer().stop();
  168. }
  169. }
  170. protected UserService getUserService()
  171. {
  172. return getUserService( null );
  173. }
  174. // START SNIPPET: get-user-service
  175. protected UserService getUserService( String authzHeader )
  176. {
  177. UserService service =
  178. JAXRSClientFactory.create( "http://localhost:" + getServerPort() + "/" + getRestServicesPath() + "/redbackServices/",
  179. UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  180. // time out for debuging purpose
  181. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( getTimeout() );
  182. if ( authzHeader != null )
  183. {
  184. WebClient.client( service ).header( "Authorization", authzHeader );
  185. }
  186. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  187. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  188. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  189. return service;
  190. }
  191. // END SNIPPET: get-user-service
  192. protected RoleManagementService getRoleManagementService( String authzHeader )
  193. {
  194. RoleManagementService service =
  195. JAXRSClientFactory.create( "http://localhost:" + getServerPort() + "/" + getRestServicesPath() + "/redbackServices/",
  196. RoleManagementService.class,
  197. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  198. // for debuging purpose
  199. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( getTimeout() );
  200. if ( authzHeader != null )
  201. {
  202. WebClient.client( service ).header( "Authorization", authzHeader );
  203. }
  204. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  205. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  206. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  207. return service;
  208. }
  209. protected LoginService getLoginService( String authzHeader )
  210. {
  211. LoginService service =
  212. JAXRSClientFactory.create( "http://localhost:" + getServerPort() + "/" + getRestServicesPath() + "/redbackServices/",
  213. LoginService.class, Collections.singletonList( getJsonProvider() ) );
  214. // for debuging purpose
  215. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( getTimeout() );
  216. if ( authzHeader != null )
  217. {
  218. WebClient.client( service ).header( "Authorization", authzHeader );
  219. }
  220. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  221. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  222. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  223. return service;
  224. }
  225. protected AuthenticationService getLoginServiceV2( String authzHeader )
  226. {
  227. AuthenticationService service =
  228. JAXRSClientFactory.create( "http://localhost:" + getServerPort() + "/" + getRestServicesPath() + "/v2/redback/",
  229. AuthenticationService.class, Collections.singletonList( getJsonProvider() ) );
  230. // for debuging purpose
  231. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( getTimeout() );
  232. if ( authzHeader != null )
  233. {
  234. WebClient.client( service ).header( "Authorization", authzHeader );
  235. }
  236. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  237. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  238. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  239. return service;
  240. }
  241. protected LdapGroupMappingService getLdapGroupMappingService( String authzHeader )
  242. {
  243. LdapGroupMappingService service =
  244. JAXRSClientFactory.create( "http://localhost:" + getServerPort() + "/" + getRestServicesPath() + "/redbackServices/",
  245. LdapGroupMappingService.class,
  246. Collections.singletonList( getJsonProvider() ) );
  247. // for debuging purpose
  248. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( getTimeout() );
  249. if ( authzHeader != null )
  250. {
  251. WebClient.client( service ).header( "Authorization", authzHeader );
  252. }
  253. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  254. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  255. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  256. return service;
  257. }
  258. }