/***********************************************************
 Copyright (C) 2004 VeriSign, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-0107  USA

 http://www.verisign.com/nds/naming/namestore/techdocs.html
 ***********************************************************/
package com.verisign.epp.codec.change;

import com.verisign.epp.codec.change.util.RandomHelper;
import com.verisign.epp.codec.gen.EPPCodecException;
import com.verisign.epp.codec.gen.EPPCodecTst;
import com.verisign.epp.codec.gen.EPPCommand;
import com.verisign.epp.codec.gen.EPPFactory;
import com.verisign.epp.codec.gen.EPPResponse;
import com.verisign.epp.util.TestThread;

import static org.junit.Assert.fail;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Is a unit test of the com.verisign.epp.codec.change package. The unit test
 * will execute, gather statistics, and output the results of a test of each
 * com.verisign.epp.codec.change package concrete <code>EPPCommand</code>'s
 * and their expected <code>EPPResponse</code>. The unit test is dependent on
 * the use of <a href=http://www.mcwestcorp.com/Junit.html>JUNIT 3.5</a> <BR>
 * NOTE: This is a positive test only. The commands are generated to be within
 * expected parameters. No commands are generated which might be expected to
 * fail.
 * @author jcolosi
 */
public class EPPChangeTst {
  /**
   * Number of each unit test iterations to run.
   */
  static private long numIterations = 100;

  /**
   * LOGIC: Randomized behavior is carefully controlled. All tests are
   * pseudorandom, but if this flag is set, the pseudorandom results are made
   * repeatable for debugging purposes.
   */
  static private final boolean DETERMINISTIC = false;

  static private final String CHECK = "Check";
  static private final String INFO = "Info";
  static private final String CREATE = "Create";
  static private final String UPDATE = "Update";
  static private final String DELETE = "Delete";

  private void testCommand(String title) {
    EPPCommand cmd = null;
    EPPResponse resp = null;

    EPPCodecTst.printStart("Test:Change:" + title);
    for (int i = 0; i < numIterations; i++) {
      if (DETERMINISTIC) RandomHelper.reset(i);
      if (title.equals(CHECK)) {
        cmd = RandomHelper.getChangeCheckCmd();
        resp = RandomHelper.getChangeCheckResp(cmd.getTransId());
      } else if (title.equals(INFO)) {
        cmd = RandomHelper.getChangeInfoCmd();
        resp = RandomHelper.getChangeInfoResp(cmd.getTransId());
      } else if (title.equals(CREATE)) {
        cmd = RandomHelper.getChangeCreateCmd();
        resp = RandomHelper.getEppResponse(cmd.getTransId());
      } else if (title.equals(UPDATE)) {
        cmd = RandomHelper.getChangeUpdateCmd();
        resp = RandomHelper.getChangeUpdateResp(cmd.getTransId());
      } else if (title.equals(DELETE)) {
        cmd = RandomHelper.getChangeDeleteCmd();
        resp = RandomHelper.getEppResponse(cmd.getTransId());
      } else {
        fail("Invalid Test: " + title);
      }

      EPPCodecTst.testEncodeDecode(cmd);
      EPPCodecTst.testEncodeDecode(resp);

    }
    EPPCodecTst.printEnd("Test:Change:" + title);
  }

  @Test
  public void test() {
    testCommand(CHECK);
    testCommand(INFO);
    testCommand(CREATE);
    testCommand(UPDATE);
    testCommand(DELETE);
  }

  /**
   * JUNIT <code>suite</code> static method, which returns the tests
   * associated with <code>EPPChangeTst</code>.
   */
  @BeforeClass
  public static void beforeClass() {
    EPPCodecTst.initEnvironment();

    // iterations Property
    String numIterProp = System.getProperty("iterations");

    if (numIterProp != null) {
      numIterations = Integer.parseInt(numIterProp);
    }

    // Add the EPPChangeMapFactory to the EPPCodec.
    try {
      EPPFactory.getInstance().addMapFactory(
          "com.verisign.epp.codec.change.EPPChangeMapFactory");
    } catch (EPPCodecException e) {
      Assert.fail("EPPCodecException adding EPPChangeMapFactory to EPPCodec: "
          + e);
    }
  }

  /**
   * Sets the number of iterations to run per test.
   * @param aNumIterations number of iterations to run per test
   */
  public static void setNumIterations(long aNumIterations) {
    numIterations = aNumIterations;
  }
}
