Skip to content

Commit ad05dd3

Browse files
authored
Merge pull request #28 from switcherapi/premaster
Premaster
2 parents 5203144 + 0c76e2e commit ad05dd3

14 files changed

Lines changed: 432 additions & 317 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.github.switcherapi</groupId>
99
<artifactId>switcher-client</artifactId>
1010
<packaging>jar</packaging>
11-
<version>1.0.10</version>
11+
<version>1.1.0</version>
1212

1313
<name>Switcher Client</name>
1414
<description>Switcher Client for working with Switcher API</description>

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ https://github.com/switcherapi/switcher-api
2626
<dependency>
2727
<groupId>com.github.switcherapi</groupId>
2828
<artifactId>switcher-client</artifactId>
29-
<version>1.0.9</version>
29+
<version>1.1.0</version>
3030
</dependency>
3131
```
3232

@@ -148,6 +148,9 @@ switcher.isItOn(); // Now, it's going to return the result retrieved from the AP
148148
```
149149

150150
# Version Log
151+
- 1.1.0:
152+
- Improved snapshot lookup mechanism
153+
- Both online and offline modes can validate/update snapshot version
151154
- 1.0.10:
152155
- Dependency patch: Commons Net from 3.7.1 to 3.7.2
153156
- Critical Fix: Downgraded jersey-media-json-jackson 3.0.0 to 2.33

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sonar.sourceEncoding=UTF-8
44

55
sonar.projectKey=switcherapi_switcher-client
66
sonar.projectName=switcher-client-java
7-
sonar.projectVersion=1.0.6
7+
sonar.projectVersion=1.1.0
88
sonar.sources=src/main/java
99
sonar.tests=src/test/java
1010
sonar.binaries=target/

src/main/java/com/github/switcherapi/client/SwitcherFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public static Switcher getSwitcher(final String key) throws SwitcherFactoryConte
100100

101101
/**
102102
* Validate and update local snapshot file
103+
* It requires offline mode or SwitcherContextParam.SNAPSHOT_LOCATION configured
103104
*
104105
* @throws SwitcherException
105106
* If an error has occrured when invoking {@link ClientService#SNAPSHOT_URL} and {@link ClientService#SNAPSHOT_VERSION_CHECK}

src/main/java/com/github/switcherapi/client/factory/SwitcherOffline.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,10 @@ public void updateContext(final Map<String, Object> properties) throws SwitcherE
110110

111111
this.init(properties);
112112
}
113+
114+
public Domain getDomain() {
115+
116+
return domain;
117+
}
113118

114119
}

src/main/java/com/github/switcherapi/client/factory/SwitcherOnline.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.switcherapi.client.facade.ClientServiceFacade;
1111
import com.github.switcherapi.client.model.Switcher;
1212
import com.github.switcherapi.client.model.response.CriteriaResponse;
13+
import com.github.switcherapi.client.utils.SwitcherContextParam;
1314

1415
/**
1516
* @author rogerio
@@ -71,7 +72,10 @@ private CriteriaResponse executeSilentCriteria(final Switcher switcher, final Sw
7172

7273
@Override
7374
public boolean checkSnapshotVersion() throws SwitcherException {
74-
75+
76+
if (properties.containsKey(SwitcherContextParam.SNAPSHOT_LOCATION)) {
77+
return super.checkSnapshotVersion(this.switcherOffline.getDomain());
78+
}
7579
return Boolean.TRUE;
7680
}
7781

@@ -88,9 +92,9 @@ public void notifyChange(String snapshotFile) {
8892
}
8993

9094
@Override
91-
public void updateContext(Map<String, Object> properties) {
95+
public void updateContext(Map<String, Object> properties) throws SwitcherException {
9296

93-
this.properties = properties;
97+
this.init(properties);
9498
}
9599

96100
}

src/main/java/com/github/switcherapi/client/service/ClientServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ public Response resolveSnapshot(final Map<String, Object> properties) {
7070

7171
final String domain = (String) properties.get(SwitcherContextParam.DOMAIN);
7272
final String environment = (String) properties.get(SwitcherContextParam.ENVIRONMENT);
73+
final String component = (String) properties.get(SwitcherContextParam.COMPONENT);
7374

7475
final StringBuilder query = new StringBuilder();
75-
query.append("{\"query\":\"{ domain(name: \\\"%s\\\", environment: \\\"%s\\\") { ");
76+
query.append("{\"query\":\"{ domain(name: \\\"%s\\\", environment: \\\"%s\\\", _component: \\\"%s\\\") { ");
7677
query.append("name version description activated ");
7778
query.append("group { name description activated ");
7879
query.append("config { key description activated ");
@@ -83,7 +84,7 @@ public Response resolveSnapshot(final Map<String, Object> properties) {
8384

8485
return myResource.request(MediaType.APPLICATION_JSON)
8586
.header(HEADER_AUTHORIZATION, String.format(TOKEN_TEXT, ((AuthResponse) properties.get(AUTH_RESPONSE)).getToken()))
86-
.post(Entity.json(String.format(query.toString(), domain, environment)));
87+
.post(Entity.json(String.format(query.toString(), domain, environment, component)));
8788
}
8889

8990
@Override

src/test/java/com/github/switcherapi/client/SwitcherApiMockTest.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
package com.github.switcherapi.client;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
56

67
import java.io.IOException;
8+
import java.nio.file.Paths;
79
import java.util.Date;
810
import java.util.HashMap;
911
import java.util.Map;
1012

13+
import org.apache.commons.lang3.StringUtils;
1114
import org.junit.After;
1215
import org.junit.Before;
1316
import org.junit.Test;
1417
import org.junit.runner.RunWith;
1518
import org.powermock.core.classloader.annotations.PowerMockIgnore;
1619
import org.powermock.modules.junit4.PowerMockRunner;
1720

18-
import com.github.switcherapi.client.exception.SwitcherAPIConnectionException;
1921
import com.github.switcherapi.client.exception.SwitcherException;
2022
import com.github.switcherapi.client.exception.SwitcherInvalidDateTimeArgumentException;
23+
import com.github.switcherapi.client.exception.SwitcherSnapshotLoadException;
2124
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;
2228
import com.github.switcherapi.client.utils.SwitcherContextParam;
2329
import com.github.switcherapi.client.utils.SwitcherUtils;
30+
import com.google.gson.Gson;
2431

2532
import okhttp3.mockwebserver.MockResponse;
2633
import okhttp3.mockwebserver.MockWebServer;
@@ -29,6 +36,8 @@
2936
@RunWith(PowerMockRunner.class)
3037
public class SwitcherApiMockTest {
3138

39+
private final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath().toString() + "/src/test/resources";
40+
3241
private Map<String, Object> properties;
3342
private MockWebServer mockBackEnd;
3443

@@ -66,6 +75,24 @@ private MockResponse generateCriteriaResponse(String result) {
6675
.addHeader("Content-Type", "application/json");
6776
}
6877

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+
6996
@Test
7097
public void shouldReturnTrue() throws SwitcherException {
7198
//mock /auth
@@ -94,12 +121,43 @@ public void shouldReturnFalse() throws SwitcherException {
94121
assertFalse(switcher.isItOn());
95122
}
96123

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 {
100153
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+
}
103161
}
104162

105163
}

src/test/java/com/github/switcherapi/client/SwitcherOnlineTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.powermock.core.classloader.annotations.PowerMockIgnore;
3030
import org.powermock.modules.junit4.PowerMockRunner;
3131

32+
import com.github.switcherapi.client.exception.SwitcherAPIConnectionException;
3233
import com.github.switcherapi.client.exception.SwitcherException;
3334
import com.github.switcherapi.client.exception.SwitcherKeyNotAvailableForComponentException;
3435
import com.github.switcherapi.client.exception.SwitcherKeyNotFoundException;
@@ -112,6 +113,14 @@ private Switcher generateSwitcherMockTrue(final int executionStatus, final int a
112113
return switcher;
113114
}
114115

116+
@Test(expected = SwitcherAPIConnectionException.class)
117+
public void shouldReturnError_noConnection() throws Exception {
118+
properties.put(SwitcherContextParam.URL, "http://localhost:30");
119+
SwitcherFactory.buildContext(properties, false);
120+
final Switcher switcher = SwitcherFactory.getSwitcher("ONLINE_KEY");
121+
switcher.isItOn();
122+
}
123+
115124
@Test
116125
public void shouldReturnTrue() throws Exception {
117126
Switcher switcher = generateSwitcherMockTrue(200);

src/test/java/com/github/switcherapi/client/model/ModelTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.powermock.core.classloader.annotations.PowerMockIgnore;
1111
import org.powermock.modules.junit4.PowerMockRunner;
1212

13-
import com.github.switcherapi.client.model.Entry;
1413
import com.github.switcherapi.client.model.criteria.Config;
1514
import com.github.switcherapi.client.model.criteria.Criteria;
1615
import com.github.switcherapi.client.model.criteria.Domain;

0 commit comments

Comments
 (0)