package cli.link;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

class LinkCommandsTest {

	LinkCliAdapter mockAdapter = mock(LinkCliAdapter.class);

	@Test
	void addCommandWorks() {
		var url = "http://tea.filefighter.de";
		var username = "mario";
		var category1 = "funStuff";
		var category2 = "workStuff";

		ArgumentCaptor<String> captureUrl = ArgumentCaptor.forClass(String.class);
		ArgumentCaptor<String> captureUsername = ArgumentCaptor.forClass(String.class);
		ArgumentCaptor<Set<String>> captureCategories = ArgumentCaptor.forClass(Set.class);

		doNothing()
				.when(mockAdapter)
				.addLink(captureUrl.capture(), captureCategories.capture(), captureUsername.capture());

		var sut = new LinkCommands(mockAdapter);
		var returnValue = sut.executeSubcommand(new String[]{"add", url, username, category1, category2});

		assertEquals("Added the new Link", returnValue);

		assertEquals(url, captureUrl.getValue());
		assertEquals(username, captureUsername.getValue());
		assertEquals(Set.of(category1, category2), captureCategories.getValue());
	}

	@Test
	void addCommandWorksNocategories() {
		var url = "http://tea.filefighter.de";
		var username = "mario";

		ArgumentCaptor<String> captureUrl = ArgumentCaptor.forClass(String.class);
		ArgumentCaptor<String> captureUsername = ArgumentCaptor.forClass(String.class);
		ArgumentCaptor<Set<String>> captureCategories = ArgumentCaptor.forClass(Set.class);

		doNothing()
				.when(mockAdapter)
				.addLink(captureUrl.capture(), captureCategories.capture(), captureUsername.capture());

		var sut = new LinkCommands(mockAdapter);
		var returnValue = sut.executeSubcommand(new String[]{"add", url, username});

		assertEquals("Added the new Link", returnValue);

		assertEquals(url, captureUrl.getValue());
		assertEquals(username, captureUsername.getValue());
		assertEquals(Set.of(), captureCategories.getValue());
	}
}
