// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.apimanagement.generated;

import com.azure.core.credential.AccessToken;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.apimanagement.ApiManagementManager;
import com.azure.resourcemanager.apimanagement.models.EmailTemplateContract;
import com.azure.resourcemanager.apimanagement.models.EmailTemplateParametersContractProperties;
import com.azure.resourcemanager.apimanagement.models.TemplateName;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public final class EmailTemplatesCreateOrUpdateWithResponseMockTests {
    @Test
    public void testCreateOrUpdateWithResponse() throws Exception {
        HttpClient httpClient = Mockito.mock(HttpClient.class);
        HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
        ArgumentCaptor<HttpRequest> httpRequest = ArgumentCaptor.forClass(HttpRequest.class);

        String responseStr =
            "{\"properties\":{\"subject\":\"xfzzjdm\",\"body\":\"pbusxy\",\"title\":\"oz\",\"description\":\"lxzgzum\",\"isDefault\":true,\"parameters\":[{\"name\":\"kkbyg\",\"title\":\"i\",\"description\":\"wyshybbnhtt\"},{\"name\":\"zonzsurqcojasfz\",\"title\":\"zcarc\",\"description\":\"oxyipdthjfvnh\"},{\"name\":\"g\",\"title\":\"putfelfchnu\",\"description\":\"sjgbfbbach\"}]},\"id\":\"xczzunfnbphcee\",\"name\":\"vkbuxlepg\",\"type\":\"cnuqhqpvtw\"}";

        Mockito.when(httpResponse.getStatusCode()).thenReturn(200);
        Mockito.when(httpResponse.getHeaders()).thenReturn(new HttpHeaders());
        Mockito
            .when(httpResponse.getBody())
            .thenReturn(Flux.just(ByteBuffer.wrap(responseStr.getBytes(StandardCharsets.UTF_8))));
        Mockito
            .when(httpResponse.getBodyAsByteArray())
            .thenReturn(Mono.just(responseStr.getBytes(StandardCharsets.UTF_8)));
        Mockito
            .when(httpClient.send(httpRequest.capture(), Mockito.any()))
            .thenReturn(
                Mono
                    .defer(
                        () -> {
                            Mockito.when(httpResponse.getRequest()).thenReturn(httpRequest.getValue());
                            return Mono.just(httpResponse);
                        }));

        ApiManagementManager manager =
            ApiManagementManager
                .configure()
                .withHttpClient(httpClient)
                .authenticate(
                    tokenRequestContext -> Mono.just(new AccessToken("this_is_a_token", OffsetDateTime.MAX)),
                    new AzureProfile("", "", AzureEnvironment.AZURE));

        EmailTemplateContract response =
            manager
                .emailTemplates()
                .define(TemplateName.PURCHASE_DEVELOPER_NOTIFICATION_MESSAGE)
                .withExistingService("j", "dlxbaeyocpkv")
                .withSubject("dzf")
                .withTitle("pbdr")
                .withDescription("bjxnnnoztn")
                .withBody("dtuoamqobqe")
                .withParameters(
                    Arrays
                        .asList(
                            new EmailTemplateParametersContractProperties()
                                .withName("htisyzfeoctr")
                                .withTitle("wn")
                                .withDescription("ckze"),
                            new EmailTemplateParametersContractProperties()
                                .withName("bvwdxgyypmxq")
                                .withTitle("mlnxrca")
                                .withDescription("uhske")))
                .withIfMatch("dkvviilyes")
                .create();

        Assertions.assertEquals("xfzzjdm", response.subject());
        Assertions.assertEquals("pbusxy", response.body());
        Assertions.assertEquals("oz", response.title());
        Assertions.assertEquals("lxzgzum", response.description());
        Assertions.assertEquals("kkbyg", response.parameters().get(0).name());
        Assertions.assertEquals("i", response.parameters().get(0).title());
        Assertions.assertEquals("wyshybbnhtt", response.parameters().get(0).description());
    }
}
