1 package de.juplo.httpresources; 2 3 import org.junit.jupiter.api.Test; 4 import org.junit.jupiter.api.extension.ExtendWith; 5 import org.mockito.Mock; 6 import org.mockito.junit.jupiter.MockitoExtension; 7 import org.mockito.junit.jupiter.MockitoSettings; 8 import org.mockito.quality.Strictness; 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 import org.springframework.core.io.Resource; 12 import org.springframework.web.context.WebApplicationContext; 13 14 import static org.assertj.core.api.Assertions.assertThat; 15 import static org.mockito.ArgumentMatchers.eq; 16 import static org.mockito.Mockito.*; 17 18 19 @ExtendWith({ MockitoExtension.class }) 20 @MockitoSettings(strictness = Strictness.LENIENT) 21 public class HttpResourceChainAwareResourceLoaderTest 22 { 23 private static final Logger LOG = 24 LoggerFactory.getLogger(HttpResourceChainAwareResourceLoaderTest.class); 25 26 27 @Test 28 public void testResourceExistsForaLocation( 29 @Mock WebApplicationContext context, 30 @Mock Resource existing, 31 @Mock Resource nonExisting) 32 { 33 LOG.info("<-- start of test-case"); 34 35 HttpResourceChainAwareResourceLoader loader = 36 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B/", "C/" }); 37 38 when(context.getResource(eq("A/PATH"))).thenReturn(existing); 39 when(context.getResource(eq("B/PATH"))).thenReturn(nonExisting); 40 when(context.getResource(eq("C/PATH"))).thenReturn(nonExisting); 41 when(context.getResource(eq("PATH"))).thenReturn(nonExisting); 42 when(existing.exists()).thenReturn(true); 43 when(nonExisting.exists()).thenReturn(false); 44 45 assertThat(loader.getResource("PATH")).isSameAs(existing); 46 47 verify(context, times(1)).getResource(eq("A/PATH")); 48 verify(context, never()).getResource(eq("B/PATH")); 49 verify(context, never()).getResource(eq("C/PATH")); 50 verify(context, never()).getResource(eq("PATH")); 51 } 52 53 @Test 54 public void testResourceExistsForSecondLocation( 55 @Mock WebApplicationContext context, 56 @Mock Resource existing, 57 @Mock Resource nonExisting) 58 { 59 LOG.info("<-- start of test-case"); 60 61 HttpResourceChainAwareResourceLoader loader = 62 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B/", "C/" }); 63 64 when(context.getResource(eq("A/PATH"))).thenReturn(nonExisting); 65 when(context.getResource(eq("B/PATH"))).thenReturn(existing); 66 when(context.getResource(eq("C/PATH"))).thenReturn(nonExisting); 67 when(context.getResource(eq("PATH"))).thenReturn(nonExisting); 68 when(existing.exists()).thenReturn(true); 69 when(nonExisting.exists()).thenReturn(false); 70 71 assertThat(loader.getResource("PATH")).isSameAs(existing); 72 73 verify(context, times(1)).getResource(eq("A/PATH")); 74 verify(context, times(1)).getResource(eq("B/PATH")); 75 verify(context, never()).getResource(eq("C/PATH")); 76 verify(context, never()).getResource(eq("PATH")); 77 } 78 79 @Test 80 public void testResourceExistsForThirdLocation( 81 @Mock WebApplicationContext context, 82 @Mock Resource existing, 83 @Mock Resource nonExisting) 84 { 85 LOG.info("<-- start of test-case"); 86 87 HttpResourceChainAwareResourceLoader loader = 88 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B/", "C/" }); 89 90 when(context.getResource(eq("A/PATH"))).thenReturn(nonExisting); 91 when(context.getResource(eq("B/PATH"))).thenReturn(nonExisting); 92 when(context.getResource(eq("C/PATH"))).thenReturn(existing); 93 when(context.getResource(eq("PATH"))).thenReturn(nonExisting); 94 when(existing.exists()).thenReturn(true); 95 when(nonExisting.exists()).thenReturn(false); 96 97 assertThat(loader.getResource("PATH")).isSameAs(existing); 98 99 verify(context, times(1)).getResource(eq("A/PATH")); 100 verify(context, times(1)).getResource(eq("B/PATH")); 101 verify(context, times(1)).getResource(eq("C/PATH")); 102 verify(context, never()).getResource(eq("PATH")); 103 } 104 105 @Test 106 public void testResourceDoesNotExist( 107 @Mock WebApplicationContext context, 108 @Mock Resource nonExisting, 109 @Mock Resource other) 110 { 111 LOG.info("<-- start of test-case"); 112 113 HttpResourceChainAwareResourceLoader loader = 114 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B/", "C/" }); 115 116 when(context.getResource(eq("A/PATH"))).thenReturn(nonExisting); 117 when(context.getResource(eq("B/PATH"))).thenReturn(nonExisting); 118 when(context.getResource(eq("C/PATH"))).thenReturn(nonExisting); 119 when(context.getResource(eq("PATH"))).thenReturn(other); 120 when(nonExisting.exists()).thenReturn(false); 121 122 assertThat(loader.getResource("PATH")).isSameAs(other); 123 124 verify(context, times(1)).getResource(eq("A/PATH")); 125 verify(context, times(1)).getResource(eq("B/PATH")); 126 verify(context, times(1)).getResource(eq("C/PATH")); 127 verify(context, times(1)).getResource(eq("PATH")); 128 } 129 130 @Test 131 public void testHandlingOfLeadingSlashesInResourceNames( 132 @Mock WebApplicationContext context, 133 @Mock Resource nonExisting, 134 @Mock Resource other) 135 { 136 LOG.info("<-- start of test-case"); 137 138 HttpResourceChainAwareResourceLoader loader = 139 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B/" }); 140 141 when(context.getResource(startsWith("A"))).thenReturn(nonExisting); 142 when(context.getResource(startsWith("B"))).thenReturn(nonExisting); 143 when(context.getResource(eq("/PATH/WITH/LEADING/SLASH"))).thenReturn(other); 144 when(nonExisting.exists()).thenReturn(false); 145 146 assertThat(loader.getResource("/PATH/WITH/LEADING/SLASH")).isSameAs(other); 147 148 verify(context, times(1)).getResource(eq("A/PATH/WITH/LEADING/SLASH")); 149 verify(context, times(1)).getResource(eq("B/PATH/WITH/LEADING/SLASH")); 150 verify(context, times(1)).getResource(eq("/PATH/WITH/LEADING/SLASH")); 151 } 152 153 @Test 154 public void testHandlingOfTrailingSlashesInResourceLocations( 155 @Mock WebApplicationContext context, 156 @Mock Resource nonExisting, 157 @Mock Resource other) 158 { 159 LOG.info("<-- start of test-case"); 160 161 HttpResourceChainAwareResourceLoader loader = 162 new HttpResourceChainAwareResourceLoader(context, new String[] { "A/", "B", "C//" }); 163 164 when(context.getResource(startsWith("A"))).thenReturn(nonExisting); 165 when(context.getResource(startsWith("B"))).thenReturn(nonExisting); 166 when(context.getResource(startsWith("C"))).thenReturn(nonExisting); 167 when(context.getResource(eq("/PATH/WITH/LEADING/SLASH"))).thenReturn(other); 168 when(nonExisting.exists()).thenReturn(false); 169 170 assertThat(loader.getResource("/PATH/WITH/LEADING/SLASH")).isSameAs(other); 171 172 verify(context, times(1)).getResource(eq("A/PATH/WITH/LEADING/SLASH")); 173 verify(context, times(1)).getResource(eq("B/PATH/WITH/LEADING/SLASH")); 174 verify(context, times(1)).getResource(eq("C//PATH/WITH/LEADING/SLASH")); 175 verify(context, times(1)).getResource(eq("/PATH/WITH/LEADING/SLASH")); 176 } 177 }