package org.folio.rest;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasSize;

import java.util.Random;

import io.restassured.response.Response;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

import io.restassured.RestAssured;

public class TenantApiTest extends ApiTestBase {
  private Random random = new Random();

  /**
   * @return a new tenant id to ensure that it doesn't exist even if the developer's database hasn't been wiped.
   */
  private String randomTenant() {
    return "tenant" + Math.abs(random.nextLong());
  }

  @Test
  void post() {
    Response response = given(r).
        header("x-okapi-tenant", randomTenant()).
        header("x-okapi-url-to", "http://localhost:" + RestAssured.port).
        body("{\"module_to\":\"mod-api-1.0.0\"}").
        when().post("/_/tenant").
        then().
        statusCode(201).extract().response();
    String location = response.getHeader("Location");
    Assert.assertTrue(location, location.startsWith("/_/tenant/"));

    given(r).
        get(location + "?wait=5000").
        then().
        statusCode(200).
        body("messages", hasSize(0));  // JSON list of commands that have failed
  }

  // https://issues.folio.org/browse/RMB-508 https://issues.folio.org/browse/RMB-511 https://issues.folio.org/browse/MODEVENTC-14
  // mod-event-config /_/tenant with "X-Okapi-Request-Id: 1" fails with 500 Internal Server Error
  @Test
  void posttWithRequestId() {
    Response response = given(r).
        header("x-okapi-tenant", randomTenant()).
        header("x-okapi-url-to", "http://localhost:" + RestAssured.port).
        header("X-Okapi-Request-Id", "1").
        body("{\"module_to\":\"mod-api-1.0.0\"}").
        when().post("/_/tenant").
        then().
        statusCode(201).extract().response();
    String location = response.getHeader("Location");
    Assert.assertTrue(location, location.startsWith("/_/tenant/"));

    given(r).
        get(location + "?wait=5000").
        then().
        statusCode(200).
        body("messages", hasSize(0));  // JSON list of commands that have failed
  }
}

@@ -106,109 +106,91 @@
 ers.
-hasSize;%0A%0Aimport java.util.Random;%0A%0Aimport io.restassured.response.Respons
+startsWith;%0Aimport static org.hamcrest.Matchers.hasSiz
 e;%0A
+%0A
 import 
-org.junit.Assert;
+java.util.Random;%0A
 %0Aimp
@@ -591,33 +591,31 @@
 ) %7B%0A    
-Response response
+String location
  = given
@@ -872,149 +872,98 @@
 01).
-extract().response();%0A    String location = response.getHeader(%22Location%22);%0A    Assert.assertTrue(location, location.startsWith(%22/_/tenant/%22)
+%0A        header(%22Location%22, startsWith(%22/_/tenant/%22)).%0A        extract().header(%22Location%22
 );%0A%0A
@@ -1415,25 +1415,23 @@
     
-Response response
+String location
  = g
@@ -1735,149 +1735,98 @@
 01).
-extract().response();%0A    String location = response.getHeader(%22Location%22);%0A    Assert.assertTrue(location, location.startsWith(%22/_/tenant/%22)
+%0A        header(%22Location%22, startsWith(%22/_/tenant/%22)).%0A        extract().header(%22Location%22
 );%0A%0A

Replace JUnit 4 assert by restassured and hamcrest