|
|
@@ -0,0 +1,81 @@
|
|
|
+package org.leumasjaffe.recipe.controller;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+import java.awt.Component;
|
|
|
+import java.awt.ComponentOrientation;
|
|
|
+import java.io.StringReader;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.leumasjaffe.recipe.model.Recipe;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.mockito.InOrder;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.mockito.Spy;
|
|
|
+
|
|
|
+class FileControllerTest {
|
|
|
+ @SuppressWarnings("serial")
|
|
|
+ private static class StubComponent extends Component implements FileController.Model {
|
|
|
+ @Override public void setModel(Recipe model) {}
|
|
|
+ }
|
|
|
+
|
|
|
+ StubComponent stub;
|
|
|
+ FileController<StubComponent> controller;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ stub = mock(StubComponent.class);
|
|
|
+ controller = spy(new FileController<>(stub));
|
|
|
+ // Make sure that any errors are directed into the test case's failure trace,
|
|
|
+ // rather than a JOptionDialog
|
|
|
+ doAnswer(inv -> { throw inv.getArgument(0, Exception.class); })
|
|
|
+ .when(controller).errorPopup(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testCanCreateNewModel() {
|
|
|
+ controller.create();
|
|
|
+ verify(stub).setModel(any(Recipe.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testRepeatedCreatesProvideNewObjects() {
|
|
|
+ InOrder inOrder = inOrder(stub);
|
|
|
+ final ArgumentCaptor<Recipe> first = ArgumentCaptor.forClass(Recipe.class);
|
|
|
+ final ArgumentCaptor<Recipe> second = ArgumentCaptor.forClass(Recipe.class);
|
|
|
+
|
|
|
+ controller.create();
|
|
|
+ inOrder.verify(stub).setModel(first.capture());
|
|
|
+
|
|
|
+ controller.create();
|
|
|
+ inOrder.verify(stub).setModel(second.capture());
|
|
|
+
|
|
|
+ assertNotSame(first.getValue(), second.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testOpensContentIntoRecipe() {
|
|
|
+ final String data = "{ \"title\": \"Example\" }";
|
|
|
+ doReturn(Optional.of(new StringReader(data))).when(controller).getOpened();
|
|
|
+
|
|
|
+ controller.open();
|
|
|
+
|
|
|
+ verify(stub).setModel(argThat(r -> r.getTitle().equals("Example")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testEmitsErrorWithMalformedData() {
|
|
|
+ final String data = "{ \"name\": \"Example\" }";
|
|
|
+ doReturn(Optional.of(new StringReader(data))).when(controller).getOpened();
|
|
|
+ doNothing().when(controller).errorPopup(any());
|
|
|
+
|
|
|
+ controller.open();
|
|
|
+
|
|
|
+ verify(controller).errorPopup(any());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|