Skip to content

Commit 44a9766

Browse files
authored
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
1 parent d3e7401 commit 44a9766

File tree

2 files changed

+81
-13
lines changed

2 files changed

+81
-13
lines changed

Diff for: abstract-document/src/main/java/com/iluwatar/abstractdocument/AbstractDocument.java

+39-13
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,67 @@
3636
*/
3737
public abstract class AbstractDocument implements Document {
3838

39-
private final Map<String, Object> properties;
39+
private final Map<String, Object> documentProperties;
4040

4141
protected AbstractDocument(Map<String, Object> properties) {
4242
Objects.requireNonNull(properties, "properties map is required");
43-
this.properties = properties;
43+
this.documentProperties = properties;
4444
}
4545

4646
@Override
4747
public Void put(String key, Object value) {
48-
properties.put(key, value);
48+
documentProperties.put(key, value);
4949
return null;
5050
}
5151

5252
@Override
5353
public Object get(String key) {
54-
return properties.get(key);
54+
return documentProperties.get(key);
5555
}
5656

5757
@Override
58-
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
58+
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> childConstructor) {
5959
return Stream.ofNullable(get(key))
60-
.filter(Objects::nonNull)
61-
.map(el -> (List<Map<String, Object>>) el)
62-
.findAny()
63-
.stream()
64-
.flatMap(Collection::stream)
65-
.map(constructor);
60+
.filter(Objects::nonNull)
61+
.map(el -> (List<Map<String, Object>>) el)
62+
.findAny()
63+
.stream()
64+
.flatMap(Collection::stream)
65+
.map(childConstructor);
6666
}
6767

6868
@Override
6969
public String toString() {
70+
return buildStringRepresentation();
71+
}
72+
73+
private String buildStringRepresentation() {
7074
var builder = new StringBuilder();
7175
builder.append(getClass().getName()).append("[");
72-
properties.forEach((key, value) -> builder.append("[").append(key).append(" : ").append(value)
73-
.append("]"));
76+
77+
// Explaining variable for document properties map
78+
Map<String, Object> documentProperties = this.documentProperties;
79+
80+
// Explaining variable for the size of document properties map
81+
int numProperties = documentProperties.size();
82+
83+
// Explaining variable for tracking the current property index
84+
int currentPropertyIndex = 0;
85+
86+
// Iterate over document properties map
87+
for (Map.Entry<String, Object> entry : documentProperties.entrySet()) {
88+
String key = entry.getKey();
89+
Object value = entry.getValue();
90+
91+
// Append key-value pair
92+
builder.append("[").append(key).append(" : ").append(value).append("]");
93+
94+
// Add comma if not last property
95+
if (++currentPropertyIndex < numProperties) {
96+
builder.append(", ");
97+
}
98+
}
99+
74100
builder.append("]");
75101
return builder.toString();
76102
}

Diff for: abstract-document/src/test/java/com/iluwatar/abstractdocument/AbstractDocumentTest.java

+42
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,46 @@ void shouldIncludePropsInToString() {
8080
assertTrue(document.toString().contains(VALUE));
8181
}
8282

83+
@Test
84+
void shouldHandleExceptionDuringConstruction() {
85+
Map<String, Object> invalidProperties = null; // Invalid properties, causing NullPointerException
86+
87+
// Throw null pointer exception
88+
assertThrows(NullPointerException.class, () -> {
89+
// Attempt to construct a document with invalid properties
90+
new DocumentImplementation(invalidProperties);
91+
});
92+
}
93+
94+
@Test
95+
void shouldPutAndGetNestedDocument() {
96+
// Creating a nested document
97+
DocumentImplementation nestedDocument = new DocumentImplementation(new HashMap<>());
98+
nestedDocument.put("nestedKey", "nestedValue");
99+
100+
101+
document.put("nested", nestedDocument);
102+
103+
// Retrieving the nested document
104+
DocumentImplementation retrievedNestedDocument = (DocumentImplementation) document.get("nested");
105+
106+
assertNotNull(retrievedNestedDocument);
107+
assertEquals("nestedValue", retrievedNestedDocument.get("nestedKey"));
108+
}
109+
110+
@Test
111+
void shouldUpdateExistingValue() {
112+
// Arrange
113+
final String key = "key";
114+
final String originalValue = "originalValue";
115+
final String updatedValue = "updatedValue";
116+
117+
document.put(key, originalValue);
118+
119+
// Updating the value
120+
document.put(key, updatedValue);
121+
122+
//Verifying that the updated value is retrieved correctly
123+
assertEquals(updatedValue, document.get(key));
124+
}
83125
}

0 commit comments

Comments
 (0)