// Copyright (C) 2007-2008 Google Inc.
//
// 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.google.enterprise.connector.otex;

import java.io.File;

import junit.framework.TestCase;

import com.google.enterprise.connector.spi.Document;
import com.google.enterprise.connector.spi.DocumentList;
import com.google.enterprise.connector.spi.RepositoryException;
import com.google.enterprise.connector.spi.TraversalManager;
import com.google.enterprise.connector.manager.Context;
import com.google.enterprise.connector.pusher.FeedException;
import com.google.enterprise.connector.pusher.Pusher;
import com.google.enterprise.connector.pusher.PushException;
import com.google.enterprise.connector.pusher.DocPusher;
import com.google.enterprise.connector.pusher.GsaFeedConnection;
import com.google.enterprise.connector.traversal.FileSizeLimitInfo;
import com.google.enterprise.connector.otex.client.Client;
import com.google.enterprise.connector.otex.client.ClientValue;

/**
 * Pushes a single document (DocID) to the GSA
 */
public class PushOneDocumentTest extends TestCase {
    private LivelinkConnector conn;
    private LivelinkSession sess;
    private Client client;
    private Pusher pusher;

    // TODO: get pusher server and port from [connector.?] properties
    //private String feedServer = "8.6.49.37";
    private String feedServer = "gogol.vizdom.com";
    private int feedPort = 19900;

    public void setUp() throws Exception {
        conn = LivelinkConnectorFactory.getConnector("connector.");
        sess = (LivelinkSession) conn.login();
        client = sess.getFactory().createClient();
        pusher = new DocPusher(new GsaFeedConnection(feedServer, feedPort),
            "livelink", new FileSizeLimitInfo());

        // Iinitialize the Context for DocPusher.take.
        // FIXME: This code is duplicated in LivelinkQueryTraverserTest..
        String cmDir = System.getProperty("connector-manager.dir");
        if (cmDir == null)
            throw new Exception("Missing connector-manager.dir property.");
        String dir = "file:" + cmDir + File.separator;
        Context.getInstance().setStandaloneContext(
            dir + Context.DEFAULT_JUNIT_CONTEXT_LOCATION,
            dir + Context.DEFAULT_JUNIT_COMMON_DIR_PATH);
    }

    public void testTraversal() throws Exception {
        int objectId = getId();

        // Extract the LastModifiedDate of the DocID from Livelink
        // to forge a checkpoint.
        ClientValue docInfo = client.GetObjectInfo(0, objectId);
        Checkpoint checkpoint = new Checkpoint();
        checkpoint.setInsertCheckpoint(docInfo.toDate("ModifyDate"),
                                       objectId - 1);

        // Now push that one document.
        TraversalManager mgr = sess.getTraversalManager();
        mgr.setBatchHint(1);
        DocumentList rs = mgr.resumeTraversal(checkpoint.toString() + ',');
        pushResultSet(rs);
    }

    private int getId() throws RepositoryException {
        try {
            String obj = System.getProperty("test.docid");
            if (obj == null)
                obj = "BAD";
            return Integer.parseInt(obj);
        } catch (NumberFormatException e) {
            throw new RepositoryException("Please specify an integer test.docid property: -Dtest.docid=12345");
        }
    }

    private void pushResultSet(DocumentList docList)
        throws RepositoryException, PushException, FeedException {

        Document doc;

        while ((doc = docList.nextDocument()) != null) {
            // TODO: make the feed name a property, along with the feed server and port
            pusher.take(doc, null);
        }
    }
}

@@ -1383,16 +1383,89 @@
 tInfo;%0D%0A
+import com.google.enterprise.connector.util.filter.DocumentFilterChain;%0D%0A
 import c
@@ -1512,24 +1512,24 @@
 nt.Client;%0D%0A
-
 import com.g
@@ -1899,56 +1899,8 @@
 es%0D%0A
-    //private String feedServer = %228.6.49.37%22;%0D%0A
     
@@ -1937,19 +1937,8 @@
 ogol
-.vizdom.com
 %22;%0D%0A
@@ -2218,16 +2218,30 @@
 cPusher(
+%0D%0A            
 new GsaF
@@ -2254,16 +2254,22 @@
 nection(
+null, 
 feedServ
@@ -2280,16 +2280,20 @@
 feedPort
+, -1
 ),%0D%0A    
@@ -2335,16 +2335,43 @@
 itInfo()
+, new DocumentFilterChain()
 );%0D%0A%0D%0A  

Fix the tests to compile against connector manager 2.8 (as of 2.8.0-M4).