package com.orchard.models;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import org.apache.kafka.connect.data.Struct;
import org.junit.jupiter.api.Test;

public class GlobalSoundRecordingsStreamsTests {
    public static final String GlobalSoundRecordingsStreamsJson =
        "{\"globalSoundRecordingUuid\":\"uuid123\","
        + "\"isrc\":\"testisrc\","
        + "\"name\":\"testname\","
        + "\"releaseDate\":\"2020-05-01\","
        + "\"imageUrl\":\"testurl\","
        + "\"popularity\":1,"
        + "\"imprint\":\"testimprint\","
        + "\"types\":\"testtype\","
        + "\"hasPublicData\":true,"
        + "\"createdAt\":\"testdate\","
        + "\"lastModifiedAt\":\"testdate\","
        + "\"labelParticipants\":["
        + GSLabelParticipantTests.LabelParticipantJson
        + "],"
        + "\"globalParticipants\":["
        + GlobalParticipantTests.GlobalParticipantJson
        + "],"
        + "\"socialAccountIds\":"
        + GSSocialAccountIdsTests.socialAccountIdsJson
        + ","
        + "\"labelSoundRecordings\":["
        + LabelSoundRecordingTests.LabelSoundRecordingJson
        + "]}";

    @Test
    public void shouldReturnValidKafkaStruct() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        GlobalSoundRecordingsStreams globalSoundRecordingsStreamsParsed = mapper.readValue(
                GlobalSoundRecordingsStreamsJson, GlobalSoundRecordingsStreams.class);

        GlobalSoundRecordingsStreams globalSoundRecordingsStreams = new GlobalSoundRecordingsStreams();

        globalSoundRecordingsStreams.globalSoundRecordingUuid = "uuid123";
        globalSoundRecordingsStreams.isrc = "testisrc";
        globalSoundRecordingsStreams.name = "testname";
        globalSoundRecordingsStreams.releaseDate = "2020-05-01";
        globalSoundRecordingsStreams.imageUrl = "testurl";
        globalSoundRecordingsStreams.popularity = 1;     
        globalSoundRecordingsStreams.imprint = "testimprint";
        globalSoundRecordingsStreams.hasPublicData = true;
        globalSoundRecordingsStreams.types = "testtype";

        globalSoundRecordingsStreams.socialAccountIds = new GSSocialAccountIds();
        globalSoundRecordingsStreams.socialAccountIds.spotify = new ArrayList<String>();
        globalSoundRecordingsStreams.socialAccountIds.spotify.add("testspotifyid");
        globalSoundRecordingsStreams.socialAccountIds.appleMusic = new ArrayList<String>();
        globalSoundRecordingsStreams.socialAccountIds.appleMusic.add("testapplemusicid");
        
        globalSoundRecordingsStreams.labelSoundRecordings = new ArrayList<LabelSoundRecording>();
        globalSoundRecordingsStreams.labelParticipants = new ArrayList<GSLabelParticipant>();
        globalSoundRecordingsStreams.globalParticipants = new ArrayList<GlobalParticipant>();

        globalSoundRecordingsStreams.labelSoundRecordings.add(LabelSoundRecordingTests.CreateLabelSoundRecording());
        globalSoundRecordingsStreams.labelParticipants.add(GSLabelParticipantTests.CreateGSLabelParticipant());
        globalSoundRecordingsStreams.globalParticipants.add(GlobalParticipantTests.CreateGlobalParticipant());
        
        Struct kafkaStruct = globalSoundRecordingsStreams.ToKafkaStruct();

        assertEquals(
                globalSoundRecordingsStreamsParsed.globalSoundRecordingUuid,
                kafkaStruct.getString("globalSoundRecordingUuid"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.isrc,
                kafkaStruct.getString("isrc"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.name,
                kafkaStruct.getString("name"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.releaseDate,
                kafkaStruct.getString("releaseDate"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.imageUrl,
                kafkaStruct.getString("imageUrl"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.popularity,
                kafkaStruct.getInt64("popularity"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.imprint,
                kafkaStruct.getString("imprint"));
        assertEquals(
                globalSoundRecordingsStreamsParsed.types,
                kafkaStruct.getString("types"));      
        assertEquals(
                globalSoundRecordingsStreamsParsed.hasPublicData,
                kafkaStruct.getBoolean("hasPublicData"));

        // We already tested LabelParticipant, GlobalParticipant and LabelSoundRecording deserialization
        // in other test, so comparing only the item count here.
        assertEquals(
                globalSoundRecordingsStreamsParsed.labelParticipants.size(),
                kafkaStruct.getArray("labelParticipants").size());
        assertEquals(
                globalSoundRecordingsStreamsParsed.globalParticipants.size(),
                kafkaStruct.getArray("globalParticipants").size());
        assertEquals(
                globalSoundRecordingsStreamsParsed.labelSoundRecordings.size(),
                kafkaStruct.getArray("labelSoundRecordings").size());

        Struct socialAccountIdsStruct = kafkaStruct.getStruct("socialAccountIds");
        assertArrayEquals(
                globalSoundRecordingsStreamsParsed.socialAccountIds.spotify.toArray(),
                socialAccountIdsStruct.getArray("spotify").toArray());
        assertArrayEquals(
                globalSoundRecordingsStreamsParsed.socialAccountIds.appleMusic.toArray(),
                socialAccountIdsStruct.getArray("appleMusic").toArray());
    }
}
