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

import com.couchbase.client.java.Collection;
import com.couchbase.client.java.kv.GetResult;
import com.couchbase.client.java.kv.LookupInResult;
import com.couchbase.client.java.kv.MutationResult;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import org.apache.nifi.services.couchbase.exception.CouchbaseException;
import org.apache.nifi.services.couchbase.utils.CouchbaseGetResult;
import org.apache.nifi.services.couchbase.utils.CouchbaseLookupInResult;
import org.apache.nifi.services.couchbase.utils.CouchbaseUpsertResult;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.nifi.services.couchbase.utils.DocumentType.JSON;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CouchbaseClientTest {

    private static final String TEST_DOCUMENT_ID = "test-document-id";
    private static final long TEST_CAS = 1L;
    private static Collection collection;

    @BeforeAll
    static void beforeAll() {
        collection = mock(Collection.class);
    }

    @Test
    void testPutJsonDocument() throws CouchbaseException {
        final String content = "{\"key\":\"value\"}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final MutationResult result = mock(MutationResult.class);
        when(result.cas()).thenReturn(TEST_CAS);

        when(collection.upsert(anyString(), any(), any())).thenReturn(result);

        final CouchbaseUpsertResult upsertResult = client.upsertDocument(TEST_DOCUMENT_ID, content.getBytes());

        assertEquals(TEST_CAS, upsertResult.cas());
    }

    @Test
    void testPutJsonDocumentValidationFailure() {
        final String content = "{invalid-json}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final Exception exception = assertThrows(CouchbaseException.class, () -> client.upsertDocument(TEST_DOCUMENT_ID, content.getBytes()));
        assertTrue(exception.getMessage().contains("The provided input is invalid"));
    }

    @Test
    void testInsertJsonDocumentValidationFailure() {
        final String content = "{invalid-json}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final Exception exception = assertThrows(CouchbaseException.class, () -> client.insertDocument(TEST_DOCUMENT_ID, content.getBytes()));
        assertTrue(exception.getMessage().contains("The provided input is invalid"));
    }

    @Test
    void testReplaceJsonDocumentValidationFailure() {
        final String content = "{invalid-json}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final Exception exception = assertThrows(CouchbaseException.class, () -> client.replaceDocument(TEST_DOCUMENT_ID, content.getBytes(), TEST_CAS));
        assertTrue(exception.getMessage().contains("The provided input is invalid"));
    }

    @Test
    void testGetDocument() throws CouchbaseException {
        final String content = "{\"key\":\"value\"}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final Instant expiryTime = Instant.now();
        final GetResult result = mock(GetResult.class);
        when(result.cas()).thenReturn(TEST_CAS);
        when(result.contentAsBytes()).thenReturn(content.getBytes());
        when(result.expiryTime()).thenReturn(Optional.of(expiryTime));

        when(collection.get(anyString(), any())).thenReturn(result);

        final CouchbaseGetResult getResult = client.getDocument(TEST_DOCUMENT_ID);

        assertEquals(TEST_CAS, getResult.cas());
        assertArrayEquals(content.getBytes(), getResult.resultContent());
    }

    @Test
    void testLookupInWithMapResult() throws CouchbaseException {
        final String expectedResult = "{\"name\":\"John\",\"age\":\"20\"}";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        Map<String, String> lookupInContent = new HashMap<>();
        lookupInContent.put("name", "John");
        lookupInContent.put("age", "20");

        final LookupInResult result = mock(LookupInResult.class);
        when(result.contentAs(anyInt(), any(Class.class))).thenReturn(lookupInContent);
        when(result.exists(anyInt())).thenReturn(true);
        when(result.cas()).thenReturn(TEST_CAS);

        when(collection.lookupIn(anyString(), any())).thenReturn(result);

        final CouchbaseLookupInResult lookupInResult = client.lookupIn(TEST_DOCUMENT_ID, "");

        assertEquals(expectedResult, lookupInResult.resultContent());
        assertEquals(TEST_CAS, lookupInResult.cas());
    }

    @Test
    void testLookupInWithArrayResult() throws CouchbaseException {
        final String expectedResult = "[{\"name\":\"John\"},{\"name\":\"Jack\"}]";
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        List<Object> lookupInContent = new ArrayList<>();
        lookupInContent.add(Collections.singletonMap("name", "John"));
        lookupInContent.add(Collections.singletonMap("name", "Jack"));

        final LookupInResult result = mock(LookupInResult.class);
        when(result.contentAs(anyInt(), any(Class.class))).thenReturn(lookupInContent);
        when(result.exists(anyInt())).thenReturn(true);
        when(result.cas()).thenReturn(TEST_CAS);

        when(collection.lookupIn(anyString(), any())).thenReturn(result);

        final CouchbaseLookupInResult lookupInResult = client.lookupIn(TEST_DOCUMENT_ID, "");

        assertEquals(expectedResult, lookupInResult.resultContent());
        assertEquals(TEST_CAS, lookupInResult.cas());
    }

    @Test
    void testLookupInWithNoResult() {
        final StandardCouchbaseClient client = new StandardCouchbaseClient(collection, JSON, PersistTo.ONE, ReplicateTo.ONE);

        final LookupInResult result = mock(LookupInResult.class);
        when(result.exists(anyInt())).thenReturn(false);

        when(collection.lookupIn(anyString(), any())).thenReturn(result);

        final Exception exception = assertThrows(CouchbaseException.class, () -> client.lookupIn(TEST_DOCUMENT_ID, "test-path"));
        assertTrue(exception.getCause().getMessage().contains("No value found on the requested path [test-path] in Couchbase"));
    }
}
