Skip to content

Commit 742b860

Browse files
Add unit tests for StructuredMessageDecoderPolicy
Co-authored-by: gunjansingh-msft <[email protected]>
1 parent a8fe5f1 commit 742b860

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.storage.blob.implementation.util;
5+
6+
import com.azure.core.http.HttpHeaderName;
7+
import com.azure.core.http.HttpHeaders;
8+
import com.azure.core.http.HttpMethod;
9+
import com.azure.core.http.HttpPipelineCallContext;
10+
import com.azure.core.http.HttpPipelineNextPolicy;
11+
import com.azure.core.http.HttpRequest;
12+
import com.azure.core.http.HttpResponse;
13+
import com.azure.core.util.Context;
14+
import com.azure.storage.common.DownloadContentValidationOptions;
15+
import org.junit.jupiter.api.Test;
16+
import org.mockito.Mockito;
17+
import reactor.core.publisher.Flux;
18+
import reactor.core.publisher.Mono;
19+
import reactor.test.StepVerifier;
20+
21+
import java.net.MalformedURLException;
22+
import java.net.URL;
23+
import java.nio.ByteBuffer;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.when;
29+
30+
/**
31+
* Unit tests for {@link StructuredMessageDecoderPolicy}.
32+
*/
33+
public class StructuredMessageDecoderPolicyTest {
34+
35+
@Test
36+
public void shouldNotApplyDecodingWhenContextKeyNotPresent() throws MalformedURLException {
37+
// Arrange
38+
StructuredMessageDecoderPolicy policy = new StructuredMessageDecoderPolicy();
39+
HttpPipelineCallContext context = createMockContext(null, null);
40+
HttpPipelineNextPolicy nextPolicy = createMockNextPolicy();
41+
42+
// Act
43+
Mono<HttpResponse> result = policy.process(context, nextPolicy);
44+
45+
// Assert
46+
StepVerifier.create(result)
47+
.assertNext(response -> {
48+
assertNotNull(response);
49+
assertEquals(200, response.getStatusCode());
50+
})
51+
.verifyComplete();
52+
}
53+
54+
@Test
55+
public void shouldNotApplyDecodingWhenContextKeyIsFalse() throws MalformedURLException {
56+
// Arrange
57+
StructuredMessageDecoderPolicy policy = new StructuredMessageDecoderPolicy();
58+
Context ctx = new Context(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY, false);
59+
HttpPipelineCallContext context = createMockContext(ctx, null);
60+
HttpPipelineNextPolicy nextPolicy = createMockNextPolicy();
61+
62+
// Act
63+
Mono<HttpResponse> result = policy.process(context, nextPolicy);
64+
65+
// Assert
66+
StepVerifier.create(result)
67+
.assertNext(response -> {
68+
assertNotNull(response);
69+
assertEquals(200, response.getStatusCode());
70+
})
71+
.verifyComplete();
72+
}
73+
74+
@Test
75+
public void shouldApplyDecodingWhenContextKeyIsTrue() throws MalformedURLException {
76+
// Arrange
77+
StructuredMessageDecoderPolicy policy = new StructuredMessageDecoderPolicy();
78+
DownloadContentValidationOptions validationOptions = new DownloadContentValidationOptions()
79+
.setStructuredMessageValidationEnabled(true);
80+
81+
Context ctx = new Context(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY, true)
82+
.addData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY, validationOptions);
83+
84+
HttpPipelineCallContext context = createMockContext(ctx, 1024L);
85+
HttpPipelineNextPolicy nextPolicy = createMockNextPolicy();
86+
87+
// Act
88+
Mono<HttpResponse> result = policy.process(context, nextPolicy);
89+
90+
// Assert
91+
StepVerifier.create(result)
92+
.assertNext(response -> {
93+
assertNotNull(response);
94+
assertEquals(200, response.getStatusCode());
95+
// Verify it's a DecodedResponse
96+
assertTrue(response instanceof StructuredMessageDecoderPolicy.DecodedResponse);
97+
})
98+
.verifyComplete();
99+
}
100+
101+
@Test
102+
public void shouldNotApplyDecodingForNonDownloadResponse() throws MalformedURLException {
103+
// Arrange
104+
StructuredMessageDecoderPolicy policy = new StructuredMessageDecoderPolicy();
105+
DownloadContentValidationOptions validationOptions = new DownloadContentValidationOptions()
106+
.setStructuredMessageValidationEnabled(true);
107+
108+
Context ctx = new Context(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY, true)
109+
.addData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY, validationOptions);
110+
111+
HttpPipelineCallContext context = createMockContext(ctx, null);
112+
// Create a non-GET request (POST)
113+
HttpRequest request = new HttpRequest(HttpMethod.POST, new URL("https://test.blob.core.windows.net/container/blob"));
114+
when(context.getHttpRequest()).thenReturn(request);
115+
116+
HttpPipelineNextPolicy nextPolicy = mock(HttpPipelineNextPolicy.class);
117+
HttpResponse mockResponse = createMockHttpResponse(request, null);
118+
when(nextPolicy.process()).thenReturn(Mono.just(mockResponse));
119+
120+
// Act
121+
Mono<HttpResponse> result = policy.process(context, nextPolicy);
122+
123+
// Assert
124+
StepVerifier.create(result)
125+
.assertNext(response -> {
126+
assertNotNull(response);
127+
assertEquals(200, response.getStatusCode());
128+
// Should not be a DecodedResponse
129+
assertFalse(response instanceof StructuredMessageDecoderPolicy.DecodedResponse);
130+
})
131+
.verifyComplete();
132+
}
133+
134+
private HttpPipelineCallContext createMockContext(Context ctx, Long contentLength) throws MalformedURLException {
135+
HttpPipelineCallContext context = mock(HttpPipelineCallContext.class);
136+
HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("https://test.blob.core.windows.net/container/blob"));
137+
138+
when(context.getHttpRequest()).thenReturn(request);
139+
140+
if (ctx != null) {
141+
when(context.getData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY))
142+
.thenReturn(ctx.getData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_DECODING_CONTEXT_KEY));
143+
when(context.getData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY))
144+
.thenReturn(ctx.getData(StructuredMessageDecoderPolicy.STRUCTURED_MESSAGE_VALIDATION_OPTIONS_CONTEXT_KEY));
145+
} else {
146+
when(context.getData(any())).thenReturn(java.util.Optional.empty());
147+
}
148+
149+
return context;
150+
}
151+
152+
private HttpPipelineNextPolicy createMockNextPolicy() throws MalformedURLException {
153+
return createMockNextPolicy(1024L);
154+
}
155+
156+
private HttpPipelineNextPolicy createMockNextPolicy(Long contentLength) throws MalformedURLException {
157+
HttpPipelineNextPolicy nextPolicy = mock(HttpPipelineNextPolicy.class);
158+
HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("https://test.blob.core.windows.net/container/blob"));
159+
HttpResponse mockResponse = createMockHttpResponse(request, contentLength);
160+
when(nextPolicy.process()).thenReturn(Mono.just(mockResponse));
161+
return nextPolicy;
162+
}
163+
164+
private HttpResponse createMockHttpResponse(HttpRequest request, Long contentLength) {
165+
HttpResponse response = mock(HttpResponse.class);
166+
HttpHeaders headers = new HttpHeaders();
167+
if (contentLength != null) {
168+
headers.set(HttpHeaderName.CONTENT_LENGTH, String.valueOf(contentLength));
169+
}
170+
171+
when(response.getRequest()).thenReturn(request);
172+
when(response.getStatusCode()).thenReturn(200);
173+
when(response.getHeaders()).thenReturn(headers);
174+
when(response.getHeaderValue(HttpHeaderName.CONTENT_LENGTH.toString()))
175+
.thenReturn(contentLength != null ? String.valueOf(contentLength) : null);
176+
177+
// Mock body
178+
ByteBuffer buffer = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5});
179+
Flux<ByteBuffer> body = Flux.just(buffer);
180+
when(response.getBody()).thenReturn(body);
181+
182+
return response;
183+
}
184+
}

0 commit comments

Comments
 (0)