-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientRemoteTest.java
More file actions
127 lines (104 loc) · 4.95 KB
/
ClientRemoteTest.java
File metadata and controls
127 lines (104 loc) · 4.95 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
package com.github.switcherapi.client.remote;
import com.github.switcherapi.Switchers;
import com.github.switcherapi.client.ContextBuilder;
import com.github.switcherapi.client.SwitcherContext;
import com.github.switcherapi.client.SwitcherProperties;
import com.github.switcherapi.client.exception.SwitchersValidationException;
import com.github.switcherapi.client.model.SwitcherRequest;
import com.github.switcherapi.client.model.SwitcherResult;
import com.github.switcherapi.client.remote.dto.SwitchersCheck;
import com.github.switcherapi.client.service.SwitcherValidator;
import com.github.switcherapi.client.service.ValidatorService;
import com.github.switcherapi.client.service.local.ClientLocal;
import com.github.switcherapi.client.service.local.ClientLocalService;
import com.github.switcherapi.client.service.local.SwitcherLocalService;
import com.github.switcherapi.client.service.remote.ClientRemote;
import com.github.switcherapi.client.service.remote.ClientRemoteService;
import com.github.switcherapi.client.service.remote.SwitcherRemoteService;
import com.github.switcherapi.client.utils.Mapper;
import com.github.switcherapi.fixture.MockWebServerHelper;
import mockwebserver3.QueueDispatcher;
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.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static com.github.switcherapi.client.remote.Constants.DEFAULT_TIMEOUT;
import static org.junit.jupiter.api.Assertions.*;
class ClientRemoteTest extends MockWebServerHelper {
private static ExecutorService executorService;
private ClientRemote clientRemote;
@BeforeAll
static void setup() throws IOException {
executorService = Executors.newSingleThreadExecutor();
MockWebServerHelper.setupMockServer();
Switchers.loadProperties();
Switchers.configure(ContextBuilder.builder().url(String.format("http://localhost:%s", mockBackEnd.getPort())));
Switchers.initializeClient();
}
@AfterAll
static void tearDown() throws IOException {
MockWebServerHelper.tearDownMockServer();
executorService.shutdown();
}
@BeforeEach
void resetSwitcherContextState() {
SwitcherProperties switcherProperties = Switchers.getSwitcherProperties();
clientRemote = new ClientRemoteService(ClientWSImpl.build(switcherProperties, executorService, DEFAULT_TIMEOUT), switcherProperties);
((QueueDispatcher) mockBackEnd.getDispatcher()).clear();
Switchers.configure(ContextBuilder.builder().checkSwitchers(false));
Switchers.initializeClient();
}
@Test
void shouldExecuteCriteria() {
//given
givenResponse(generateMockAuth(100));
givenResponse(generateCriteriaResponse("true", false));
SwitcherProperties switcherProperties = Switchers.getSwitcherProperties();
SwitcherValidator validatorService = new ValidatorService();
ClientLocal clientLocal = new ClientLocalService(validatorService);
SwitcherRequest switcher = new SwitcherRequest(
"KEY",
new SwitcherRemoteService(clientRemote, new SwitcherLocalService(clientRemote, clientLocal, switcherProperties)),
switcherProperties);
//test
SwitcherResult actual = Mapper.mapFrom(clientRemote.executeCriteria(Mapper.mapFrom(switcher)));
assertTrue(actual.isItOn());
}
@Test
void shouldCheckSwitchersError() {
//given
final Set<String> switcherKeys = new HashSet<>();
switcherKeys.add("KEY");
givenResponse(generateMockAuth(100));
givenResponse(generateCheckSwitchersResponse(switcherKeys));
//test
SwitchersCheck actual = clientRemote.checkSwitchers(switcherKeys);
assertEquals(1, actual.getNotFound().length);
}
@Test
void shouldCheckSwitchersError_throughContextConfiguration() {
//given
final Set<String> switcherKeys = new HashSet<>();
switcherKeys.add("KEY");
givenResponse(generateMockAuth(100));
givenResponse(generateCheckSwitchersResponse(switcherKeys));
//test
Switchers.configure(ContextBuilder.builder().checkSwitchers(true));
SwitchersValidationException exception = assertThrows(SwitchersValidationException.class, SwitcherContext::initializeClient);
assertEquals("Something went wrong: Unable to load the following Switcher Key(s): [KEY]", exception.getMessage());
}
@Test
void shouldCheckSwitchersSuccess_throughContextConfiguration() {
//given
givenResponse(generateMockAuth(100));
givenResponse(generateCheckSwitchersResponse(new HashSet<>()));
//test
Switchers.configure(ContextBuilder.builder().checkSwitchers(true));
assertDoesNotThrow(SwitcherContext::initializeClient);
}
}