/*
 * Copyright 2015-2017 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.datamovement;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory.SecurityContext;
import com.marklogic.client.datamovement.DataMovementManager;
import com.marklogic.client.datamovement.impl.DataMovementManagerImpl;
import com.marklogic.client.datamovement.Forest;
import com.marklogic.client.datamovement.ForestConfiguration;

import com.marklogic.client.test.Common;

public class ForestConfigTest {
  private static DatabaseClient client = Common.connect();
  private DataMovementManager moveMgr = client.newDataMovementManager();

  @BeforeClass
  public static void beforeClass() {
  }

  @AfterClass
  public static void afterClass() {
  }

  @Test
  public void testArgs() throws Exception {
    int defaultPort = client.getPort();
    Class<?> defaultAuthContext = client.getSecurityContext().getClass();
    ForestConfiguration forestConfig = moveMgr.readForestConfig();
    Forest[] forests = forestConfig.listForests();
    String defaultDatabase = forests[0].getDatabaseName();
    assertEquals(3, forests.length);
    for ( Forest forest : forests ) {
      DatabaseClient forestClient = ((DataMovementManagerImpl) moveMgr).getForestClient(forest);
      // not all forests for a database are on the same host, so all we
      // can check is that the hostname is not null
      assertNotNull(forest.getHost());
      // not all hosts have the original REST server, but all hosts have the uber port
      assertEquals(defaultPort, forestClient.getPort());
      assertEquals(defaultDatabase, forest.getDatabaseName());
      assertEquals(defaultAuthContext, forestClient.getSecurityContext().getClass());
      assertEquals(true, forest.isUpdateable());
      if ( ! "java-unittest-1".equals(forest.getForestName()) &&
           ! "java-unittest-2".equals(forest.getForestName()) &&
           ! "java-unittest-3".equals(forest.getForestName()) ) {
        fail("Unexpected forestName \"" + forest.getForestName() + "\"");
      }
    }
  }
}

@@ -757,24 +757,68 @@
 rtNotNull;%0D%0A
+import static org.junit.Assert.assertTrue;%0D%0A
 import stati
@@ -1986,24 +1986,57 @@
     
-assertEquals(3, 
+// expect three forests per node%0D%0A    assertTrue(
 fore
@@ -2045,16 +2045,25 @@
 s.length
+ %25 3 == 0
 );%0D%0A    
@@ -2723,35 +2723,8 @@
 f ( 
-! %22java-unittest-1%22.equals(
 fore
@@ -2733,36 +2733,43 @@
 .getForestName()
-) &&
+ == null %7C%7C
 %0D%0A           ! %22
@@ -2771,121 +2771,58 @@
   ! 
-%22java-unittest-2%22.equals(forest.getForestName()) &&%0D%0A           ! %22java-unittest-3%22.equals(forest.getForestName()
+forest.getForestName().startsWith(%22java-unittest-%22
 ) ) 

fix #489 - make this test also work against a cluster