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

package com.azure.security.confidentialledger;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.RequestOptions;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public final class LedgerEntriesTest extends ConfidentialLedgerClientTestBase {
    @Test
    public void testPostLedgerEntryTests() throws Exception {
        BinaryData entry = BinaryData.fromString("{\"contents\":\"New ledger entry contents.\"}");
        RequestOptions requestOptions = new RequestOptions();
        Response<BinaryData> response = confidentialLedgerClient.createLedgerEntryWithResponse(entry, requestOptions);

        String transactionId = response.getHeaders().get("x-ms-ccf-transaction-id").getValue();

        BinaryData parsedResponse = response.getValue();

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode responseBodyJson = null;

        responseBodyJson = objectMapper.readTree(parsedResponse.toBytes());

        Assertions.assertEquals(responseBodyJson.get("collectionId").asText(), "subledger:0");

        Response<BinaryData> transactionResponse = confidentialLedgerClient.getTransactionStatusWithResponse(transactionId, requestOptions);

        JsonNode transactionResponseBodyJson = null;

        try {
            transactionResponseBodyJson = objectMapper.readTree(transactionResponse.getValue().toBytes());
        } catch (IOException e) {
            e.printStackTrace();
            Assertions.assertTrue(false);
        }

        Assertions.assertEquals(transactionResponseBodyJson.get("transactionId").asText(), transactionId);
        Assertions.assertTrue(200 == transactionResponse.getStatusCode() || 406 == transactionResponse.getStatusCode());

        Response<BinaryData> currentResponse = confidentialLedgerClient.getCurrentLedgerEntryWithResponse(requestOptions);

        JsonNode currentResponseBodyJson = null;

        try {
            
            currentResponseBodyJson = objectMapper.readTree(currentResponse.getValue().toBytes());
        } catch (IOException e) {
            e.printStackTrace();
            Assertions.assertTrue(false);
        }

        Assertions.assertTrue(200 == currentResponse.getStatusCode() || 406 == currentResponse.getStatusCode());

        if (200 == currentResponse.getStatusCode()) {
            // we assume no one else is using this test ledger
            Assertions.assertTrue(currentResponseBodyJson.get("transactionId").asDouble() <= Double.parseDouble(transactionId));
        }
    }

    @Test
    public void testGetCollectionIdsTests() throws Exception {
        RequestOptions requestOptions = new RequestOptions();
        PagedIterable<BinaryData> pagedIterableResponse = confidentialLedgerClient.listCollections(requestOptions);

        List<String> collectionKeys = new ArrayList<>();

        pagedIterableResponse.streamByPage().forEach(resp -> {
            Assertions.assertEquals(200, resp.getStatusCode());
            resp.getValue().forEach(item -> {
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode responseBodyJson = null;

                try {
                    responseBodyJson = objectMapper.readTree(item.toBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                    Assertions.assertTrue(false);
                }

                Assertions.assertNotNull(responseBodyJson.get("collectionId"));
                collectionKeys.add(responseBodyJson.get("collectionId").asText());
            });

            collectionKeys.stream().anyMatch((item) -> item.contains("subledger:0"));
        });
    }
}

