mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-14 18:23:03 +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,6 +34,14 @@
|
||||
</parent>
|
||||
<artifactId>abstract-document</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
|
||||
+7
-10
@@ -31,9 +31,7 @@ import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Abstract implementation of Document interface.
|
||||
*/
|
||||
/** Abstract implementation of Document interface. */
|
||||
public abstract class AbstractDocument implements Document {
|
||||
|
||||
private final Map<String, Object> documentProperties;
|
||||
@@ -57,12 +55,12 @@ public abstract class AbstractDocument implements Document {
|
||||
@Override
|
||||
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> childConstructor) {
|
||||
return Stream.ofNullable(get(key))
|
||||
.filter(Objects::nonNull)
|
||||
.map(el -> (List<Map<String, Object>>) el)
|
||||
.findAny()
|
||||
.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.map(childConstructor);
|
||||
.filter(Objects::nonNull)
|
||||
.map(el -> (List<Map<String, Object>>) el)
|
||||
.findAny()
|
||||
.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.map(childConstructor);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,5 +98,4 @@ public abstract class AbstractDocument implements Document {
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,20 +49,26 @@ public class App {
|
||||
public static void main(String[] args) {
|
||||
LOGGER.info("Constructing parts and car");
|
||||
|
||||
var wheelProperties = Map.of(
|
||||
Property.TYPE.toString(), "wheel",
|
||||
Property.MODEL.toString(), "15C",
|
||||
Property.PRICE.toString(), 100L);
|
||||
var wheelProperties =
|
||||
Map.of(
|
||||
Property.TYPE.toString(), "wheel",
|
||||
Property.MODEL.toString(), "15C",
|
||||
Property.PRICE.toString(), 100L);
|
||||
|
||||
var doorProperties = Map.of(
|
||||
Property.TYPE.toString(), "door",
|
||||
Property.MODEL.toString(), "Lambo",
|
||||
Property.PRICE.toString(), 300L);
|
||||
var doorProperties =
|
||||
Map.of(
|
||||
Property.TYPE.toString(), "door",
|
||||
Property.MODEL.toString(), "Lambo",
|
||||
Property.PRICE.toString(), 300L);
|
||||
|
||||
var carProperties = Map.of(
|
||||
Property.MODEL.toString(), "300SL",
|
||||
Property.PRICE.toString(), 10000L,
|
||||
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
|
||||
var carProperties =
|
||||
Map.of(
|
||||
Property.MODEL.toString(),
|
||||
"300SL",
|
||||
Property.PRICE.toString(),
|
||||
10000L,
|
||||
Property.PARTS.toString(),
|
||||
List.of(wheelProperties, doorProperties));
|
||||
|
||||
var car = new Car(carProperties);
|
||||
|
||||
@@ -70,10 +76,13 @@ public class App {
|
||||
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
|
||||
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
|
||||
LOGGER.info("-> parts: ");
|
||||
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
|
||||
p.getType().orElse(null),
|
||||
p.getModel().orElse(null),
|
||||
p.getPrice().orElse(null))
|
||||
);
|
||||
car.getParts()
|
||||
.forEach(
|
||||
p ->
|
||||
LOGGER.info(
|
||||
"\t{}/{}/{}",
|
||||
p.getType().orElse(null),
|
||||
p.getModel().orElse(null),
|
||||
p.getPrice().orElse(null)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,13 @@ import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Document interface.
|
||||
*/
|
||||
/** Document interface. */
|
||||
public interface Document {
|
||||
|
||||
/**
|
||||
* Puts the value related to the key.
|
||||
*
|
||||
* @param key element key
|
||||
* @param key element key
|
||||
* @param value element value
|
||||
* @return Void
|
||||
*/
|
||||
@@ -53,7 +51,7 @@ public interface Document {
|
||||
/**
|
||||
* Gets the stream of child documents.
|
||||
*
|
||||
* @param key element key
|
||||
* @param key element key
|
||||
* @param constructor constructor of child class
|
||||
* @return child documents
|
||||
*/
|
||||
|
||||
@@ -27,13 +27,10 @@ package com.iluwatar.abstractdocument.domain;
|
||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Car entity.
|
||||
*/
|
||||
/** Car entity. */
|
||||
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
|
||||
|
||||
public Car(Map<String, Object> properties) {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,13 +28,10 @@ import com.iluwatar.abstractdocument.Document;
|
||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* HasModel trait for static access to 'model' property.
|
||||
*/
|
||||
/** HasModel trait for static access to 'model' property. */
|
||||
public interface HasModel extends Document {
|
||||
|
||||
default Optional<String> getModel() {
|
||||
return Optional.ofNullable((String) get(Property.MODEL.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,13 +28,10 @@ import com.iluwatar.abstractdocument.Document;
|
||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* HasParts trait for static access to 'parts' property.
|
||||
*/
|
||||
/** HasParts trait for static access to 'parts' property. */
|
||||
public interface HasParts extends Document {
|
||||
|
||||
default Stream<Part> getParts() {
|
||||
return children(Property.PARTS.toString(), Part::new);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,13 +28,10 @@ import com.iluwatar.abstractdocument.Document;
|
||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* HasPrice trait for static access to 'price' property.
|
||||
*/
|
||||
/** HasPrice trait for static access to 'price' property. */
|
||||
public interface HasPrice extends Document {
|
||||
|
||||
default Optional<Number> getPrice() {
|
||||
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,13 +28,10 @@ import com.iluwatar.abstractdocument.Document;
|
||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* HasType trait for static access to 'type' property.
|
||||
*/
|
||||
/** HasType trait for static access to 'type' property. */
|
||||
public interface HasType extends Document {
|
||||
|
||||
default Optional<String> getType() {
|
||||
return Optional.ofNullable((String) get(Property.TYPE.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,13 +27,10 @@ package com.iluwatar.abstractdocument.domain;
|
||||
import com.iluwatar.abstractdocument.AbstractDocument;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Part entity.
|
||||
*/
|
||||
/** Part entity. */
|
||||
public class Part extends AbstractDocument implements HasType, HasModel, HasPrice {
|
||||
|
||||
public Part(Map<String, Object> properties) {
|
||||
super(properties);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-5
@@ -24,10 +24,10 @@
|
||||
*/
|
||||
package com.iluwatar.abstractdocument.domain.enums;
|
||||
|
||||
/**
|
||||
* Enum To Describe Property type.
|
||||
*/
|
||||
/** Enum To Describe Property type. */
|
||||
public enum Property {
|
||||
|
||||
PARTS, TYPE, PRICE, MODEL
|
||||
PARTS,
|
||||
TYPE,
|
||||
PRICE,
|
||||
MODEL
|
||||
}
|
||||
|
||||
+14
-13
@@ -24,16 +24,14 @@
|
||||
*/
|
||||
package com.iluwatar.abstractdocument;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* AbstractDocument test class
|
||||
*/
|
||||
/** AbstractDocument test class */
|
||||
class AbstractDocumentTest {
|
||||
|
||||
private static final String KEY = "key";
|
||||
@@ -82,13 +80,16 @@ class AbstractDocumentTest {
|
||||
|
||||
@Test
|
||||
void shouldHandleExceptionDuringConstruction() {
|
||||
Map<String, Object> invalidProperties = null; // Invalid properties, causing NullPointerException
|
||||
Map<String, Object> invalidProperties =
|
||||
null; // Invalid properties, causing NullPointerException
|
||||
|
||||
// Throw null pointer exception
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
// Attempt to construct a document with invalid properties
|
||||
new DocumentImplementation(invalidProperties);
|
||||
});
|
||||
assertThrows(
|
||||
NullPointerException.class,
|
||||
() -> {
|
||||
// Attempt to construct a document with invalid properties
|
||||
new DocumentImplementation(invalidProperties);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,11 +98,11 @@ class AbstractDocumentTest {
|
||||
DocumentImplementation nestedDocument = new DocumentImplementation(new HashMap<>());
|
||||
nestedDocument.put("nestedKey", "nestedValue");
|
||||
|
||||
|
||||
document.put("nested", nestedDocument);
|
||||
|
||||
// Retrieving the nested document
|
||||
DocumentImplementation retrievedNestedDocument = (DocumentImplementation) document.get("nested");
|
||||
DocumentImplementation retrievedNestedDocument =
|
||||
(DocumentImplementation) document.get("nested");
|
||||
|
||||
assertNotNull(retrievedNestedDocument);
|
||||
assertEquals("nestedValue", retrievedNestedDocument.get("nestedKey"));
|
||||
|
||||
@@ -24,25 +24,21 @@
|
||||
*/
|
||||
package com.iluwatar.abstractdocument;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Simple App test
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Simple App test */
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
* <p>Solution: Inserted assertion to check whether the execution of the main method in {@link
|
||||
* App} throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void shouldExecuteAppWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,18 +24,16 @@
|
||||
*/
|
||||
package com.iluwatar.abstractdocument;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.iluwatar.abstractdocument.domain.Car;
|
||||
import com.iluwatar.abstractdocument.domain.Part;
|
||||
import com.iluwatar.abstractdocument.domain.enums.Property;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Test for Part and Car
|
||||
*/
|
||||
/** Test for Part and Car */
|
||||
class DomainTest {
|
||||
|
||||
private static final String TEST_PART_TYPE = "test-part-type";
|
||||
@@ -47,11 +45,11 @@ class DomainTest {
|
||||
|
||||
@Test
|
||||
void shouldConstructPart() {
|
||||
var partProperties = Map.of(
|
||||
Property.TYPE.toString(), TEST_PART_TYPE,
|
||||
Property.MODEL.toString(), TEST_PART_MODEL,
|
||||
Property.PRICE.toString(), (Object) TEST_PART_PRICE
|
||||
);
|
||||
var partProperties =
|
||||
Map.of(
|
||||
Property.TYPE.toString(), TEST_PART_TYPE,
|
||||
Property.MODEL.toString(), TEST_PART_MODEL,
|
||||
Property.PRICE.toString(), (Object) TEST_PART_PRICE);
|
||||
var part = new Part(partProperties);
|
||||
assertEquals(TEST_PART_TYPE, part.getType().orElseThrow());
|
||||
assertEquals(TEST_PART_MODEL, part.getModel().orElseThrow());
|
||||
@@ -60,15 +58,14 @@ class DomainTest {
|
||||
|
||||
@Test
|
||||
void shouldConstructCar() {
|
||||
var carProperties = Map.of(
|
||||
Property.MODEL.toString(), TEST_CAR_MODEL,
|
||||
Property.PRICE.toString(), TEST_CAR_PRICE,
|
||||
Property.PARTS.toString(), List.of(Map.of(), Map.of())
|
||||
);
|
||||
var carProperties =
|
||||
Map.of(
|
||||
Property.MODEL.toString(), TEST_CAR_MODEL,
|
||||
Property.PRICE.toString(), TEST_CAR_PRICE,
|
||||
Property.PARTS.toString(), List.of(Map.of(), Map.of()));
|
||||
var car = new Car(carProperties);
|
||||
assertEquals(TEST_CAR_MODEL, car.getModel().orElseThrow());
|
||||
assertEquals(TEST_CAR_PRICE, car.getPrice().orElseThrow());
|
||||
assertEquals(2, car.getParts().count());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user