<%#
 Copyright 2013-2022 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.web.rest.errors;

import <%= packageName %>.IntegrationTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (testsNeedCsrf) { _%>
import org.junit.jupiter.api.BeforeEach;

<%_ } _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ if (testsNeedCsrf) { _%>

import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } _%>

/**
 * Integration tests {@link ExceptionTranslator} controller advice.
 */
@WithMockUser
@AutoConfigureWebTestClient(timeout = IntegrationTest.DEFAULT_TIMEOUT)
@IntegrationTest
class ExceptionTranslatorIT {

    @Autowired
    private WebTestClient webTestClient;
<%_ if (testsNeedCsrf) { _%>

    @BeforeEach
    public void setupCsrf() {
        webTestClient = webTestClient.mutateWith(csrf());
    }
<%_ } _%>
<%_ if (!databaseTypeNo && !databaseTypeCassandra) { _%>

    @Test
    void testConcurrencyFailure() {
        webTestClient.get().uri("/api/exception-translator-test/concurrency-failure")
            .exchange()
            .expectStatus()
            .isEqualTo(HttpStatus.CONFLICT)
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.CONFLICT.getReasonPhrase());
    }
<%_ } _%>

    @Test
    void testMethodArgumentNotValid() {
         webTestClient.post().uri("/api/exception-translator-test/method-argument")
             .contentType(MediaType.APPLICATION_JSON)
             .bodyValue("{}")
             .exchange()
             .expectStatus().isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY)
             .expectHeader().contentType(MediaType.APPLICATION_JSON)
             .expectBody()
             .jsonPath("$.errors.test.[0]").isEqualTo("must not be null");
    }

    @Test
    void testMissingRequestPart() {
        webTestClient.get().uri("/api/exception-translator-test/missing-servlet-request-part")
            .exchange()
            .expectStatus().isBadRequest()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    @Test
    void testMissingRequestParameter() {
        webTestClient.get().uri("/api/exception-translator-test/missing-servlet-request-parameter")
            .exchange()
            .expectStatus().isBadRequest()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    @Test
    void testAccessDenied() {
        webTestClient.get().uri("/api/exception-translator-test/access-denied")
            .exchange()
            .expectStatus().isForbidden()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo("[API] This action requires merchant approval for the necessary scope.");
    }

    @Test
    void testUnauthorized() {
        webTestClient.get().uri("/api/exception-translator-test/unauthorized")
            .exchange()
            .expectStatus().isUnauthorized()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo("[API] Invalid API key or access token (unrecognized login or wrong password)");
    }

    @Test
    void testMethodNotSupported() {
        webTestClient.post().uri("/api/exception-translator-test/access-denied")
            .exchange()
            .expectStatus().isEqualTo(HttpStatus.NOT_ACCEPTABLE)
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.NOT_ACCEPTABLE.getReasonPhrase());
    }

    @Test
    void testExceptionWithResponseStatus() {
        webTestClient.get().uri("/api/exception-translator-test/response-status")
            .exchange()
            .expectStatus().isBadRequest()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    @Test
    void testInternalServerError() {
        webTestClient.get().uri("/api/exception-translator-test/internal-server-error")
            .exchange()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    @Test
    void testNtsNotFound() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/nts-not-found")
            .exchange()
            .expectStatus().isNotFound()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors").isEqualTo(HttpStatus.NOT_FOUND.getReasonPhrase());
    }

    @Test
    void testNtsValidateObject() {
        webTestClient
            .post()
            .uri("/api/exception-translator-test/nts-validate-object")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue("{}")
            .exchange()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors.test.[0]").isEqualTo("must not be null");
    }

    @Test
    void testNtsValidationError() {
        webTestClient
            .put()
            .uri("/api/exception-translator-test/nts-validation-error")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue("{}")
            .exchange()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.errors.test.[0]").isEqualTo("must not be null");
    }
}
