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