/*
 * Copyright 2019 MarkLogic Corporation
 *
 * Licensed 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 com.marklogic.client.test.dataservices;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.marklogic.client.dataservices.impl.OutputCallerImpl;
import com.marklogic.client.io.JacksonHandle;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;

import static org.junit.Assert.*;

public class OutputEndpointImplTest {
    static ObjectNode apiObj;
    static String apiName = "outputCallerImpl.api";
    static String scriptPath;
    static String apiPath;

    @BeforeClass
    public static void setup() throws Exception {
        apiObj = IOTestUtil.readApi(apiName);
        scriptPath = IOTestUtil.getScriptPath(apiObj);
        apiPath = IOTestUtil.getApiPath(scriptPath);
        IOTestUtil.load(apiName, apiObj, scriptPath, apiPath);
    }

    @Test
    public void testOutputCallerImpl() throws IOException {

        String endpointState = "{\"endpoint\":1}";
        String workUnit = "{\"workUnit\":\"/workUnit1\"}";

        OutputCallerImpl caller = new OutputCallerImpl(new JacksonHandle(apiObj));
        Stream<InputStream> result = caller.call(IOTestUtil.db, IOTestUtil.asInputStream(endpointState), caller.newSessionState(),
                IOTestUtil.asInputStream(workUnit));
        assertNotNull(result);
        InputStream[] resultArray = result.toArray(size -> new InputStream[size]);

        ObjectNode resultObj = IOTestUtil.mapper.readValue(resultArray[0], ObjectNode.class);
        assertNotNull(resultObj);
        assertEquals("Endpoint value not as expected.",endpointState, resultObj.get("endpointState").toString());
        assertEquals("Workunit not as expected.",workUnit, resultObj.get("workUnit").toString());
    }

    @Test
    public void testOutputCallerImplWithNullEndpointState() throws IOException {

        String workUnit = "{\"collection\":\"/dataset1\"}";

        OutputCallerImpl caller = new OutputCallerImpl(new JacksonHandle(apiObj));
        Stream<InputStream> result = caller.call(IOTestUtil.db, null, caller.newSessionState(),
                IOTestUtil.asInputStream(workUnit));
        assertNotNull(result);
        InputStream[] resultArray = result.toArray(size -> new InputStream[size]);

        ObjectNode resultObj = IOTestUtil.mapper.readValue(resultArray[0], ObjectNode.class);
        assertNull(resultObj.get("endpointState"));
    }

    @Test
    public void testOutputCallerImplWithNullSession() throws IOException {

        String workUnit = "{\"workUnit\":\"/1\"}";
        OutputCallerImpl caller = new OutputCallerImpl(new JacksonHandle(apiObj));
        Stream<InputStream> result = caller.call(IOTestUtil.db, IOTestUtil.asInputStream("{\"endpoint\":1}"), null,
                IOTestUtil.asInputStream(workUnit));
        assertNotNull(result);
        InputStream[] resultArray = result.toArray(size -> new InputStream[size]);
        ObjectNode resultObj = IOTestUtil.mapper.readValue(resultArray[0], ObjectNode.class);
        assertNull(resultObj.get("session"));
    }

    @Test
    public void testOutputCallerImplWithNull() throws IOException {

        OutputCallerImpl caller = new OutputCallerImpl(new JacksonHandle(apiObj));
        Stream<InputStream> result = caller.call(IOTestUtil.db, null, null,
                null);
        assertNotNull(result);
        InputStream[] resultArray = result.toArray(size -> new InputStream[size]);
        ObjectNode resultObj = IOTestUtil.mapper.readValue(resultArray[0], ObjectNode.class);
        assertNotNull(resultObj);
        assertTrue("Result object is not empty", resultObj.size() == 0);
    }

    @AfterClass
    public static void cleanup() {
        IOTestUtil.modMgr.delete(scriptPath, apiPath);
    }
}

@@ -1788,33 +1788,39 @@
 result = caller.
-c
+streamC
 all(IOTestUtil.d
@@ -2676,33 +2676,39 @@
 result = caller.
-c
+streamC
 all(IOTestUtil.d
@@ -3319,33 +3319,39 @@
 result = caller.
-c
+streamC
 all(IOTestUtil.d
@@ -3925,17 +3925,23 @@
  caller.
-c
+streamC
 all(IOTe

support for arrays in bulk data services #1154