|
| 1 | +package com.flowingcode.vaadin.addons.rssitems; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.doNothing; |
| 4 | +import static org.mockito.Mockito.doReturn; |
| 5 | +import static org.mockito.Mockito.anyString; |
| 6 | +import static org.mockito.Mockito.anyBoolean; |
| 7 | +import static org.mockito.Mockito.anyInt; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | +import static org.mockito.Mockito.spy; |
| 11 | +import static org.mockito.Mockito.times; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.ArgumentMatchers.any; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Optional; |
| 17 | +import org.apache.http.client.ClientProtocolException; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.mockito.Answers; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | + |
| 24 | +import com.vaadin.flow.component.UI; |
| 25 | +import com.vaadin.flow.component.Element; // Correct import for Element |
| 26 | + |
| 27 | +public class RssItemsTest { |
| 28 | + |
| 29 | + private RssItems rssItemsSpy; |
| 30 | + private static final String INITIAL_URL = "http://example.com/rss"; |
| 31 | + private static final String ANOTHER_URL = "http://another.example.com/rss"; |
| 32 | + private static final String CONSTRUCTOR_TEST_URL = "http://constructortest.com/rss"; |
| 33 | + private static final String DUMMY_RSS_XML = "<rss version=\"2.0\"><channel><item><title>Test</title></item></channel></rss>"; |
| 34 | + |
| 35 | + @Mock |
| 36 | + private Element elementMock; |
| 37 | + |
| 38 | + @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 39 | + private UI uiMock; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws ClientProtocolException, IOException { |
| 43 | + MockitoAnnotations.openMocks(this); |
| 44 | + |
| 45 | + RssItems realRssItems = new RssItems(); |
| 46 | + rssItemsSpy = spy(realRssItems); |
| 47 | + |
| 48 | + when(rssItemsSpy.getElement()).thenReturn(elementMock); |
| 49 | + when(elementMock.getUI()).thenReturn(Optional.of(uiMock)); |
| 50 | + |
| 51 | + when(elementMock.setProperty(anyString(), anyString())).thenReturn(elementMock); |
| 52 | + when(elementMock.setProperty(anyString(), anyBoolean())).thenReturn(elementMock); |
| 53 | + when(elementMock.setProperty(anyString(), anyInt())).thenReturn(elementMock); |
| 54 | + |
| 55 | + doNothing().when(rssItemsSpy).invokeXmlToItems(anyString()); |
| 56 | + doReturn(DUMMY_RSS_XML).when(rssItemsSpy).obtainRss(anyString()); |
| 57 | + |
| 58 | + rssItemsSpy.setUrl(INITIAL_URL); |
| 59 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSetMaxTitleLengthDoesNotCallObtainRss() { |
| 64 | + rssItemsSpy.setMaxTitleLength(100); |
| 65 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 66 | + verify(rssItemsSpy, times(1)).obtainRss(anyString()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testSetMaxExcerptLengthDoesNotCallObtainRss() { |
| 71 | + rssItemsSpy.setMaxExcerptLength(200); |
| 72 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 73 | + verify(rssItemsSpy, times(1)).obtainRss(anyString()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testSetMaxDoesNotCallObtainRss() { |
| 78 | + rssItemsSpy.setMax(10); |
| 79 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 80 | + verify(rssItemsSpy, times(1)).obtainRss(anyString()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSetExtractImageFromDescriptionDoesNotCallObtainRss() { |
| 85 | + rssItemsSpy.setExtractImageFromDescription(true); |
| 86 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 87 | + verify(rssItemsSpy, times(1)).obtainRss(anyString()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testSetUrlTriggersObtainRss() throws ClientProtocolException, IOException { |
| 92 | + rssItemsSpy.setUrl(ANOTHER_URL); |
| 93 | + verify(rssItemsSpy, times(1)).obtainRss(INITIAL_URL); |
| 94 | + verify(rssItemsSpy, times(1)).obtainRss(ANOTHER_URL); |
| 95 | + verify(rssItemsSpy, times(2)).obtainRss(anyString()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testRefreshTriggersObtainRss() throws ClientProtocolException, IOException { |
| 100 | + rssItemsSpy.refresh(); |
| 101 | + verify(rssItemsSpy, times(2)).obtainRss(INITIAL_URL); |
| 102 | + verify(rssItemsSpy, times(2)).obtainRss(anyString()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testConstructorWithUrlCallsObtainRss() throws ClientProtocolException, IOException { |
| 107 | + RssItems itemsConstructedWithUrl = new RssItems(); |
| 108 | + RssItems constructorSpy = spy(itemsConstructedWithUrl); |
| 109 | + |
| 110 | + Element localElementMock = mock(Element.class); |
| 111 | + UI localUiMock = mock(UI.class, Answers.RETURNS_DEEP_STUBS); |
| 112 | + |
| 113 | + when(constructorSpy.getElement()).thenReturn(localElementMock); |
| 114 | + when(localElementMock.getUI()).thenReturn(Optional.of(localUiMock)); |
| 115 | + when(localElementMock.setProperty(anyString(), anyString())).thenReturn(localElementMock); |
| 116 | + when(localElementMock.setProperty(anyString(), anyBoolean())).thenReturn(localElementMock); |
| 117 | + when(localElementMock.setProperty(anyString(), anyInt())).thenReturn(localElementMock); |
| 118 | + doNothing().when(constructorSpy).invokeXmlToItems(anyString()); |
| 119 | + |
| 120 | + // Specific mock for the URL used in this constructor test |
| 121 | + doReturn(DUMMY_RSS_XML).when(constructorSpy).obtainRss(CONSTRUCTOR_TEST_URL); |
| 122 | + // Fallback for any other unexpected string if other URLs are called by internal setters etc. |
| 123 | + // This helps if setUrl(null) or similar is called by a default constructor path we aren't expecting. |
| 124 | + doReturn(DUMMY_RSS_XML).when(constructorSpy).obtainRss(argThat(argument -> !CONSTRUCTOR_TEST_URL.equals(argument))); |
| 125 | + |
| 126 | + |
| 127 | + // Simulate the sequence of calls as it happens in RssItems(String url) |
| 128 | + // RssItems(String url) -> RssItems(url, DEFAULT_MAX, ...) |
| 129 | + // The main constructor calls: |
| 130 | + // 1. setUrl(url) -> obtainRss (1st call) |
| 131 | + // 2. setAuto(true), setMax, etc. (no obtainRss) |
| 132 | + // 3. refreshUrl() -> obtainRss (2nd call) |
| 133 | + |
| 134 | + constructorSpy.setUrl(CONSTRUCTOR_TEST_URL); |
| 135 | + constructorSpy.setAuto(true); |
| 136 | + constructorSpy.setMax(RssItems.DEFAULT_MAX); // Using public constant |
| 137 | + constructorSpy.setMaxExcerptLength(RssItems.DEFAULT_MAX_EXCERPT_LENGTH); // Using public constant |
| 138 | + constructorSpy.setMaxTitleLength(RssItems.DEFAULT_MAX_TITLE_LENGTH); // Using public constant |
| 139 | + constructorSpy.refresh(); |
| 140 | + |
| 141 | + verify(constructorSpy, times(2)).obtainRss(CONSTRUCTOR_TEST_URL); |
| 142 | + } |
| 143 | +} |
0 commit comments