Skip to content
Snippets Groups Projects
Commit d77291f3 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-1513 OZG-2594 fix NPE on bodyObject mapping

parent 75952c36
Branches
Tags
No related merge requests found
package de.itvsh.goofy.common.command;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -66,7 +68,13 @@ public class CommandRemoteService {
}
Map<String, Object> getBodyAsMap(CommandBody commandBody) {
return new BeanMap(commandBody).entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> entry.getValue()));
return new BeanMap(commandBody).entrySet().stream()
.filter(this::isValidEntry)
.collect(Collectors.toMap(entry -> entry.getKey().toString(), Entry::getValue));
}
boolean isValidEntry(Entry<Object, Object> entry) {
return Objects.nonNull(entry.getKey()) && Objects.nonNull(entry.getValue());
}
GrpcCreateCommandRequest buildCreateAttachedItemCommandRequest(CreateCommand command, String itemName) {
......
......@@ -4,7 +4,9 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -209,6 +211,39 @@ class CommandRemoteServiceTest {
assertThat(bodyMap).containsEntry("postfachId", PostfachMailTestFactory.POSTFACH_ID);
}
@DisplayName("is valid entry")
@Nested
class TestIsValidEntry {
@Test
void shouldReturnFalseOnNullKey() {
var isValidEntry = service.isValidEntry(createEntry(null, "huhu"));
assertThat(isValidEntry).isFalse();
}
@Test
void shouldReturnFalseOnNullValue() {
var isValidEntry = service.isValidEntry(createEntry("huhu", null));
assertThat(isValidEntry).isFalse();
}
@Test
void shouldReturnFaoseOnValidEntry() {
var isValidEntry = service.isValidEntry(createEntry("huhu", "huhu"));
assertThat(isValidEntry).isTrue();
}
private Entry<Object, Object> createEntry(Object key, Object value) {
var map = new HashMap<Object, Object>();
map.put(key, value);
return (Entry<Object, Object>) map.entrySet().toArray()[0];
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment