package ch.unisg.roster.roster.adapter.out.persistence.mongodb;


import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.roster.roster.domain.RosterItem;
import ch.unisg.roster.roster.domain.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureDataMongo
@Import({RosterPersistenceAdapter.class, RosterMapper.class})
public class RosterPersistenceAdapterTest {

    @Autowired
    private RosterRepository rosterRepository;

    @Autowired
    private RosterPersistenceAdapter adapterunderTest;

    @Test
    void addsNewRosterItem(){

        String taskId = "test-id";
        String executorType = "test-type";
        String executorURI = "test-uri";

        RosterItem testRosterItem = new RosterItem(
            taskId,
            executorType,
            new ExecutorURI(executorURI)
        );
        adapterunderTest.addRosterItem(testRosterItem);

        MongoRosterDocument retrievedDoc = rosterRepository.findByTaskId(taskId);

        assertThat(retrievedDoc.taskId).isEqualTo(taskId);
        assertThat(retrievedDoc.executorURI).isEqualTo(executorURI);
        assertThat(retrievedDoc.taskType).isEqualTo(executorType);

    }

    @Test
    void retrievesRosterItem(){

        String taskId = "test-id";
        String executorType = "test-type";
        String executorURI = "test-uri";

        MongoRosterDocument mongoRosterDocument = new MongoRosterDocument(taskId, executorType, executorURI);
        rosterRepository.insert(mongoRosterDocument);

        RosterItem retrievedRosterItem = adapterunderTest.loadRosterItem(taskId);

        assertThat(retrievedRosterItem.getTaskID()).isEqualTo(taskId);
        assertThat(retrievedRosterItem.getTaskType()).isEqualTo(executorType);
        assertThat(retrievedRosterItem.getExecutorURI()).isEqualTo(executorURI);
    }
}
