mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-23 20:26:50 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -34,7 +34,6 @@ import lombok.extern.slf4j.Slf4j;
|
||||
*
|
||||
* <p>{@link VideoResource} act as server to serve video information.
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
@@ -44,17 +43,22 @@ public class App {
|
||||
* @param args program argument.
|
||||
*/
|
||||
public static void main(String[] args) throws Exception {
|
||||
var videos = Map.of(
|
||||
1, new Video(1, "Avatar", 178, "epic science fiction film",
|
||||
"James Cameron", "English"),
|
||||
2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|",
|
||||
"Hideaki Anno", "Japanese"),
|
||||
3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi",
|
||||
"Christopher Nolan", "English")
|
||||
);
|
||||
var videos =
|
||||
Map.of(
|
||||
1, new Video(1, "Avatar", 178, "epic science fiction film", "James Cameron", "English"),
|
||||
2,
|
||||
new Video(
|
||||
2,
|
||||
"Godzilla Resurgence",
|
||||
120,
|
||||
"Action & drama movie|",
|
||||
"Hideaki Anno",
|
||||
"Japanese"),
|
||||
3,
|
||||
new Video(
|
||||
3, "Interstellar", 169, "Adventure & Sci-Fi", "Christopher Nolan", "English"));
|
||||
var videoResource = new VideoResource(new FieldJsonMapper(), videos);
|
||||
|
||||
|
||||
LOGGER.info("Retrieving full response from server:-");
|
||||
LOGGER.info("Get all video information:");
|
||||
var videoDetails = videoResource.getDetails(1);
|
||||
|
||||
@@ -27,15 +27,13 @@ package com.iluwatar.partialresponse;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* Map a video to json.
|
||||
*/
|
||||
/** Map a video to json. */
|
||||
public class FieldJsonMapper {
|
||||
|
||||
/**
|
||||
* Gets json of required fields from video.
|
||||
*
|
||||
* @param video object containing video information
|
||||
* @param video object containing video information
|
||||
* @param fields fields information to get
|
||||
* @return json of required fields from video
|
||||
*/
|
||||
|
||||
@@ -25,11 +25,16 @@
|
||||
package com.iluwatar.partialresponse;
|
||||
|
||||
/**
|
||||
* {@link Video} is an entity to serve from server.It contains all video related information.
|
||||
* Video is a record class.
|
||||
* {@link Video} is an entity to serve from server.It contains all video related information. Video
|
||||
* is a record class.
|
||||
*/
|
||||
|
||||
public record Video(Integer id, String title, Integer length, String description, String director, String language) {
|
||||
public record Video(
|
||||
Integer id,
|
||||
String title,
|
||||
Integer length,
|
||||
String description,
|
||||
String director,
|
||||
String language) {
|
||||
/**
|
||||
* ToString.
|
||||
*
|
||||
@@ -38,12 +43,24 @@ public record Video(Integer id, String title, Integer length, String description
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{"
|
||||
+ "\"id\": " + id + ","
|
||||
+ "\"title\": \"" + title + "\","
|
||||
+ "\"length\": " + length + ","
|
||||
+ "\"description\": \"" + description + "\","
|
||||
+ "\"director\": \"" + director + "\","
|
||||
+ "\"language\": \"" + language + "\""
|
||||
+ "}";
|
||||
+ "\"id\": "
|
||||
+ id
|
||||
+ ","
|
||||
+ "\"title\": \""
|
||||
+ title
|
||||
+ "\","
|
||||
+ "\"length\": "
|
||||
+ length
|
||||
+ ","
|
||||
+ "\"description\": \""
|
||||
+ description
|
||||
+ "\","
|
||||
+ "\"director\": \""
|
||||
+ director
|
||||
+ "\","
|
||||
+ "\"language\": \""
|
||||
+ language
|
||||
+ "\""
|
||||
+ "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,18 +27,17 @@ package com.iluwatar.partialresponse;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The resource record class which serves video information. This class act as server in the demo. Which
|
||||
* has all video details.
|
||||
* The resource record class which serves video information. This class act as server in the demo.
|
||||
* Which has all video details.
|
||||
*
|
||||
* @param fieldJsonMapper map object to json.
|
||||
* @param videos initialize resource with existing videos. Act as database.
|
||||
* @param videos initialize resource with existing videos. Act as database.
|
||||
*/
|
||||
|
||||
public record VideoResource(FieldJsonMapper fieldJsonMapper, Map<Integer, Video> videos) {
|
||||
/**
|
||||
* Get Details.
|
||||
*
|
||||
* @param id video id
|
||||
* @param id video id
|
||||
* @param fields fields to get information about
|
||||
* @return full response if no fields specified else partial response for given field.
|
||||
*/
|
||||
|
||||
@@ -24,17 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.partialresponse;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
/** Application test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
Assertions.assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
Assertions.assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
+7
-10
@@ -24,13 +24,11 @@
|
||||
*/
|
||||
package com.iluwatar.partialresponse;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* tests {@link FieldJsonMapper}.
|
||||
*/
|
||||
/** tests {@link FieldJsonMapper}. */
|
||||
class FieldJsonMapperTest {
|
||||
private static FieldJsonMapper mapper;
|
||||
|
||||
@@ -41,15 +39,14 @@ class FieldJsonMapperTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception {
|
||||
var fields = new String[]{"id", "title", "length"};
|
||||
var video = new Video(
|
||||
2, "Godzilla Resurgence", 120,
|
||||
"Action & drama movie|", "Hideaki Anno", "Japanese"
|
||||
);
|
||||
var fields = new String[] {"id", "title", "length"};
|
||||
var video =
|
||||
new Video(
|
||||
2, "Godzilla Resurgence", 120, "Action & drama movie|", "Hideaki Anno", "Japanese");
|
||||
|
||||
var jsonFieldResponse = mapper.toJson(video, fields);
|
||||
|
||||
var expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}";
|
||||
Assertions.assertEquals(expectedDetails, jsonFieldResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+24
-18
@@ -36,25 +36,29 @@ import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
/**
|
||||
* tests {@link VideoResource}.
|
||||
*/
|
||||
/** tests {@link VideoResource}. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class VideoResourceTest {
|
||||
@Mock
|
||||
private static FieldJsonMapper fieldJsonMapper;
|
||||
@Mock private static FieldJsonMapper fieldJsonMapper;
|
||||
|
||||
private static VideoResource resource;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
var videos = Map.of(
|
||||
1, new Video(1, "Avatar", 178, "epic science fiction film",
|
||||
"James Cameron", "English"),
|
||||
2, new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|",
|
||||
"Hideaki Anno", "Japanese"),
|
||||
3, new Video(3, "Interstellar", 169, "Adventure & Sci-Fi",
|
||||
"Christopher Nolan", "English"));
|
||||
var videos =
|
||||
Map.of(
|
||||
1, new Video(1, "Avatar", 178, "epic science fiction film", "James Cameron", "English"),
|
||||
2,
|
||||
new Video(
|
||||
2,
|
||||
"Godzilla Resurgence",
|
||||
120,
|
||||
"Action & drama movie|",
|
||||
"Hideaki Anno",
|
||||
"Japanese"),
|
||||
3,
|
||||
new Video(
|
||||
3, "Interstellar", 169, "Adventure & Sci-Fi", "Christopher Nolan", "English"));
|
||||
resource = new VideoResource(fieldJsonMapper, videos);
|
||||
}
|
||||
|
||||
@@ -62,14 +66,15 @@ class VideoResourceTest {
|
||||
void shouldGiveVideoDetailsById() throws Exception {
|
||||
var actualDetails = resource.getDetails(1);
|
||||
|
||||
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||
var expectedDetails =
|
||||
"{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||
Assertions.assertEquals(expectedDetails, actualDetails);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGiveSpecifiedFieldsInformationOfVideo() throws Exception {
|
||||
var fields = new String[]{"id", "title", "length"};
|
||||
var fields = new String[] {"id", "title", "length"};
|
||||
|
||||
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178}";
|
||||
Mockito.when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
|
||||
@@ -81,10 +86,11 @@ class VideoResourceTest {
|
||||
|
||||
@Test
|
||||
void shouldAllSpecifiedFieldsInformationOfVideo() throws Exception {
|
||||
var fields = new String[]{"id", "title", "length", "description", "director", "language"};
|
||||
var fields = new String[] {"id", "title", "length", "description", "director", "language"};
|
||||
|
||||
var expectedDetails = "{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||
var expectedDetails =
|
||||
"{\"id\": 1,\"title\": \"Avatar\",\"length\": 178,\"description\": "
|
||||
+ "\"epic science fiction film\",\"director\": \"James Cameron\",\"language\": \"English\"}";
|
||||
Mockito.when(fieldJsonMapper.toJson(any(Video.class), eq(fields))).thenReturn(expectedDetails);
|
||||
|
||||
var actualFieldsDetails = resource.getDetails(1, fields);
|
||||
|
||||
Reference in New Issue
Block a user