-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitcherSnapshotValidationTest.java
More file actions
146 lines (112 loc) · 3.66 KB
/
SwitcherSnapshotValidationTest.java
File metadata and controls
146 lines (112 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.github.switcherapi.client;
import com.github.switcherapi.Switchers;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.github.switcherapi.fixture.MockWebServerHelper;
import mockwebserver3.QueueDispatcher;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
class SwitcherSnapshotValidationTest extends MockWebServerHelper {
private static final String RESOURCES_PATH = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources";
@BeforeAll
static void setup() throws IOException {
MockWebServerHelper.setupMockServer();
Switchers.loadProperties();
Switchers.configure(ContextBuilder.builder().url(String.format("http://localhost:%s", mockBackEnd.getPort())));
}
@AfterAll
static void tearDown() {
MockWebServerHelper.tearDownMockServer();
//clean generated outputs
SwitcherContext.stopWatchingSnapshot();
}
@BeforeEach
void resetSwitcherContextState() {
((QueueDispatcher) mockBackEnd.getDispatcher()).clear();
Switchers.configure(ContextBuilder.builder()
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
}
@Test
void shouldValidateAndUpdateSnapshot() {
//auth
givenResponse(generateMockAuth(10));
//criteria/snapshot_check
givenResponse(generateCheckSnapshotVersionResponse("false"));
//graphql
givenResponse(generateSnapshotResponse(RESOURCES_PATH));
//test
Switchers.configure(ContextBuilder.builder().snapshotLocation(RESOURCES_PATH));
assertDoesNotThrow(() -> {
Switchers.initializeClient();
assertTrue(Switchers.validateSnapshot());
});
}
@Test
void shouldValidateAndNotUpdateSnapshot() {
//auth
givenResponse(generateMockAuth(10));
//criteria/snapshot_check
givenResponse(generateCheckSnapshotVersionResponse("true"));
//test
Switchers.configure(ContextBuilder.builder().snapshotLocation(RESOURCES_PATH));
assertDoesNotThrow(() -> {
Switchers.initializeClient();
assertFalse(Switchers.validateSnapshot());
});
}
@Test
void shouldSkipValidateSnapshot() {
//given
Switchers.configure(ContextBuilder.builder().snapshotSkipValidation(true));
//test
assertDoesNotThrow(() -> {
Switchers.initializeClient();
assertFalse(Switchers.validateSnapshot());
});
}
@Test
void shouldValidateAndLoadSnapshot_whenLocal() {
//given
Switchers.configure(ContextBuilder.builder()
.local(true)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
Switchers.initializeClient();
//auth
givenResponse(generateMockAuth(10));
//criteria/snapshot_check
givenResponse(generateCheckSnapshotVersionResponse("false"));
//graphql
givenResponse(generateSnapshotResponse(RESOURCES_PATH));
//test
assertDoesNotThrow(Switchers::validateSnapshot);
}
@Test
void shouldNotValidateAndLoadSnapshot_serviceUnavailable() {
//given
Switchers.configure(ContextBuilder.builder()
.local(false)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
Switchers.initializeClient();
//auth
givenResponse(generateMockAuth(10));
//criteria/snapshot_check
givenResponse(generateStatusResponse("503"));
//test
assertThrows(SwitcherRemoteException.class, Switchers::validateSnapshot);
}
}