Skip to content

Commit

Permalink
test: create test scenario to list
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Nov 29, 2023
1 parent 250f933 commit 5b5e279
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/src/main/java/expert/os/harperdb/JSONMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -45,7 +46,8 @@ <T> List<T> readValue(byte[] bytes, Class<T> type) {
if(bytes == null || bytes.length == 0){
return Collections.emptyList();
}
return mapper.readValue(bytes, new TypeReference<>() {});
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, type);
return mapper.readValue(bytes, javaType);
} catch (IOException exception) {
throw new HarperDBException("There is an issue to read from json value", exception);
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/expert/os/harperdb/JSONMapperTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package expert.os.harperdb;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -40,4 +42,24 @@ void shouldReturnEntityFromJSON() {
softly.assertThat(schema.name()).isEqualTo("test");
});
}

@Test
void shouldReturnEmptyListWhenByteIsNull() {
List<CreateSchema> schemas = mapper.readValue(null, CreateSchema.class);
Assertions.assertThat(schemas).isNotNull().isEmpty();
}

@Test
void shouldReturnEmptyListWhenByteIsEmpty() {
List<CreateSchema> schemas = mapper.readValue(new byte[0], CreateSchema.class);
Assertions.assertThat(schemas).isNotNull().isEmpty();
}

@Test
void shouldReturnListOfEntitiesFromJSON() {
String expected = "[{\"id\":\"1\",\"name\":\"Lion\"}, {\"id\":\"2\",\"name\":\"Dog\"}]";
List<Animal> animals = mapper.readValue(expected.getBytes(), Animal.class);
Assertions.assertThat(animals).isNotNull().isNotEmpty().hasSize(2)
.extracting(Animal::id).contains("1", "2");
}
}

0 comments on commit 5b5e279

Please sign in to comment.