/*
 * 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.asana;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.asana.models.Tag;
import java.util.stream.Stream;
import org.apache.nifi.controller.asana.AsanaClient;
import org.apache.nifi.processors.asana.utils.AsanaObject;
import org.apache.nifi.processors.asana.utils.AsanaObjectFetcher;
import org.apache.nifi.processors.asana.utils.AsanaObjectState;
import org.apache.nifi.processors.asana.utils.AsanaTagFetcher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class AsanaTagFetcherTest {

    @Mock
    private AsanaClient client;

    @Test
    public void testNoTagsFetchedWhenNoTagsReturned() {
        when(client.getTags()).then(invocation -> Stream.empty());

        final AsanaObjectFetcher fetcher = new AsanaTagFetcher(client);
        assertNull(fetcher.fetchNext());

        verify(client, times(1)).getTags();
        verifyNoMoreInteractions(client);
    }

    @Test
    public void testSingleTagFetched() {
        final Tag tag = new Tag();
        tag.gid = "123";
        tag.name = "My Tag";

        when(client.getTags()).then(invocation -> Stream.of(tag));

        final AsanaObjectFetcher fetcher = new AsanaTagFetcher(client);
        final AsanaObject object = fetcher.fetchNext();

        assertEquals(AsanaObjectState.NEW, object.getState());
        assertEquals(tag.gid, object.getGid());
        verify(client, times(1)).getTags();
        verifyNoMoreInteractions(client);
    }

    @Test
    public void testSingleTagUpdatedWhenAnyPartChanges() {
        final Tag tag = new Tag();
        tag.gid = "123";
        tag.name = "My Tag";

        when(client.getTags()).then(invocation -> Stream.of(tag));

        final AsanaObjectFetcher fetcher = new AsanaTagFetcher(client);
        assertNotNull(fetcher.fetchNext());
        assertNull(fetcher.fetchNext());

        tag.color = "brown";
        final AsanaObject object = fetcher.fetchNext();

        assertEquals(AsanaObjectState.UPDATED, object.getState());
        assertEquals(tag.gid, object.getGid());
        verify(client, times(2)).getTags();
        verifyNoMoreInteractions(client);
    }

    @Test
    public void testClearState() {
        final Tag tag = new Tag();
        tag.gid = "123";
        tag.name = "My Tag";

        when(client.getTags()).then(invocation -> Stream.of(tag));

        final AsanaObjectFetcher fetcher = new AsanaTagFetcher(client);
        assertNotNull(fetcher.fetchNext());
        assertNull(fetcher.fetchNext());

        fetcher.clearState();

        tag.color = "brown";
        final AsanaObject object = fetcher.fetchNext();

        assertEquals(AsanaObjectState.NEW, object.getState());
        assertEquals(tag.gid, object.getGid());
        verify(client, times(2)).getTags();
        verifyNoMoreInteractions(client);
    }

    @Test
    public void testRestoreStateAndContinue() {
        final Tag tag = new Tag();
        tag.gid = "123";
        tag.name = "My Tag";

        when(client.getTags()).then(invocation -> Stream.of(tag));

        final AsanaObjectFetcher fetcher1 = new AsanaTagFetcher(client);
        assertNotNull(fetcher1.fetchNext());
        assertNull(fetcher1.fetchNext());

        final AsanaObjectFetcher fetcher2 = new AsanaTagFetcher(client);
        fetcher2.loadState(fetcher1.saveState());

        tag.color = "brown";
        final AsanaObject object = fetcher2.fetchNext();

        assertEquals(AsanaObjectState.UPDATED, object.getState());
        assertEquals(tag.gid, object.getGid());
        verify(client, times(2)).getTags();
        verifyNoMoreInteractions(client);
    }
}
