package <%= pkg %>.domain.infrastructure

import <%= pkg %>.domain.IdentifiableValueObject
import groovy.json.JsonSlurper
import io.micronaut.core.type.Argument
import io.micronaut.http.client.BlockingHttpClient
import io.micronaut.http.client.RxHttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.security.authentication.UsernamePasswordCredentials
import io.micronaut.security.token.jwt.render.BearerAccessRefreshToken

import javax.inject.Inject

import static io.micronaut.http.HttpRequest.DELETE
import static io.micronaut.http.HttpRequest.GET
import static io.micronaut.http.HttpRequest.POST
import static io.micronaut.http.HttpRequest.PUT

abstract class ControllerSpecificationBase extends ServiceSpecificationBase {
   @Client("/api") @Inject RxHttpClient httpClient

   private BlockingHttpClient client

   void setup() {
      client = httpClient.toBlocking()
   }

   Object get(String path) throws HttpClientResponseException {
      return client.exchange(
         GET("/${path}"),
         Argument.of(String) as Argument<String>,
         Argument.of(String) as Argument<String>
      ).bodyAsJson()

      return jsonSlurper.parseText(json)
   }

   def <BODY extends IdentifiableValueObject> Object post(String path, BODY body) throws HttpClientResponseException {
      return client.exchange(
         POST("/${path}", body),
         Argument.of(String),
         Argument.of(String)
      ).bodyAsJson()
   }

   def <BODY extends IdentifiableValueObject> Object put(String path, BODY body) throws HttpClientResponseException {
      return client.exchange(
         PUT("/${path}", body),
         Argument.of(String),
         Argument.of(String)
      ).bodyAsJson()
   }

   Object delete(String path) throws HttpClientResponseException {
      return client.exchange(
         DELETE("/${path}"),
         Argument.of(String),
         Argument.of(String)
      ).bodyAsJson()
   }
}
