/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *     http://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 org.apache.nifi.processors.salesforce.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.record.MapRecord;
import org.apache.nifi.serialization.record.RecordField;
import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.serialization.record.RecordSchema;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import static org.apache.nifi.processors.salesforce.util.RecordExtender.ATTRIBUTES_RECORD_SCHEMA;
import static org.junit.jupiter.api.Assertions.assertEquals;

class TestRecordExtender {

    private static ObjectMapper objectMapper;
    private static RecordSchema originalSchema;
    private static RecordSchema expectedExtendedSchema;

    @BeforeAll
    public static void setup() {
        objectMapper = new ObjectMapper();
        originalSchema = new SimpleRecordSchema(Arrays.asList(
                new RecordField("testRecordField1", RecordFieldType.STRING.getDataType()),
                new RecordField("testRecordField2", RecordFieldType.STRING.getDataType())
        ));
        expectedExtendedSchema = new SimpleRecordSchema(Arrays.asList(
                new RecordField("testRecordField1", RecordFieldType.STRING.getDataType()),
                new RecordField("testRecordField2", RecordFieldType.STRING.getDataType()),
                new RecordField("attributes", RecordFieldType.RECORD.getRecordDataType(new SimpleRecordSchema(Arrays.asList(
                        new RecordField("type", RecordFieldType.STRING.getDataType()),
                        new RecordField("referenceId", RecordFieldType.STRING.getDataType()
                        )))))
        ));
    }

    private RecordExtender testSubject;

    @BeforeEach
    public void init() {
        testSubject = new RecordExtender(originalSchema);
    }

    @Test
    void testGetWrappedRecordJson() throws IOException {
        ObjectNode testNode = objectMapper.createObjectNode();
        testNode.put("testField1", "testValue1");
        testNode.put("testField2", "testValue2");

        ObjectNode expectedWrappedNode = objectMapper.createObjectNode();
        expectedWrappedNode.set("records", testNode);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(testNode.toString().getBytes());

        ObjectNode actualWrappedJson = testSubject.getWrappedRecordsJson(out);

        assertEquals(expectedWrappedNode, actualWrappedJson);
    }

    @Test
    void testGetExtendedSchema() {
        final RecordSchema actualExtendedSchema = testSubject.getExtendedSchema();

        assertEquals(expectedExtendedSchema, actualExtendedSchema);
    }

    @Test
    void testGetExtendedRecord() {
        int referenceId = 0;
        String objectType = "Account";

        MapRecord testRecord = new MapRecord(originalSchema, Map.of(
                "testRecordField1", "testRecordValue1",
                "testRecordField2", "testRecordValue2"
        ));

        MapRecord expectedRecord = new MapRecord(expectedExtendedSchema, Map.of(
                "attributes", new MapRecord(ATTRIBUTES_RECORD_SCHEMA, Map.of("type", objectType, "referenceId", referenceId)),
                "testRecordField1", "testRecordValue1",
                "testRecordField2", "testRecordValue2"
        ));

        MapRecord actualRecord = testSubject.getExtendedRecord(objectType, referenceId, testRecord);

        assertEquals(expectedRecord, actualRecord);
    }

}
