mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-18 06:26:18 +00:00
refactor: Changes to make Abstract-Document and Adapter better. (#2872)
* Added new test case for error handling * Added new test cases for error handling * Refactored Abstract Document * Changes updated
This commit is contained in:
+39
-13
@@ -36,41 +36,67 @@ import java.util.stream.Stream;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractDocument implements Document {
|
public abstract class AbstractDocument implements Document {
|
||||||
|
|
||||||
private final Map<String, Object> properties;
|
private final Map<String, Object> documentProperties;
|
||||||
|
|
||||||
protected AbstractDocument(Map<String, Object> properties) {
|
protected AbstractDocument(Map<String, Object> properties) {
|
||||||
Objects.requireNonNull(properties, "properties map is required");
|
Objects.requireNonNull(properties, "properties map is required");
|
||||||
this.properties = properties;
|
this.documentProperties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void put(String key, Object value) {
|
public Void put(String key, Object value) {
|
||||||
properties.put(key, value);
|
documentProperties.put(key, value);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object get(String key) {
|
public Object get(String key) {
|
||||||
return properties.get(key);
|
return documentProperties.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
|
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> childConstructor) {
|
||||||
return Stream.ofNullable(get(key))
|
return Stream.ofNullable(get(key))
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.map(el -> (List<Map<String, Object>>) el)
|
.map(el -> (List<Map<String, Object>>) el)
|
||||||
.findAny()
|
.findAny()
|
||||||
.stream()
|
.stream()
|
||||||
.flatMap(Collection::stream)
|
.flatMap(Collection::stream)
|
||||||
.map(constructor);
|
.map(childConstructor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
return buildStringRepresentation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String buildStringRepresentation() {
|
||||||
var builder = new StringBuilder();
|
var builder = new StringBuilder();
|
||||||
builder.append(getClass().getName()).append("[");
|
builder.append(getClass().getName()).append("[");
|
||||||
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
|
|
||||||
.append("]"));
|
// Explaining variable for document properties map
|
||||||
|
Map<String, Object> documentProperties = this.documentProperties;
|
||||||
|
|
||||||
|
// Explaining variable for the size of document properties map
|
||||||
|
int numProperties = documentProperties.size();
|
||||||
|
|
||||||
|
// Explaining variable for tracking the current property index
|
||||||
|
int currentPropertyIndex = 0;
|
||||||
|
|
||||||
|
// Iterate over document properties map
|
||||||
|
for (Map.Entry<String, Object> entry : documentProperties.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
Object value = entry.getValue();
|
||||||
|
|
||||||
|
// Append key-value pair
|
||||||
|
builder.append("[").append(key).append(" : ").append(value).append("]");
|
||||||
|
|
||||||
|
// Add comma if not last property
|
||||||
|
if (++currentPropertyIndex < numProperties) {
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
builder.append("]");
|
builder.append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
+42
@@ -80,4 +80,46 @@ class AbstractDocumentTest {
|
|||||||
assertTrue(document.toString().contains(VALUE));
|
assertTrue(document.toString().contains(VALUE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldHandleExceptionDuringConstruction() {
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldPutAndGetNestedDocument() {
|
||||||
|
// Creating a nested document
|
||||||
|
DocumentImplementation nestedDocument = new DocumentImplementation(new HashMap<>());
|
||||||
|
nestedDocument.put("nestedKey", "nestedValue");
|
||||||
|
|
||||||
|
|
||||||
|
document.put("nested", nestedDocument);
|
||||||
|
|
||||||
|
// Retrieving the nested document
|
||||||
|
DocumentImplementation retrievedNestedDocument = (DocumentImplementation) document.get("nested");
|
||||||
|
|
||||||
|
assertNotNull(retrievedNestedDocument);
|
||||||
|
assertEquals("nestedValue", retrievedNestedDocument.get("nestedKey"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldUpdateExistingValue() {
|
||||||
|
// Arrange
|
||||||
|
final String key = "key";
|
||||||
|
final String originalValue = "originalValue";
|
||||||
|
final String updatedValue = "updatedValue";
|
||||||
|
|
||||||
|
document.put(key, originalValue);
|
||||||
|
|
||||||
|
// Updating the value
|
||||||
|
document.put(key, updatedValue);
|
||||||
|
|
||||||
|
//Verifying that the updated value is retrieved correctly
|
||||||
|
assertEquals(updatedValue, document.get(key));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user