From 2229d5048ecf5104024b51563a107f465d140f22 Mon Sep 17 00:00:00 2001 From: Eliezio Oliveira Date: Thu, 30 May 2019 08:20:24 +0100 Subject: Re-enabled RestClientServiceTest Secondary HTTP server switched from blocking Tomcat to reactive Netty Change-Id: I44ae3e4d1f4f1af1335a94032f236a1023dbdea0 Issue-ID: CCSDK-1447 Signed-off-by: Eliezio Oliveira --- .../modules/commons/rest-lib/pom.xml | 10 +- .../rest/service/RestClientServiceTest.kt | 107 ++++++++++----------- 2 files changed, 59 insertions(+), 58 deletions(-) (limited to 'ms/blueprintsprocessor/modules') diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/pom.xml b/ms/blueprintsprocessor/modules/commons/rest-lib/pom.xml index 0cae94690..011edae95 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/pom.xml +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/pom.xml @@ -44,7 +44,15 @@ org.springframework.boot - spring-boot-starter-web + spring-boot-starter + + + org.springframework + spring-web + + + org.springframework.boot + spring-boot-starter-webflux test diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt index 21d05e620..e6a66800b 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/RestClientServiceTest.kt @@ -23,8 +23,8 @@ import com.fasterxml.jackson.databind.ObjectMapper import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking -import org.apache.catalina.connector.Connector -import org.junit.Ignore +import org.junit.After +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties @@ -34,18 +34,17 @@ import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.EnableAutoConfiguration import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory -import org.springframework.boot.web.servlet.server.ServletWebServerFactory +import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory +import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory +import org.springframework.boot.web.server.WebServer import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration import org.springframework.http.HttpMethod -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder -import org.springframework.security.config.annotation.web.builders.HttpSecurity -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder -import org.springframework.security.crypto.password.PasswordEncoder -import org.springframework.stereotype.Component +import org.springframework.http.server.reactive.HttpHandler +import org.springframework.security.config.web.server.ServerHttpSecurity +import org.springframework.security.core.userdetails.MapReactiveUserDetailsService +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.web.server.SecurityWebFilterChain import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringRunner @@ -63,10 +62,9 @@ import kotlin.test.assertNotNull @RunWith(SpringRunner::class) @EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class]) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -@ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, - BlueprintPropertyConfiguration::class, - SampleController::class, BluePrintProperties::class, - BluePrintProperties::class]) +@ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, SampleController::class, + SecurityConfiguration::class, + BlueprintPropertyConfiguration::class, BluePrintProperties::class]) @TestPropertySource(properties = [ "server.port=8443", @@ -76,7 +74,7 @@ import kotlin.test.assertNotNull "server.ssl.keyStoreType=PKCS12", "server.ssl.keyAlias=tomcat", "blueprintsprocessor.restclient.sample.type=basic-auth", - "blueprintsprocessor.restclient.sample.url=http://127.0.0.1:8080", + "blueprintsprocessor.restclient.sample.url=http://127.0.0.1:8081", "blueprintsprocessor.restclient.sample.username=admin", "blueprintsprocessor.restclient.sample.password=jans", "blueprintsprocessor.restclient.test.type=ssl-basic-auth", @@ -87,12 +85,31 @@ import kotlin.test.assertNotNull "blueprintsprocessor.restclient.test.sslTrust=src/test/resources/keystore.p12", "blueprintsprocessor.restclient.test.sslTrustPassword=changeit" ]) -@Ignore class RestClientServiceTest { @Autowired lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService + @Autowired + lateinit var httpHandler : HttpHandler + + lateinit var http : WebServer + + fun localPort() = http.port + + @Before + fun start() { + // Second Http server required for non-SSL requests to be processed along with the https server. + val factory: ReactiveWebServerFactory = NettyReactiveWebServerFactory(8081) + this.http = factory.getWebServer(this.httpHandler) + this.http.start() + } + + @After + fun stop() { + this.http.stop() + } + @Test fun testPatch() { val restClientService = bluePrintRestLibPropertyService @@ -118,7 +135,7 @@ class RestClientServiceTest { fun testSimpleBasicAuth() { val json: String = "{\n" + " \"type\" : \"basic-auth\",\n" + - " \"url\" : \"http://localhost:8080\",\n" + + " \"url\" : \"http://localhost:8081\",\n" + " \"username\" : \"admin\",\n" + " \"password\" : \"jans\"\n" + "}" @@ -308,49 +325,25 @@ open class SampleController { * Security configuration required for basic authentication with username and * password for any request in the server. */ -@Configuration -@EnableWebSecurity -open class SecurityConfig : WebSecurityConfigurerAdapter() { - - @Throws(Exception::class) - override fun configure(http: HttpSecurity) { - http - .csrf().disable() - .authorizeRequests().anyRequest().authenticated() - .and() - .httpBasic() - } +open class SecurityConfiguration { - @Autowired - @Throws(Exception::class) - open fun configureGlobal(auth: AuthenticationManagerBuilder) { - auth.inMemoryAuthentication() - .withUser("admin") - .password(passwordEncoder().encode("jans")) + @Bean + open fun userDetailsService(): MapReactiveUserDetailsService { + val user: UserDetails = User.withDefaultPasswordEncoder() + .username("admin") + .password("jans") .roles("USER") + .build() + return MapReactiveUserDetailsService(user) } @Bean - open fun passwordEncoder(): PasswordEncoder { - return BCryptPasswordEncoder() - } -} - -/** - * Http server required for http request to be processed along with the https - * server. - */ -@Component -class HttpServer { - @Bean - fun servletContainer(): ServletWebServerFactory { - - val connector = Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL) - connector.port = 8080 - - val tomcat = TomcatServletWebServerFactory() - tomcat.addAdditionalTomcatConnectors(connector) - return tomcat + open fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain { + return http + .csrf().disable() + .authorizeExchange().anyExchange().authenticated() + .and().httpBasic() + .and().build() } } -- cgit 1.2.3-korg