Skip to content

Commit dbd0568

Browse files
authored
Merge pull request #160 from contentstack/test/DX-3046-ngf-testcases
Add tests for nested global fields and API version header
2 parents 56df5a4 + c3a81c4 commit dbd0568

File tree

4 files changed

+289
-30
lines changed

4 files changed

+289
-30
lines changed

src/main/java/com/contentstack/cms/stack/GlobalField.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GlobalField implements BaseImplementation<GlobalField> {
3737
protected HashMap<String, Object> headers;
3838
protected HashMap<String, Object> params;
3939
protected String globalFiledUid;
40-
40+
protected String apiVersion;
4141
protected GlobalField(Retrofit retrofit,Map<String, Object> headers) {
4242
this.headers = new HashMap<>();
4343
this.headers.putAll(headers);
@@ -85,7 +85,11 @@ public GlobalField addParam(@NotNull String key, @NotNull Object value) {
8585
*/
8686
@Override
8787
public GlobalField addHeader(@NotNull String key, @NotNull String value) {
88-
this.headers.put(key, value);
88+
if ("api_version".equalsIgnoreCase(key)) {
89+
this.apiVersion = value;
90+
} else {
91+
this.headers.put(key, value);
92+
}
8993
return this;
9094
}
9195

@@ -122,6 +126,10 @@ public GlobalField addParams(@NotNull HashMap<String, Object> params) {
122126
*/
123127
@Override
124128
public GlobalField addHeaders(@NotNull HashMap<String, String> headers) {
129+
if (headers.containsKey("api_version")) {
130+
this.apiVersion = headers.get("api_version");
131+
headers.remove("api_version");
132+
}
125133
this.headers.putAll(headers);
126134
return this;
127135
}
@@ -146,6 +154,21 @@ protected GlobalField clearParams() {
146154
this.params.clear();
147155
return this;
148156
}
157+
/*
158+
* For Nested Global Fields the api_version is set to 3.2 which needs
159+
* to removed from the headers inorder for other modules to function correctly
160+
*/
161+
162+
/**
163+
* Returns a copy of the headers with api_version set if needed.
164+
*/
165+
private Map<String, Object> getRequestHeaders() {
166+
Map<String, Object> requestHeaders = new HashMap<>(this.headers);
167+
if (this.apiVersion != null) {
168+
requestHeaders.put("api_version", this.apiVersion);
169+
}
170+
return requestHeaders;
171+
}
149172

150173
/**
151174
* <b>Get All Global Fields</b>
@@ -167,11 +190,10 @@ protected GlobalField clearParams() {
167190
* </a>
168191
* @see #addHeader(String, String) to add headers
169192
* @see #addParam(String, Object) to add query parameters
170-
* @see #removeHeader(String) to remove header
171193
* @since 0.1.0
172194
*/
173195
public Call<ResponseBody> find() {
174-
return this.service.fetch(this.headers, this.params);
196+
return this.service.fetch(getRequestHeaders(), this.params);
175197
}
176198

177199
/**
@@ -197,12 +219,11 @@ public Call<ResponseBody> find() {
197219
* </a>
198220
* @see #addHeader(String, String) to add headers
199221
* @see #addParam(String, Object) to add query parameters
200-
* @see #removeHeader(String) to remove header
201222
* @since 0.1.0
202223
*/
203224
public Call<ResponseBody> fetch() {
204225
validate();
205-
return this.service.single(this.headers, this.globalFiledUid, this.params);
226+
return this.service.single(getRequestHeaders(), this.globalFiledUid, this.params);
206227
}
207228

208229
/**
@@ -230,11 +251,10 @@ public Call<ResponseBody> fetch() {
230251
*
231252
* </a>
232253
* @see #addHeader(String, String) to add headers
233-
* @see #removeHeader(String) to remove header
234254
* @since 0.1.0
235255
*/
236256
public Call<ResponseBody> create(@NotNull JSONObject requestBody) {
237-
return this.service.create(this.headers, requestBody);
257+
return this.service.create(getRequestHeaders(), requestBody);
238258
}
239259

240260
/**
@@ -260,12 +280,11 @@ public Call<ResponseBody> create(@NotNull JSONObject requestBody) {
260280
*
261281
* </a>
262282
* @see #addHeader(String, String) to add headers
263-
* @see #removeHeader(String) to remove header
264283
* @since 0.1.0
265284
*/
266285
public Call<ResponseBody> update(@NotNull JSONObject requestBody) {
267286
validate();
268-
return this.service.update(this.headers, this.globalFiledUid, requestBody);
287+
return this.service.update(getRequestHeaders(), this.globalFiledUid, requestBody);
269288
}
270289

271290
/**
@@ -286,12 +305,11 @@ public Call<ResponseBody> update(@NotNull JSONObject requestBody) {
286305
* field
287306
* </a>
288307
* @see #addHeader(String, String) to add headers
289-
* @see #removeHeader(String) to remove header
290308
* @since 0.1.0
291309
*/
292310
public Call<ResponseBody> delete() {
293311
validate();
294-
return this.service.delete(this.headers, this.globalFiledUid);
312+
return this.service.delete(getRequestHeaders(), this.globalFiledUid);
295313
}
296314

297315
/**
@@ -315,11 +333,10 @@ public Call<ResponseBody> delete() {
315333
*
316334
* </a>
317335
* @see #addHeader(String, String) to add headers
318-
* @see #removeHeader(String) to remove header
319336
* @since 0.1.0
320337
*/
321338
public Call<ResponseBody> imports(@NotNull JSONObject body) {
322-
return this.service.imports(this.headers, body);
339+
return this.service.imports(getRequestHeaders(), body);
323340
}
324341

325342
/**
@@ -336,15 +353,14 @@ public Call<ResponseBody> imports(@NotNull JSONObject body) {
336353
*
337354
* </a>
338355
* @see #addHeader(String, String) to add headers
339-
* @see #removeHeader(String) to remove header
340356
* @since 0.1.0
341357
*/
342358
public Call<ResponseBody> export() {
343359
validate();
344-
return this.service.export(this.headers, this.globalFiledUid);
360+
return this.service.export(getRequestHeaders(), this.globalFiledUid);
345361
}
346362

347-
/**
363+
/**
348364
* <b>Restore a global field </b>
349365
* <p>
350366
* The <b>Restore a global field</b> request allows you to restore the schema of
@@ -367,11 +383,10 @@ public Call<ResponseBody> export() {
367383
*
368384
* </a>
369385
* @see #addHeader(String, String) to add headers
370-
* @see #removeHeader(String) to remove header
371386
* @since 0.1.0
372387
*/
373388
public Call<ResponseBody> restore(@NotNull JSONObject requestBody) {
374389
validate();
375-
return this.service.restore(this.headers, this.globalFiledUid, requestBody);
390+
return this.service.restore(getRequestHeaders(), this.globalFiledUid, requestBody);
376391
}
377392
}

0 commit comments

Comments
 (0)