Newer
Older
sendgrid-api / src / test / java / io / ljunggren / sendgrid / api / model / StatisticsTest.java
package io.ljunggren.sendgrid.api.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import io.ljunggren.json.utils.JsonUtils;

class StatisticsTest {

	private static final ObjectMapper MAPPER = new ObjectMapper().registerModule(new JavaTimeModule());

	private String readFromResources(String file) throws IOException {
		try (InputStream in = this.getClass().getResourceAsStream(file)) {
			assertNotNull(in, "Test resource not found: " + file);
			return IOUtils.toString(in, StandardCharsets.UTF_8);
		}
	}

	@Test
	void instantiateTest() throws IOException {
		String json = readFromResources("/io/ljunggren/sendgrid/api/model/stats.json");
		List<Statistics> statistics = JsonUtils.jsonToList(json, Statistics.class, MAPPER);
		assertEquals(2, statistics.size());
		assertEquals(LocalDate.of(2025, 10, 18), statistics.get(0).getDate());
		assertEquals(LocalDate.of(2025, 10, 19), statistics.get(1).getDate());
		assertEquals(1, statistics.get(1).getStats().get(0).getMetrics().getDelivered());
	}

}