Add unit test for state manager.
[rhynodge.git] / src / test / java / net / pterodactylus / rhynodge / states / StateManagerTest.java
1 package net.pterodactylus.rhynodge.states;
2
3 import static com.google.common.base.Charsets.UTF_8;
4 import static com.google.common.base.Objects.equal;
5 import static org.hamcrest.MatcherAssert.assertThat;
6 import static org.hamcrest.Matchers.is;
7
8 import java.io.File;
9 import java.io.IOException;
10
11 import net.pterodactylus.rhynodge.State;
12
13 import com.fasterxml.jackson.annotation.JsonProperty;
14 import com.google.common.base.Optional;
15 import com.google.common.io.Files;
16 import org.hamcrest.Matchers;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
20
21 /**
22  * Unit test for {@link StateManager}.
23  *
24  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25  */
26 public class StateManagerTest {
27
28         @Rule
29         public TemporaryFolder tempFolder = new TemporaryFolder();
30         private final File statePath;
31         private final StateManager stateManager;
32
33         public StateManagerTest() throws IOException {
34                 tempFolder.create();
35                 statePath = tempFolder.newFolder();
36                 stateManager = new StateManager(statePath.getPath());
37         }
38
39         @Test
40         public void successStateCanBeRestored() {
41                 TestState testState = new TestState();
42                 stateManager.saveState("test", testState);
43                 Optional<State> restoredState = stateManager.loadLastState("test");
44                 assertThat(restoredState.get(), Matchers.<State>is(testState));
45                 restoredState = stateManager.loadLastSuccessfulState("test");
46                 assertThat(restoredState.get(), Matchers.<State>is(testState));
47         }
48
49         @Test
50         public void failStateIsNotSavedAsSuccessfulState() {
51                 TestState testState = new TestState(false);
52                 stateManager.saveState("test", testState);
53                 Optional<State> restoredState = stateManager.loadLastState("test");
54                 assertThat(restoredState.get(), Matchers.<State>is(testState));
55                 restoredState = stateManager.loadLastSuccessfulState("test");
56                 assertThat(restoredState.isPresent(), is(false));
57         }
58
59         @Test
60         public void invalidJsonFileCanNotBeLoaded() throws IOException {
61                 Files.write("not json", new File(statePath, "test.last.json"), UTF_8);
62                 Optional<State> restoredState = stateManager.loadLastState("test");
63                 assertThat(restoredState.isPresent(), is(false));
64         }
65
66         @Test
67         public void jsonWithInvalidFieldsCanNotBeLoaded() throws IOException {
68                 Files.write("{\"not\":\"json\"}", new File(statePath, "test.last.json"), UTF_8);
69                 Optional<State> restoredState = stateManager.loadLastState("test");
70                 assertThat(restoredState.isPresent(), is(false));
71         }
72
73         @Test
74         public void unmappableStateCanBeSavedButNotLoaded() throws IOException {
75                 InvalidState invalidState = new InvalidState();
76                 stateManager.saveState("test", invalidState);
77                 assertThat(new File(statePath, "test.last.json").exists(), is(false));
78         }
79
80         public static class TestState extends AbstractState {
81
82                 public TestState() {
83                         this(true);
84                 }
85
86                 public TestState(boolean success) {
87                         super(success);
88                 }
89
90                 @Override
91                 public boolean equals(Object object) {
92                         if (!(object instanceof TestState)) {
93                                 return false;
94                         }
95                         TestState testState = (TestState) object;
96                         return equal(exception(), testState.exception())
97                                         && (failCount() == testState.failCount())
98                                         && (time() == testState.time());
99                 }
100
101         }
102
103         public static class InvalidState extends AbstractState {
104
105                 @JsonProperty
106                 private final Object someObject = new Object();
107
108         }
109
110 }