Skip to content

Commit bbb3c07

Browse files
committed
test(internal): Cover RequestContext equals/hashCode/toString
11 tests exercising structural equality on the byte[] component, the record-pattern destructure, null tolerance, hashCode stability, and the custom toString that prints body length instead of the raw array reference.
1 parent 0d47046 commit bbb3c07

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.retailsvc.http.internal;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Map;
6+
import org.junit.jupiter.api.Test;
7+
8+
class RequestContextTest {
9+
10+
private static final byte[] BODY_A = {1, 2, 3};
11+
private static final byte[] BODY_A_COPY = {1, 2, 3};
12+
private static final byte[] BODY_B = {1, 2, 4};
13+
14+
private RequestContext context(byte[] body, Object parsed, String opId, Map<String, String> pp) {
15+
return new RequestContext(body, parsed, opId, pp);
16+
}
17+
18+
@Test
19+
void equalsIsReflexive() {
20+
RequestContext c = context(BODY_A, "p", "op", Map.of("k", "v"));
21+
assertThat(c).isEqualTo(c);
22+
}
23+
24+
@Test
25+
void equalsTreatsByteArraysStructurally() {
26+
RequestContext a = context(BODY_A, "p", "op", Map.of("k", "v"));
27+
RequestContext b = context(BODY_A_COPY, "p", "op", Map.of("k", "v"));
28+
assertThat(a).isEqualTo(b);
29+
assertThat(a).hasSameHashCodeAs(b);
30+
}
31+
32+
@Test
33+
void equalsRejectsDifferentBytes() {
34+
RequestContext a = context(BODY_A, "p", "op", Map.of("k", "v"));
35+
RequestContext b = context(BODY_B, "p", "op", Map.of("k", "v"));
36+
assertThat(a).isNotEqualTo(b);
37+
}
38+
39+
@Test
40+
void equalsRejectsDifferentParsedBody() {
41+
RequestContext a = context(BODY_A, "p", "op", Map.of("k", "v"));
42+
RequestContext b = context(BODY_A, "different", "op", Map.of("k", "v"));
43+
assertThat(a).isNotEqualTo(b);
44+
}
45+
46+
@Test
47+
void equalsRejectsDifferentOperationId() {
48+
RequestContext a = context(BODY_A, "p", "op", Map.of("k", "v"));
49+
RequestContext b = context(BODY_A, "p", "other-op", Map.of("k", "v"));
50+
assertThat(a).isNotEqualTo(b);
51+
}
52+
53+
@Test
54+
void equalsRejectsDifferentPathParameters() {
55+
RequestContext a = context(BODY_A, "p", "op", Map.of("k", "v"));
56+
RequestContext b = context(BODY_A, "p", "op", Map.of("k", "other"));
57+
assertThat(a).isNotEqualTo(b);
58+
}
59+
60+
@Test
61+
void equalsRejectsNullAndOtherTypes() {
62+
RequestContext c = context(BODY_A, "p", "op", Map.of());
63+
assertThat(c).isNotEqualTo(null);
64+
assertThat(c).isNotEqualTo("not a context");
65+
}
66+
67+
@Test
68+
void equalsHandlesNullParsedBody() {
69+
RequestContext a = context(BODY_A, null, "op", Map.of());
70+
RequestContext b = context(BODY_A_COPY, null, "op", Map.of());
71+
assertThat(a).isEqualTo(b);
72+
}
73+
74+
@Test
75+
void hashCodeIsStableAcrossInvocations() {
76+
RequestContext c = context(BODY_A, "p", "op", Map.of("k", "v"));
77+
int first = c.hashCode();
78+
int second = c.hashCode();
79+
assertThat(first).isEqualTo(second);
80+
}
81+
82+
@Test
83+
void toStringSummarisesBodyByLength() {
84+
RequestContext c = context(BODY_A, "parsed", "get-x", Map.of("id", "42"));
85+
assertThat(c.toString())
86+
.contains("body=byte[3]")
87+
.contains("parsedBody=parsed")
88+
.contains("operationId=get-x")
89+
.contains("pathParameters={id=42}")
90+
.doesNotContain("[B@");
91+
}
92+
93+
@Test
94+
void toStringHandlesNullBody() {
95+
RequestContext c = context(null, null, "op", Map.of());
96+
assertThat(c.toString()).contains("body=byte[0]");
97+
}
98+
}

0 commit comments

Comments
 (0)