Skip to content

Commit 327f5e5

Browse files
author
Devi Pathak
committed
FINERACT-2537: Add unit tests for the StreamResponseUtil
Tests cover: - ok(StreamResponseData) response creation - ok(AsyncResponse, StreamResponseData) asynchronous response handling - Content-Disposition header generation - StreamingOutput functionality - Async response execution These tests improve test coverage and help ensure correct behavior of response streaming utilities.
1 parent 83021ce commit 327f5e5

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.util;
20+
21+
import jakarta.ws.rs.container.AsyncResponse;
22+
import jakarta.ws.rs.core.HttpHeaders;
23+
import jakarta.ws.rs.core.Response;
24+
import jakarta.ws.rs.core.StreamingOutput;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.InputStream;
28+
import java.util.concurrent.Future;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
import org.mockito.Mockito;
32+
33+
class StreamResponseUtilTest {
34+
35+
@Test
36+
void okReturnsResponseWithoutContentDispositionWhenDispositionTypeEmpty() {
37+
38+
InputStream stream = new ByteArrayInputStream("test".getBytes());
39+
40+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
41+
.fileName("file.txt").build();
42+
43+
Response response = StreamResponseUtil.ok(data);
44+
45+
Assertions.assertEquals("text/plain", response.getMediaType().toString());
46+
Assertions.assertNull(response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION));
47+
}
48+
49+
@Test
50+
void okReturnsResponseWithContentDispositionWhenDispositionTypePresent() {
51+
52+
InputStream stream = new ByteArrayInputStream("test".getBytes());
53+
54+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
55+
.fileName("file.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT).build();
56+
57+
Response response = StreamResponseUtil.ok(data);
58+
59+
Assertions.assertEquals("text/plain", response.getMediaType().toString());
60+
61+
String header = response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
62+
63+
Assertions.assertNotNull(header);
64+
Assertions.assertTrue(header.contains(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT));
65+
Assertions.assertTrue(header.contains("file.txt"));
66+
}
67+
68+
@Test
69+
void okAsyncResponseWithoutDisposition() throws Exception {
70+
71+
AsyncResponse asyncResponse = Mockito.mock(AsyncResponse.class);
72+
73+
InputStream stream = new ByteArrayInputStream("test".getBytes());
74+
75+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
76+
.build();
77+
78+
Future<?> future = StreamResponseUtil.ok(asyncResponse, data);
79+
80+
future.get();
81+
82+
Mockito.verify(asyncResponse).resume(Mockito.any(Response.class));
83+
}
84+
85+
@Test
86+
void okAsyncResponseWithDisposition() throws Exception {
87+
88+
AsyncResponse asyncResponse = Mockito.mock(AsyncResponse.class);
89+
90+
InputStream stream = new ByteArrayInputStream("test".getBytes());
91+
92+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
93+
.fileName("file.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_INLINE).build();
94+
95+
Future<?> future = StreamResponseUtil.ok(asyncResponse, data);
96+
97+
future.get();
98+
99+
Mockito.verify(asyncResponse).resume(Mockito.any(Response.class));
100+
}
101+
102+
@Test
103+
void okHeaderContainsCorrectFilename() {
104+
105+
InputStream stream = new ByteArrayInputStream("hello".getBytes());
106+
107+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
108+
.fileName("example.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT).build();
109+
110+
Response response = StreamResponseUtil.ok(data);
111+
112+
String header = response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
113+
114+
Assertions.assertTrue(header.contains("attachment"));
115+
Assertions.assertTrue(header.contains("example.txt"));
116+
}
117+
118+
@Test
119+
void okStreamingOutputWritesStreamData() throws Exception {
120+
121+
byte[] content = "stream-content".getBytes();
122+
InputStream stream = new ByteArrayInputStream(content);
123+
124+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
125+
.build();
126+
127+
Response response = StreamResponseUtil.ok(data);
128+
129+
StreamingOutput streamingOutput = (StreamingOutput) response.getEntity();
130+
131+
ByteArrayOutputStream out = new ByteArrayOutputStream();
132+
133+
streamingOutput.write(out);
134+
135+
Assertions.assertArrayEquals(content, out.toByteArray());
136+
}
137+
}

0 commit comments

Comments
 (0)