Skip to content

Commit a0c081f

Browse files
author
Gopinath Langote
committed
iluwatar#631 - Partial Response : Return video details as json string.
1 parent b7dbb40 commit a0c081f

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* <p>
3030
*/
3131
public class Video {
32-
private String id;
32+
private Integer id;
3333
private String title;
3434
private Integer length;
3535
private String description;
@@ -41,10 +41,10 @@ public class Video {
4141
* @param title video title
4242
* @param length video length in minutes
4343
* @param description video description by publisher
44-
* @param director video director name
45-
* @param language video language {private, public}
44+
* @param director video director name
45+
* @param language video language {private, public}
4646
*/
47-
public Video(String id, String title, Integer length, String description, String director, String language) {
47+
public Video(Integer id, String title, Integer length, String description, String director, String language) {
4848
this.id = id;
4949
this.title = title;
5050
this.length = length;
@@ -55,13 +55,12 @@ public Video(String id, String title, Integer length, String description, String
5555

5656
@Override
5757
public String toString() {
58-
return "Video{" +
59-
"id='" + id + '\'' +
60-
", title='" + title + '\'' +
61-
", length=" + length +
62-
", description='" + description + '\'' +
63-
", director='" + director + '\'' +
64-
", language='" + language + '\'' +
65-
'}';
58+
return "{" +
59+
"\"id\": \"" + id + "\"," +
60+
"\"title\": \"" + title + "\"," +
61+
"\"description\": \"" + description + "\"," +
62+
"\"director\": \"" + director + "\"," +
63+
"\"language\": \"" + language + "\"," +
64+
"}";
6665
}
6766
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import java.util.Map;
2828

2929
public class VideoResource {
30-
private Map<String, Video> videos;
30+
private Map<Integer, Video> videos;
3131

32-
public VideoResource(Map<String, Video> videos) {
32+
public VideoResource(Map<Integer, Video> videos) {
3333
this.videos = videos;
3434
}
3535

36-
public String getDetails(String id) {
36+
public String getDetails(Integer id) {
3737
return videos.get(id).toString();
3838
}
3939
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@
3030
import java.util.HashMap;
3131
import java.util.Map;
3232

33+
import static org.junit.Assert.assertEquals;
34+
3335
public class VideoResourceTest {
3436
private VideoResource resource;
3537

3638
@Before
3739
public void setUp() {
38-
Map<String, Video> videos = new HashMap<>();
39-
videos.put("1", new Video("1", "Avatar", 178, "epic science fiction film", "James Cameron", "English"));
40-
videos.put("1", new Video("2", "Godzilla Resurgence", 120, "Action & drama movie|", "Hideaki Anno", "Japanese"));
41-
videos.put("1", new Video("3", "Interstellar", 169, "Adventure & Sci-Fi", "Christopher Nolan", "English"));
40+
Map<Integer, Video> videos = new HashMap<>();
41+
videos.put(1, new Video(1, "Avatar", 178, "epic science fiction film", "James Cameron", "English"));
42+
videos.put(2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|", "Hideaki Anno", "Japanese"));
43+
videos.put(3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi", "Christopher Nolan", "English"));
4244
resource = new VideoResource(videos);
4345
}
4446

4547
@Test
4648
public void shouldGiveVideoDetailsById() {
47-
String details = resource.getDetails("1");
48-
System.out.println(details);
49+
String details = resource.getDetails(1);
50+
51+
String expectedDetails = "{\"id\": \"1\",\"title\": \"Avatar\",\"description\": \"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\",}";
52+
assertEquals(details, expectedDetails);
4953
}
5054
}

0 commit comments

Comments
 (0)