Skip to content

Commit 603ddc3

Browse files
committed
Fix tests for RedirectionResource
1 parent 599c094 commit 603ddc3

5 files changed

Lines changed: 79 additions & 5 deletions

File tree

backend/src/main/java/de/envite/proa/rest/RedirectionResource.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.envite.proa.rest;
22

3+
import de.envite.proa.util.ResourceLoader;
4+
import jakarta.inject.Inject;
35
import jakarta.ws.rs.GET;
46
import jakarta.ws.rs.Path;
57
import jakarta.ws.rs.Produces;
@@ -10,14 +12,17 @@
1012
@Path("")
1113
public class RedirectionResource {
1214

15+
@Inject
16+
ResourceLoader resourceLoader;
17+
1318
/**
1419
* Serve index.html for evey call that is not an api call
1520
*/
1621
@GET
1722
@Path("/{path: (?!.*api).+}")
1823
@Produces("text/html")
1924
public Response serveVueApp() {
20-
InputStream indexHtmlStream = getClass().getClassLoader().getResourceAsStream("META-INF/resources/index.html");
25+
InputStream indexHtmlStream = resourceLoader.loadResource("META-INF/resources/index.html");
2126
if (indexHtmlStream == null) {
2227
return Response.status(Response.Status.NOT_FOUND).build();
2328
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.envite.proa.util;
2+
3+
import jakarta.enterprise.context.ApplicationScoped;
4+
5+
import java.io.InputStream;
6+
7+
@ApplicationScoped
8+
public class ResourceLoader {
9+
public InputStream loadResource(String path) {
10+
return getClass().getClassLoader().getResourceAsStream(path);
11+
}
12+
}
Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package de.envite.proa.rest;
22

3+
import de.envite.proa.util.ResourceLoader;
4+
import jakarta.ws.rs.core.Response;
35
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
47
import org.mockito.InjectMocks;
8+
import org.mockito.Mock;
59
import org.mockito.MockitoAnnotations;
610

11+
import java.io.ByteArrayInputStream;
12+
import java.io.InputStream;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.mockito.Mockito.*;
16+
717
public class RedirectionResourceTest {
818

19+
@Mock
20+
private ResourceLoader resourceLoader;
21+
922
@InjectMocks
1023
private RedirectionResource redirectionResource;
1124

@@ -14,12 +27,28 @@ public void setUp() {
1427
MockitoAnnotations.openMocks(this);
1528
}
1629

17-
/*@Test
30+
@Test
1831
public void testServeVueApp() {
32+
InputStream mockStream = new ByteArrayInputStream("<html>Test</html>".getBytes());
33+
when(resourceLoader.loadResource("META-INF/resources/index.html")).thenReturn(mockStream);
34+
1935
Response response = redirectionResource.serveVueApp();
2036

21-
assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
37+
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
38+
39+
verify(resourceLoader, times(1)).loadResource("META-INF/resources/index.html");
40+
verifyNoMoreInteractions(resourceLoader);
41+
}
2242

23-
assertEquals("/", response.getHeaderString("Location"));
24-
}*/
43+
@Test
44+
public void testServeVueApp_NotFound() {
45+
when(resourceLoader.loadResource("META-INF/resources/index.html")).thenReturn(null);
46+
47+
Response response = redirectionResource.serveVueApp();
48+
49+
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
50+
51+
verify(resourceLoader, times(1)).loadResource("META-INF/resources/index.html");
52+
verifyNoMoreInteractions(resourceLoader);
53+
}
2554
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.envite.proa.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.InputStream;
6+
7+
import static org.junit.jupiter.api.Assertions.assertNotNull;
8+
import static org.junit.jupiter.api.Assertions.assertNull;
9+
10+
public class ResourceLoaderTest {
11+
12+
private final ResourceLoader resourceLoader = new ResourceLoader();
13+
14+
@Test
15+
public void testLoadExistingResource() {
16+
InputStream inputStream = resourceLoader.loadResource("test-index.html");
17+
18+
assertNotNull(inputStream, "Resource should be found and return a non-null InputStream");
19+
}
20+
21+
@Test
22+
public void testLoadNonExistingResource() {
23+
InputStream inputStream = resourceLoader.loadResource("non-existent-file.html");
24+
25+
assertNull(inputStream, "ResourceLoader should return null when the file is not found");
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html lang="en">Test</html>

0 commit comments

Comments
 (0)