Skip to content

Commit 34d4689

Browse files
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 34d4689

1 file changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 static org.junit.jupiter.api.Assertions.assertArrayEquals;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertNull;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
27+
import jakarta.ws.rs.container.AsyncResponse;
28+
import jakarta.ws.rs.core.HttpHeaders;
29+
import jakarta.ws.rs.core.Response;
30+
import jakarta.ws.rs.core.StreamingOutput;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.ByteArrayOutputStream;
33+
import java.io.InputStream;
34+
import java.util.concurrent.Future;
35+
import org.junit.jupiter.api.Test;
36+
import org.mockito.Mockito;
37+
38+
class StreamResponseUtilTest {
39+
40+
@Test
41+
void okReturnsResponseWithoutContentDispositionWhenDispositionTypeEmpty() {
42+
43+
InputStream stream = new ByteArrayInputStream("test".getBytes());
44+
45+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
46+
.fileName("file.txt").build();
47+
48+
Response response = StreamResponseUtil.ok(data);
49+
50+
assertEquals("text/plain", response.getMediaType().toString());
51+
assertNull(response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION));
52+
}
53+
54+
@Test
55+
void okReturnsResponseWithContentDispositionWhenDispositionTypePresent() {
56+
57+
InputStream stream = new ByteArrayInputStream("test".getBytes());
58+
59+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
60+
.fileName("file.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT).build();
61+
62+
Response response = StreamResponseUtil.ok(data);
63+
64+
assertEquals("text/plain", response.getMediaType().toString());
65+
66+
String header = response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
67+
68+
assertNotNull(header);
69+
assertTrue(header.contains(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT));
70+
assertTrue(header.contains("file.txt"));
71+
}
72+
73+
@Test
74+
void okAsyncResponseWithoutDisposition() throws Exception {
75+
76+
AsyncResponse asyncResponse = Mockito.mock(AsyncResponse.class);
77+
78+
InputStream stream = new ByteArrayInputStream("test".getBytes());
79+
80+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
81+
.build();
82+
83+
Future<?> future = StreamResponseUtil.ok(asyncResponse, data);
84+
85+
future.get();
86+
87+
Mockito.verify(asyncResponse).resume(Mockito.any(Response.class));
88+
}
89+
90+
@Test
91+
void okAsyncResponseWithDisposition() throws Exception {
92+
93+
AsyncResponse asyncResponse = Mockito.mock(AsyncResponse.class);
94+
95+
InputStream stream = new ByteArrayInputStream("test".getBytes());
96+
97+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
98+
.fileName("file.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_INLINE).build();
99+
100+
Future<?> future = StreamResponseUtil.ok(asyncResponse, data);
101+
102+
future.get();
103+
104+
Mockito.verify(asyncResponse).resume(Mockito.any(Response.class));
105+
}
106+
107+
@Test
108+
void okHeaderContainsCorrectFilename() {
109+
110+
InputStream stream = new ByteArrayInputStream("hello".getBytes());
111+
112+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
113+
.fileName("example.txt").dispositionType(StreamResponseUtil.DISPOSITION_TYPE_ATTACHMENT).build();
114+
115+
Response response = StreamResponseUtil.ok(data);
116+
117+
String header = response.getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
118+
119+
assertTrue(header.contains("attachment"));
120+
assertTrue(header.contains("example.txt"));
121+
}
122+
123+
@Test
124+
void okStreamingOutputWritesStreamData() throws Exception {
125+
126+
byte[] content = "stream-content".getBytes();
127+
InputStream stream = new ByteArrayInputStream(content);
128+
129+
StreamResponseUtil.StreamResponseData data = StreamResponseUtil.StreamResponseData.builder().stream(stream).type("text/plain")
130+
.build();
131+
132+
Response response = StreamResponseUtil.ok(data);
133+
134+
StreamingOutput streamingOutput = (StreamingOutput) response.getEntity();
135+
136+
ByteArrayOutputStream out = new ByteArrayOutputStream();
137+
138+
streamingOutput.write(out);
139+
140+
assertArrayEquals(content, out.toByteArray());
141+
}
142+
}

0 commit comments

Comments
 (0)