// Path: src/main/java/org/jboss/elasticsearch/river/sysinfo/IRiverMgm.java
// public interface IRiverMgm {
// 
//   /**
//    * Get name of river.
//    * 
//    * @return name of river
//    */
//   public abstract RiverName riverName();
// 
//   /**
//    * Stop river indexers, but leave instance existing in {@link #riverInstances} so it can be found over management REST
//    * calls and/or reconfigured and started later again. Note that standard ES river {@link #close()} method
//    * implementation removes river instance from {@link #riverInstances}.
//    */
//   public abstract void stop();
// 
//   /**
//    * Restart river. Configuration of river is updated during restart from <code>_meta</code> file.
//    */
//   public abstract void restart();
// 
//   /**
//    * Change indexing period for indexers.
//    * 
//    * @param indexerNames names of indexers to change period for
//    * @param indexingPeriod to use
//    * @return true if at least one indexer of passed names was found
//    */
//   public abstract boolean changeIndexerPeriod(String[] indexerNames, long indexingPeriod);
// 
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/river/sysinfo/mgm/TransportJRMgmBaseAction.java
// @SuppressWarnings("rawtypes")
// public abstract class TransportJRMgmBaseAction<Request extends JRMgmBaseRequest, Response extends JRMgmBaseResponse, NodeRequest extends NodeJRMgmBaseRequest<Request>, NodeResponse extends NodeJRMgmBaseResponse>
// 		extends TransportNodesOperationAction<Request, Response, NodeRequest, NodeResponse> {
// 
// 	protected final ESLogger logger;
// 
// 	@Inject
// 	public TransportJRMgmBaseAction(Settings settings, String actionName, ClusterName clusterName, ThreadPool threadPool,
// 			ClusterService clusterService, TransportService transportService, ActionFilters actionFilters) {
// 		super(settings, actionName, clusterName, threadPool, clusterService, transportService, actionFilters);
// 		logger = Loggers.getLogger(getClass());
// 	}
// 
// 	@Override
// 	protected String executor() {
// 		return ThreadPool.Names.MANAGEMENT;
// 	}
// 
// 	@SuppressWarnings("unchecked")
// 	@Override
// 	protected Response newResponse(Request request, AtomicReferenceArray responses) {
// 		final List<NodeResponse> nodesInfos = new ArrayList<NodeResponse>();
// 		for (int i = 0; i < responses.length(); i++) {
// 			Object resp = responses.get(i);
// 			if (resp instanceof NodeJRMgmBaseResponse) {
// 				nodesInfos.add((NodeResponse) resp);
// 			}
// 		}
// 		return newResponse(clusterName, nodesInfos.toArray(newNodeResponseArray(nodesInfos.size())));
// 	}
// 
// 	protected abstract NodeResponse[] newNodeResponseArray(int len);
// 
// 	protected abstract Response newResponse(ClusterName clusterName, NodeResponse[] array);
// 
// 	@Override
// 	protected boolean accumulateExceptions() {
// 		return false;
// 	}
// 
// 	@Override
// 	protected NodeResponse nodeOperation(NodeRequest nodeRequest) throws ElasticsearchException {
// 		Request req = nodeRequest.getRequest();
// 		logger.debug("Go to look for Sysinfo river '{}' on this node", req.getRiverName());
// 		IRiverMgm river = SysinfoRiver.getRunningInstance(req.getRiverName());
// 		if (river == null) {
// 			logger.debug("Sysinfo River {} not found on this node", req.getRiverName());
// 			return newNodeResponse();
// 		} else {
// 			logger.debug("Sysinfo River {} found on this node, go to call mgm operation on it {}", req.getRiverName(), req);
// 			try {
// 				return performOperationOnRiver(river, req, clusterService.localNode());
// 			} catch (Exception e) {
// 				logger.error("Exception from river management operation: {}", e, e.getMessage());
// 				throw new ElasticsearchException(e.getMessage(), e);
// 			}
// 		}
// 	}
// 
// 	/**
// 	 * Implement in subclass to perform necessary management operation on river instance. This method is called only on
// 	 * node where river really runs.
// 	 * 
// 	 * @param river instance to perform operation on, never null
// 	 * @param req request for operation to process
// 	 * @param node this operation runs on (used to construct response etc.)
// 	 * @return node response with operation result
// 	 * @throws Exception if something is wrong
// 	 */
// 	protected abstract NodeResponse performOperationOnRiver(IRiverMgm river, Request req, DiscoveryNode node)
// 			throws Exception;
// }


import junit.framework.Assert;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.DummyTransportAddress;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.TransportService;
import org.jboss.elasticsearch.river.sysinfo.IRiverMgm;
import org.jboss.elasticsearch.river.sysinfo.mgm.TransportJRMgmBaseAction;
import org.junit.Test;
import org.mockito.Mockito;
		Assert.assertNotNull(array);
		Assert.assertEquals(2, array.length);
	}

	@Test
	public void newResponse() {
		TransportJRLifecycleAction tested = prepareTestedInstance(clusterName);

		NodeJRLifecycleResponse[] array = new NodeJRLifecycleResponse[0];
		JRLifecycleResponse resp = tested.newResponse(clusterName, array);
		Assert.assertNotNull(resp);
		Assert.assertEquals(resp.getClusterName(), clusterName);
		Assert.assertEquals(resp.getNodes(), array);

	}

	@Test
	public void performOperationOnJiraRiver() throws Exception {

		TransportJRLifecycleAction tested = prepareTestedInstance(clusterName);

		IRiverMgm river = Mockito.mock(IRiverMgm.class);

		{
			JRLifecycleRequest req = new JRLifecycleRequest("myriver", JRLifecycleCommand.RESTART);
			NodeJRLifecycleResponse resp = tested.performOperationOnRiver(river, req, dn);
			Assert.assertNotNull(resp);
			Assert.assertTrue(resp.isRiverFound());
			Assert.assertEquals(dn, resp.getNode());
			Mockito.verify(river).restart();
// Path: src/main/java/org/jboss/elasticsearch/river/sysinfo/IRiverMgm.java
// public interface IRiverMgm {
// 
//   /**
//    * Get name of river.
//    * 
//    * @return name of river
//    */
//   public abstract RiverName riverName();
// 
//   /**
//    * Stop river indexers, but leave instance existing in {@link #riverInstances} so it can be found over management REST
//    * calls and/or reconfigured and started later again. Note that standard ES river {@link #close()} method
//    * implementation removes river instance from {@link #riverInstances}.
//    */
//   public abstract void stop();
// 
//   /**
//    * Restart river. Configuration of river is updated during restart from <code>_meta</code> file.
//    */
//   public abstract void restart();
// 
//   /**
//    * Change indexing period for indexers.
//    * 
//    * @param indexerNames names of indexers to change period for
//    * @param indexingPeriod to use
//    * @return true if at least one indexer of passed names was found
//    */
//   public abstract boolean changeIndexerPeriod(String[] indexerNames, long indexingPeriod);
// 
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/river/sysinfo/mgm/TransportJRMgmBaseAction.java
// @SuppressWarnings("rawtypes")
// public abstract class TransportJRMgmBaseAction<Request extends JRMgmBaseRequest, Response extends JRMgmBaseResponse, NodeRequest extends NodeJRMgmBaseRequest<Request>, NodeResponse extends NodeJRMgmBaseResponse>
// 		extends TransportNodesOperationAction<Request, Response, NodeRequest, NodeResponse> {
// 
// 	protected final ESLogger logger;
// 
// 	@Inject
// 	public TransportJRMgmBaseAction(Settings settings, String actionName, ClusterName clusterName, ThreadPool threadPool,
// 			ClusterService clusterService, TransportService transportService, ActionFilters actionFilters) {
// 		super(settings, actionName, clusterName, threadPool, clusterService, transportService, actionFilters);
// 		logger = Loggers.getLogger(getClass());
// 	}
// 
// 	@Override
// 	protected String executor() {
// 		return ThreadPool.Names.MANAGEMENT;
// 	}
// 
// 	@SuppressWarnings("unchecked")
// 	@Override
// 	protected Response newResponse(Request request, AtomicReferenceArray responses) {
// 		final List<NodeResponse> nodesInfos = new ArrayList<NodeResponse>();
// 		for (int i = 0; i < responses.length(); i++) {
// 			Object resp = responses.get(i);
// 			if (resp instanceof NodeJRMgmBaseResponse) {
// 				nodesInfos.add((NodeResponse) resp);
// 			}
// 		}
// 		return newResponse(clusterName, nodesInfos.toArray(newNodeResponseArray(nodesInfos.size())));
// 	}
// 
// 	protected abstract NodeResponse[] newNodeResponseArray(int len);
// 
// 	protected abstract Response newResponse(ClusterName clusterName, NodeResponse[] array);
// 
// 	@Override
// 	protected boolean accumulateExceptions() {
// 		return false;
// 	}
// 
// 	@Override
// 	protected NodeResponse nodeOperation(NodeRequest nodeRequest) throws ElasticsearchException {
// 		Request req = nodeRequest.getRequest();
// 		logger.debug("Go to look for Sysinfo river '{}' on this node", req.getRiverName());
// 		IRiverMgm river = SysinfoRiver.getRunningInstance(req.getRiverName());
// 		if (river == null) {
// 			logger.debug("Sysinfo River {} not found on this node", req.getRiverName());
// 			return newNodeResponse();
// 		} else {
// 			logger.debug("Sysinfo River {} found on this node, go to call mgm operation on it {}", req.getRiverName(), req);
// 			try {
// 				return performOperationOnRiver(river, req, clusterService.localNode());
// 			} catch (Exception e) {
// 				logger.error("Exception from river management operation: {}", e, e.getMessage());
// 				throw new ElasticsearchException(e.getMessage(), e);
// 			}
// 		}
// 	}
// 
// 	/**
// 	 * Implement in subclass to perform necessary management operation on river instance. This method is called only on
// 	 * node where river really runs.
// 	 * 
// 	 * @param river instance to perform operation on, never null
// 	 * @param req request for operation to process
// 	 * @param node this operation runs on (used to construct response etc.)
// 	 * @return node response with operation result
// 	 * @throws Exception if something is wrong
// 	 */
// 	protected abstract NodeResponse performOperationOnRiver(IRiverMgm river, Request req, DiscoveryNode node)
// 			throws Exception;
// }


// Path: src/test/java/org/jboss/elasticsearch/river/sysinfo/mgm/lifecycle/TransportJRLifecycleActionTest.java
import junit.framework.Assert;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.DummyTransportAddress;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.Transport;
import org.elasticsearch.transport.TransportService;
import org.jboss.elasticsearch.river.sysinfo.IRiverMgm;
import org.jboss.elasticsearch.river.sysinfo.mgm.TransportJRMgmBaseAction;
import org.junit.Test;
import org.mockito.Mockito;

		Assert.assertNotNull(array);
		Assert.assertEquals(2, array.length);
	}

	@Test
	public void newResponse() {
		TransportJRLifecycleAction tested = prepareTestedInstance(clusterName);

		NodeJRLifecycleResponse[] array = new NodeJRLifecycleResponse[0];
		JRLifecycleResponse resp = tested.newResponse(clusterName, array);
		Assert.assertNotNull(resp);
		Assert.assertEquals(resp.getClusterName(), clusterName);
		Assert.assertEquals(resp.getNodes(), array);

	}

	@Test
	public void performOperationOnJiraRiver() throws Exception {

		TransportJRLifecycleAction tested = prepareTestedInstance(clusterName);

		IRiverMgm river = Mockito.mock(IRiverMgm.class);

		{
			JRLifecycleRequest req = new JRLifecycleRequest("myriver", JRLifecycleCommand.RESTART);
			NodeJRLifecycleResponse resp = tested.performOperationOnRiver(river, req, dn);
			Assert.assertNotNull(resp);
			Assert.assertTrue(resp.isRiverFound());
			Assert.assertEquals(dn, resp.getNode());
			Mockito.verify(river).restart();
			Mockito.verifyNoMoreInteractions(river);