|
1 | 1 | package com.github.switcherapi.client; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertTrue; |
5 | 6 |
|
6 | 7 | import java.io.IOException; |
| 8 | +import java.nio.file.Paths; |
7 | 9 | import java.util.Date; |
8 | 10 | import java.util.HashMap; |
9 | 11 | import java.util.Map; |
10 | 12 |
|
| 13 | +import org.apache.commons.lang3.StringUtils; |
11 | 14 | import org.junit.After; |
12 | 15 | import org.junit.Before; |
13 | 16 | import org.junit.Test; |
14 | 17 | import org.junit.runner.RunWith; |
15 | 18 | import org.powermock.core.classloader.annotations.PowerMockIgnore; |
16 | 19 | import org.powermock.modules.junit4.PowerMockRunner; |
17 | 20 |
|
18 | | -import com.github.switcherapi.client.exception.SwitcherAPIConnectionException; |
19 | 21 | import com.github.switcherapi.client.exception.SwitcherException; |
20 | 22 | import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException; |
| 23 | +import com.github.switcherapi.client.exception.SwitcherSnapshotLoadException; |
21 | 24 | import com.github.switcherapi.client.model.Switcher; |
| 25 | +import com.github.switcherapi.client.model.criteria.Criteria; |
| 26 | +import com.github.switcherapi.client.model.criteria.Snapshot; |
| 27 | +import com.github.switcherapi.client.utils.SnapshotLoader; |
22 | 28 | import com.github.switcherapi.client.utils.SwitcherContextParam; |
23 | 29 | import com.github.switcherapi.client.utils.SwitcherUtils; |
| 30 | +import com.google.gson.Gson; |
24 | 31 |
|
25 | 32 | import okhttp3.mockwebserver.MockResponse; |
26 | 33 | import okhttp3.mockwebserver.MockWebServer; |
|
29 | 36 | @RunWith(PowerMockRunner.class) |
30 | 37 | public class SwitcherApiMockTest { |
31 | 38 |
|
| 39 | + private final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath().toString() + "/src/test/resources"; |
| 40 | + |
32 | 41 | private Map<String, Object> properties; |
33 | 42 | private MockWebServer mockBackEnd; |
34 | 43 |
|
@@ -66,6 +75,24 @@ private MockResponse generateCriteriaResponse(String result) { |
66 | 75 | .addHeader("Content-Type", "application/json"); |
67 | 76 | } |
68 | 77 |
|
| 78 | + private MockResponse generateCheckSnapshotVersionResponse(String status) { |
| 79 | + return new MockResponse() |
| 80 | + .setBody(String.format("{ \"status\": \"%s\" }", status)) |
| 81 | + .addHeader("Content-Type", "application/json"); |
| 82 | + } |
| 83 | + |
| 84 | + private MockResponse generateSnapshotResponse() throws SwitcherSnapshotLoadException { |
| 85 | + final Snapshot mockedSnapshot = new Snapshot(); |
| 86 | + final Criteria criteria = new Criteria(); |
| 87 | + criteria.setDomain(SnapshotLoader.loadSnapshot(SNAPSHOTS_LOCAL + "/default.json")); |
| 88 | + mockedSnapshot.setData(criteria); |
| 89 | + |
| 90 | + Gson gson = new Gson(); |
| 91 | + return new MockResponse() |
| 92 | + .setBody(gson.toJson(mockedSnapshot)) |
| 93 | + .addHeader("Content-Type", "application/json"); |
| 94 | + } |
| 95 | + |
69 | 96 | @Test |
70 | 97 | public void shouldReturnTrue() throws SwitcherException { |
71 | 98 | //mock /auth |
@@ -94,12 +121,43 @@ public void shouldReturnFalse() throws SwitcherException { |
94 | 121 | assertFalse(switcher.isItOn()); |
95 | 122 | } |
96 | 123 |
|
97 | | - @Test(expected = SwitcherAPIConnectionException.class) |
98 | | - public void shouldReturnError_noConnection() throws Exception { |
99 | | - properties.put(SwitcherContextParam.URL, "http://localhost:30"); |
| 124 | + @Test |
| 125 | + public void shouldValidateAndUpdateSnapshot() throws SwitcherException { |
| 126 | + //mock /auth |
| 127 | + mockBackEnd.enqueue(generateMockAuth("token", 10)); |
| 128 | + |
| 129 | + //mock /criteria/snapshot_check |
| 130 | + mockBackEnd.enqueue(generateCheckSnapshotVersionResponse("false")); |
| 131 | + |
| 132 | + //mock /auth isAlive |
| 133 | + mockBackEnd.enqueue(generateMockAuth("token", 10)); |
| 134 | + |
| 135 | + //mock /graphql |
| 136 | + mockBackEnd.enqueue(generateSnapshotResponse()); |
| 137 | + |
| 138 | + //test |
| 139 | + properties.put(SwitcherContextParam.SNAPSHOT_LOCATION, SNAPSHOTS_LOCAL); |
| 140 | + SwitcherFactory.buildContext(properties, false); |
| 141 | + |
| 142 | + try { |
| 143 | + SwitcherFactory.validateSnapshot(); |
| 144 | + } catch (Exception e) { |
| 145 | + System.out.println(e.getMessage()); |
| 146 | + assertEquals("Something went wrong", e.getMessage()); |
| 147 | + throw e; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void shouldNotValidateSnapshot() throws SwitcherException { |
100 | 153 | SwitcherFactory.buildContext(properties, false); |
101 | | - final Switcher switcher = SwitcherFactory.getSwitcher("ONLINE_KEY"); |
102 | | - switcher.isItOn(); |
| 154 | + |
| 155 | + try { |
| 156 | + SwitcherFactory.validateSnapshot(); |
| 157 | + } catch (Exception e) { |
| 158 | + assertEquals("Something went wrong", e.getMessage()); |
| 159 | + throw e; |
| 160 | + } |
103 | 161 | } |
104 | 162 |
|
105 | 163 | } |
0 commit comments