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