Convert component functions IT to UT.
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / rest-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / rest / service / RestClientServiceTest.kt
index 98cdfc0..05f3290 100644 (file)
@@ -23,49 +23,41 @@ 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.After
+import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
-import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
-import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration
+import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
+import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration
 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
-import org.springframework.web.bind.annotation.DeleteMapping
-import org.springframework.web.bind.annotation.GetMapping
-import org.springframework.web.bind.annotation.PatchMapping
-import org.springframework.web.bind.annotation.PostMapping
-import org.springframework.web.bind.annotation.PutMapping
-import org.springframework.web.bind.annotation.RequestHeader
-import org.springframework.web.bind.annotation.RequestMapping
-import org.springframework.web.bind.annotation.RestController
+import org.springframework.web.bind.annotation.*
 import kotlin.test.assertEquals
 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, BluePrintPropertiesService::class])
 @TestPropertySource(properties =
 [
     "server.port=8443",
@@ -75,7 +67,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",
@@ -91,13 +83,33 @@ 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
                 .blueprintWebClientService("sample")
         val response = restClientService.exchangeResource(
                 HttpMethod.PATCH.name, "/sample/name", "")
-        assertEquals("Patch request successful", response,
+        assertEquals("Patch request successful", response.body,
                 "failed to get patch response")
     }
 
@@ -109,14 +121,14 @@ class RestClientServiceTest {
         headers["X-Transaction-Id"] = "1234"
         val response = restClientService.exchangeResource(HttpMethod.GET.name,
                 "/sample/name", "")
-        assertNotNull(response, "failed to get response")
+        assertNotNull(response.body, "failed to get response")
     }
 
     @Test
     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" +
                 "}"
@@ -128,7 +140,7 @@ class RestClientServiceTest {
         runBlocking {
             val get = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.GET.name,
-                        "/sample/basic", "")}
+                        "/sample/basic", "").body}
             get.start()
             res = get.await()
         }
@@ -161,32 +173,32 @@ class RestClientServiceTest {
             val get1 = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.GET.name,
                         "/sample/aai/v14/business/customers", "", headers,
-                        Customer::class.java)}
+                        Customer::class.java).body}
 
             val get2 = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.GET.name,
                         "/sample/aai/v14/business/customers", "", headers,
-                        Customer::class.java)}
+                        Customer::class.java).body}
 
             val post = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.POST.name,
                         "/sample/aai/v14/business/customers", post1, headers,
-                        String::class.java)}
+                        String::class.java).body}
 
             val put = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.PUT.name,
                         "/sample/aai/v14/business/customers", post1, headers,
-                        String::class.java)}
+                        String::class.java).body}
 
             val patch = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.PATCH.name,
                         "/sample/aai/v14/business/customers", post1, headers,
-                        String::class.java)}
+                        String::class.java).body}
 
             val delete = async(start = CoroutineStart.LAZY) {
                 restClientService.exchangeNB(HttpMethod.DELETE.name,
                         "/sample/aai/v14/business/customers", "", headers,
-                        String::class.java)}
+                        String::class.java).body}
 
             get1.start()
             get2.start()
@@ -306,49 +318,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.setPort(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()
     }
 }
 
@@ -359,4 +347,4 @@ data class Customer(
        val id: String,
        val name: String,
        val type: String,
-       val resource: String)
\ No newline at end of file
+       val resource: String)