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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import io.ljunggren.sendgrid.api.exception.SendgridException;
import io.ljunggren.sendgrid.api.model.Statistics;
import io.ljunggren.sendgrid.api.service.Aggregation;
import io.ljunggren.sendgrid.api.service.EmailService;
import io.ljunggren.sendgrid.api.service.UsageService;

@ExtendWith(MockitoExtension.class)
class SendgridApiTest {

	@Mock
	private EmailService emailService;

	@Mock
	private UsageService usageService;

	private SendgridApi api;

	@BeforeEach
	void setUp() {
		api = new SendgridApi(emailService, usageService);
	}

	@Test
	void publicConstructor_rejectsNullApiKey() {
		assertThrows(IllegalArgumentException.class, () -> new SendgridApi(null));
	}

	@Test
	void publicConstructor_rejectsBlankApiKey() {
		assertThrows(IllegalArgumentException.class, () -> new SendgridApi("  "));
	}

	@Test
	void packageConstructor_rejectsNullEmailService() {
		assertThrows(IllegalArgumentException.class, () -> new SendgridApi(null, usageService));
	}

	@Test
	void packageConstructor_rejectsNullUsageService() {
		assertThrows(IllegalArgumentException.class, () -> new SendgridApi(emailService, null));
	}

	@Test
	void sendEmail_delegatesToEmailService() throws SendgridException {
		api.sendEmail("a@b.com", "c@d.com", "hi", "body");
		verify(emailService, times(1)).sendEmail("a@b.com", "c@d.com", "hi", "body");
		verify(usageService, never()).getDailyUsage();
	}

	@Test
	void sendTemplateEmail_delegatesToEmailService() throws SendgridException {
		Map<String, Object> data = Map.of("k", "v");
		api.sendTemplateEmail("tpl", "a@b.com", "c@d.com", data);
		verify(emailService, times(1)).sendTemplateEmail("tpl", "a@b.com", "c@d.com", data);
	}

	@Test
	void getDailyUsage_delegatesToUsageService() throws SendgridException {
		List<Statistics> expected = Collections.singletonList(new Statistics());
		when(usageService.getDailyUsage()).thenReturn(expected);
		assertSame(expected, api.getDailyUsage());
	}

	@Test
	void getMonthlyStatistics_delegatesToUsageService() throws SendgridException {
		List<Statistics> expected = Collections.singletonList(new Statistics());
		when(usageService.getMonthlyStatistics()).thenReturn(expected);
		assertSame(expected, api.getMonthlyStatistics());
	}

	@Test
	void getStatisticsWithDays_delegatesToUsageService() throws SendgridException {
		List<Statistics> expected = Collections.emptyList();
		when(usageService.getStatistics(7)).thenReturn(expected);
		assertEquals(expected, api.getStatistics(7));
	}

	@Test
	void getStatisticsWithAggregation_delegatesToUsageService() throws SendgridException {
		List<Statistics> expected = Collections.emptyList();
		when(usageService.getStatistics(7, Aggregation.WEEK)).thenReturn(expected);
		assertEquals(expected, api.getStatistics(7, Aggregation.WEEK));
	}

}