Skip to content

Commit 44e4b3c

Browse files
author
Gopinath Langote
committed
iluwatar#631 - Partial Response : Implement Field to json conversion
1 parent c5b9c63 commit 44e4b3c

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

partial-response/src/main/java/com/iluwatar/partialresponse/FieldJsonMapper.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
package com.iluwatar.partialresponse;
2626

27+
import java.lang.reflect.Field;
28+
2729
/**
2830
* Map a video to json
2931
*/
@@ -34,7 +36,25 @@ public class FieldJsonMapper {
3436
* @param fields fields information to get
3537
* @return json of required fields from video
3638
*/
37-
public String toJson(Video video, String[] fields) {
38-
return null;
39+
public String toJson(Video video, String[] fields) throws Exception {
40+
StringBuilder json = new StringBuilder().append("{");
41+
42+
for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) {
43+
json.append(getString(video, Video.class.getDeclaredField(fields[i])));
44+
if (i != fieldsLength - 1) {
45+
json.append(",");
46+
}
47+
}
48+
json.append("}");
49+
return json.toString();
50+
}
51+
52+
private String getString(Video video, Field declaredField) throws IllegalAccessException {
53+
declaredField.setAccessible(true);
54+
Object value = declaredField.get(video);
55+
if (declaredField.get(video) instanceof Integer) {
56+
return "\"" + declaredField.getName() + "\"" + ": " + value;
57+
}
58+
return "\"" + declaredField.getName() + "\"" + ": " + "\"" + value.toString() + "\"";
3959
}
4060
}

partial-response/src/main/java/com/iluwatar/partialresponse/Video.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Video(Integer id, String title, Integer length, String description, Strin
5959
@Override
6060
public String toString() {
6161
return "{"
62-
+ "\"id\": \"" + id + "\","
62+
+ "\"id\": " + id + ","
6363
+ "\"title\": \"" + title + "\","
6464
+ "\"length\": " + length + ","
6565
+ "\"description\": \"" + description + "\","

partial-response/src/main/java/com/iluwatar/partialresponse/VideoResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos
4848
* @param fields fields to get information about
4949
* @return json of specified fields of particular video by id
5050
*/
51-
public String getDetails(Integer id, String... fields) {
51+
public String getDetails(Integer id, String... fields) throws Exception {
5252
if (fields.length == 0) {
5353
return videos.get(id).toString();
5454
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.partialresponse;
26+
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* tests {@link FieldJsonMapper}.
34+
*/
35+
public class FieldJsonMapperTest {
36+
private FieldJsonMapper mapper;
37+
38+
@Before
39+
public void setUp() {
40+
mapper = new FieldJsonMapper();
41+
}
42+
43+
@Test
44+
public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
45+
String[] fields = new String[]{"id", "title", "length"};
46+
Video video = new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|", "Hideaki Anno", "Japanese");
47+
48+
String jsonFieldResponse = mapper.toJson(video, fields);
49+
50+
String expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}";
51+
assertEquals(expectedDetails, jsonFieldResponse);
52+
}
53+
}

partial-response/src/test/java/com/iluwatar/partialresponse/VideoResourceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ public void setUp() {
5858
}
5959

6060
@Test
61-
public void shouldGiveVideoDetailsById() {
61+
public void shouldGiveVideoDetailsById() throws Exception {
6262
String actualDetails = resource.getDetails(1);
6363

64-
String expectedDetails = "{\"id\": \"1\",\"title\": \"Avatar\",\"length\": 178,\"description\": "
64+
String expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
6565
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
6666
assertEquals(expectedDetails, actualDetails);
6767
}
6868

6969
@Test
70-
public void shouldGiveSpecifiedFieldsInformationOfVideo() {
71-
String[] fields = new String[]{"title", "length"};
70+
public void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
71+
String[] fields = new String[]{"id", "title", "length"};
7272

73-
String expectedDetails = "{\"id\": \"1\",\"title\": \"Avatar\",\"length\": 178}";
73+
String expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}";
7474
when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
7575

7676
String actualFieldsDetails = resource.getDetails(2, fields);

0 commit comments

Comments
 (0)