// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/event/EventHandler.java
// public interface EventHandler {
//     default Transition onTransition(ServerContext serverContext, Transition transition) {return Transition.STEADY;}
//     default Transition onCommand(ServerContext serverContext, Command command) {return Transition.STEADY;}
//     default Transition onVoteRequest(ServerContext serverContext, VoteRequest voteRequest) {return Transition.STEADY;}
//     default Transition onVoteResponse(ServerContext serverContext, VoteResponse voteResponse) {return Transition.STEADY;}
//     default Transition onAppendRequest(ServerContext serverContext, AppendRequest appendRequest) {return Transition.STEADY;}
//     default Transition onAppendResponse(ServerContext serverContext, AppendResponse appendResponse) {return Transition.STEADY;}
//     default Transition onTimeoutNow(ServerContext serverContext, TimeoutNow timeoutNow) {return Transition.STEADY;}
//     default Transition onTimerEvent(ServerContext serverContext, TimerEvent timerEvent) {return Transition.STEADY;};
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/event/HigherTermHandler.java
// public final class HigherTermHandler implements EventHandler {
// 
//     private final PersistentState persistentState;
// 
//     public HigherTermHandler(final PersistentState persistentState) {
//         this.persistentState = Objects.requireNonNull(persistentState);
//     }
// 
//     @Override
//     public Transition onVoteRequest(final ServerContext serverContext, final VoteRequest voteRequest) {
//         return onTerm(voteRequest.term());
//     }
// 
//     @Override
//     public Transition onVoteResponse(final ServerContext serverContext, final VoteResponse voteResponse) {
//         return onTerm(voteResponse.term());
//     }
// 
//     @Override
//     public Transition onAppendRequest(final ServerContext serverContext, final AppendRequest appendRequest) {
//         return onTerm(appendRequest.term());
//     }
// 
//     @Override
//     public Transition onAppendResponse(final ServerContext serverContext, final AppendResponse appendResponse) {
//         return onTerm(appendResponse.term());
//     }
// 
//     @Override
//     public Transition onTimeoutNow(final ServerContext serverContext, final TimeoutNow timeoutRequest) {
//         return onTerm(timeoutRequest.term());
//     }
// 
//     private Transition onTerm(final int term) {
//         if (term > persistentState.currentTerm()) {
//             persistentState.clearVotedForAndSetCurrentTerm(term);
//             return Transition.TO_FOLLOWER;
//         }
//         return Transition.STEADY;
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/state/PersistentState.java
// public interface PersistentState {
// 
//     int NOT_VOTED_YET = -1;
// 
//     int currentTerm();
// 
//     int votedFor();
// 
//     int clearVotedForAndSetCurrentTerm(int term);
// 
//     int clearVotedForAndIncCurrentTerm();
// 
//     void votedFor(final int candidateId);
// 
//     CommandLog commandLog();
// }


import org.tools4j.hoverraft.event.HigherTermHandler;
import org.tools4j.hoverraft.message.*;
import org.tools4j.hoverraft.state.PersistentState;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.event.EventHandler;
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.server;



@RunWith(MockitoJUnitRunner.class)
public class HigherTermHandlerTest {

    //under test
    private EventHandler handler;

    private ServerContext serverContext;
    private PersistentState persistentState;

    @Mock
    private VoteRequest voteRequest;
    @Mock
    private VoteResponse voteResponse;
    @Mock
    private AppendRequest appendRequest;
    @Mock
    private AppendResponse appendResponse;
    @Mock
    private TimeoutNow timeoutNow;
    @Mock
    private Command command;

    @Before
    public void init() {
        serverContext = Mockery.simple(1);
        persistentState = Mockery.persistentState();

        //under test
// Path: src/main/java/org/tools4j/hoverraft/command/Command.java
// public interface Command extends DirectPayload, Event {
// 
//     CommandKey commandKey();
// 
//     CommandPayload commandPayload();
// 
//     default Command sourceId(final int sourceId) {
//         commandKey().sourceId(sourceId);
//         return this;
//     }
// 
//     default Command commandIndex(final long commandIndex) {
//         commandKey().commandIndex(commandIndex);
//         return this;
//     }
// 
//     default void copyFrom(final Command command) {
//         writeBufferOrNull().putBytes(offset(), command.readBufferOrNull(), command.offset(), command.byteLength());
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onCommand(serverContext, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/event/EventHandler.java
// public interface EventHandler {
//     default Transition onTransition(ServerContext serverContext, Transition transition) {return Transition.STEADY;}
//     default Transition onCommand(ServerContext serverContext, Command command) {return Transition.STEADY;}
//     default Transition onVoteRequest(ServerContext serverContext, VoteRequest voteRequest) {return Transition.STEADY;}
//     default Transition onVoteResponse(ServerContext serverContext, VoteResponse voteResponse) {return Transition.STEADY;}
//     default Transition onAppendRequest(ServerContext serverContext, AppendRequest appendRequest) {return Transition.STEADY;}
//     default Transition onAppendResponse(ServerContext serverContext, AppendResponse appendResponse) {return Transition.STEADY;}
//     default Transition onTimeoutNow(ServerContext serverContext, TimeoutNow timeoutNow) {return Transition.STEADY;}
//     default Transition onTimerEvent(ServerContext serverContext, TimerEvent timerEvent) {return Transition.STEADY;};
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/event/HigherTermHandler.java
// public final class HigherTermHandler implements EventHandler {
// 
//     private final PersistentState persistentState;
// 
//     public HigherTermHandler(final PersistentState persistentState) {
//         this.persistentState = Objects.requireNonNull(persistentState);
//     }
// 
//     @Override
//     public Transition onVoteRequest(final ServerContext serverContext, final VoteRequest voteRequest) {
//         return onTerm(voteRequest.term());
//     }
// 
//     @Override
//     public Transition onVoteResponse(final ServerContext serverContext, final VoteResponse voteResponse) {
//         return onTerm(voteResponse.term());
//     }
// 
//     @Override
//     public Transition onAppendRequest(final ServerContext serverContext, final AppendRequest appendRequest) {
//         return onTerm(appendRequest.term());
//     }
// 
//     @Override
//     public Transition onAppendResponse(final ServerContext serverContext, final AppendResponse appendResponse) {
//         return onTerm(appendResponse.term());
//     }
// 
//     @Override
//     public Transition onTimeoutNow(final ServerContext serverContext, final TimeoutNow timeoutRequest) {
//         return onTerm(timeoutRequest.term());
//     }
// 
//     private Transition onTerm(final int term) {
//         if (term > persistentState.currentTerm()) {
//             persistentState.clearVotedForAndSetCurrentTerm(term);
//             return Transition.TO_FOLLOWER;
//         }
//         return Transition.STEADY;
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/state/PersistentState.java
// public interface PersistentState {
// 
//     int NOT_VOTED_YET = -1;
// 
//     int currentTerm();
// 
//     int votedFor();
// 
//     int clearVotedForAndSetCurrentTerm(int term);
// 
//     int clearVotedForAndIncCurrentTerm();
// 
//     void votedFor(final int candidateId);
// 
//     CommandLog commandLog();
// }


// Path: src/test/java/org/tools4j/hoverraft/server/HigherTermHandlerTest.java
import org.tools4j.hoverraft.event.HigherTermHandler;
import org.tools4j.hoverraft.message.*;
import org.tools4j.hoverraft.state.PersistentState;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.tools4j.hoverraft.command.Command;
import org.tools4j.hoverraft.event.EventHandler;

/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 hover-raft (tools4j), Marco Terzer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.tools4j.hoverraft.server;



@RunWith(MockitoJUnitRunner.class)
public class HigherTermHandlerTest {

    //under test
    private EventHandler handler;

    private ServerContext serverContext;
    private PersistentState persistentState;

    @Mock
    private VoteRequest voteRequest;
    @Mock
    private VoteResponse voteResponse;
    @Mock
    private AppendRequest appendRequest;
    @Mock
    private AppendResponse appendResponse;
    @Mock
    private TimeoutNow timeoutNow;
    @Mock
    private Command command;

    @Before
    public void init() {
        serverContext = Mockery.simple(1);
        persistentState = Mockery.persistentState();

        //under test
        handler = new HigherTermHandler(persistentState);