Repository navigation
Expand file tree
/
Copy pathJsonIncludeNonNullTest.java
More file actions
189 lines (155 loc) · 6.94 KB
/
Copy pathJsonIncludeNonNullTest.java
File metadata and controls
189 lines (155 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import com.github.copilot.rpc.CapiSessionOptions;
import com.github.copilot.rpc.CopilotClientOptions;
import com.github.copilot.rpc.CustomAgentConfig;
import com.github.copilot.rpc.InfiniteSessionConfig;
import com.github.copilot.rpc.InputOptions;
import com.github.copilot.rpc.MemoryConfiguration;
import com.github.copilot.rpc.ModelCapabilitiesOverride;
import com.github.copilot.rpc.ProviderConfig;
import com.github.copilot.rpc.ResumeSessionConfig;
import com.github.copilot.rpc.SessionConfig;
import com.github.copilot.rpc.SessionUiCapabilities;
import com.github.copilot.rpc.TelemetryConfig;
import com.github.copilot.rpc.UserInputRequest;
/**
* Verifies that public DTO classes in the {@code com.github.copilot.rpc}
* package are annotated with {@code @JsonInclude(JsonInclude.Include.NON_NULL)}
* so that null-valued fields are omitted during JSON serialization.
*/
class JsonIncludeNonNullTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
// --- Annotation presence checks ---
@Test
void copilotClientOptionsHasNonNullAnnotation() {
assertHasNonNullInclude(CopilotClientOptions.class);
}
@Test
void sessionConfigHasNonNullAnnotation() {
assertHasNonNullInclude(SessionConfig.class);
}
@Test
void resumeSessionConfigHasNonNullAnnotation() {
assertHasNonNullInclude(ResumeSessionConfig.class);
}
@Test
void infiniteSessionConfigHasNonNullAnnotation() {
assertHasNonNullInclude(InfiniteSessionConfig.class);
}
@Test
void memoryConfigurationHasNonNullAnnotation() {
assertHasNonNullInclude(MemoryConfiguration.class);
}
@Test
void inputOptionsHasNonNullAnnotation() {
assertHasNonNullInclude(InputOptions.class);
}
@Test
void modelCapabilitiesOverrideHasNonNullAnnotation() {
assertHasNonNullInclude(ModelCapabilitiesOverride.class);
}
@Test
void providerConfigHasNonNullAnnotation() {
assertHasNonNullInclude(ProviderConfig.class);
}
@Test
void capiSessionOptionsHasNonNullAnnotation() {
assertHasNonNullInclude(CapiSessionOptions.class);
}
@Test
void telemetryConfigHasNonNullAnnotation() {
assertHasNonNullInclude(TelemetryConfig.class);
}
@Test
void sessionUiCapabilitiesHasNonNullAnnotation() {
assertHasNonNullInclude(SessionUiCapabilities.class);
}
@Test
void customAgentConfigHasNonNullAnnotation() {
assertHasNonNullInclude(CustomAgentConfig.class);
}
@Test
void userInputRequestHasNonNullAnnotation() {
assertHasNonNullInclude(UserInputRequest.class);
}
// --- Serialization tests: null fields are omitted ---
@Test
void inputOptionsOmitsNullFieldsInJson() throws JsonProcessingException {
var opts = new InputOptions();
String json = MAPPER.writeValueAsString(opts);
assertEquals("{}", json, "All-null InputOptions should serialize to empty JSON");
}
@Test
void telemetryConfigOmitsNullFieldsInJson() throws JsonProcessingException {
var config = new TelemetryConfig();
String json = MAPPER.writeValueAsString(config);
assertEquals("{}", json, "All-null TelemetryConfig should serialize to empty JSON");
}
@Test
void sessionUiCapabilitiesOmitsNullFieldsInJson() throws JsonProcessingException {
var caps = new SessionUiCapabilities();
String json = MAPPER.writeValueAsString(caps);
assertEquals("{}", json, "All-null SessionUiCapabilities should serialize to empty JSON");
}
@Test
void userInputRequestOmitsNullFieldsInJson() throws JsonProcessingException {
var req = new UserInputRequest();
String json = MAPPER.writeValueAsString(req);
assertFalse(json.contains("null"), "UserInputRequest with no fields set should not contain 'null' values");
}
@Test
void inputOptionsIncludesSetFieldsInJson() throws JsonProcessingException {
var opts = new InputOptions();
opts.setMinLength(5);
opts.setMaxLength(100);
String json = MAPPER.writeValueAsString(opts);
assertTrue(json.contains("\"minLength\":5"), "Set minLength should appear in JSON");
assertTrue(json.contains("\"maxLength\":100"), "Set maxLength should appear in JSON");
assertFalse(json.contains("\"title\""), "Unset title should be omitted from JSON");
}
@Test
void telemetryConfigIncludesSetFieldsInJson() throws JsonProcessingException {
var config = new TelemetryConfig();
config.setOtlpEndpoint("http://localhost:4318");
String json = MAPPER.writeValueAsString(config);
assertTrue(json.contains("\"otlpEndpoint\":\"http://localhost:4318\""),
"Set otlpEndpoint should appear in JSON");
assertFalse(json.contains("\"filePath\""), "Unset filePath should be omitted from JSON");
}
@Test
void sessionUiCapabilitiesIncludesSetFieldsInJson() throws JsonProcessingException {
var caps = new SessionUiCapabilities();
caps.setElicitation(true);
String json = MAPPER.writeValueAsString(caps);
assertTrue(json.contains("\"elicitation\":true"), "Set elicitation should appear in JSON");
}
@Test
void memoryConfigurationSerializesEnabled() throws JsonProcessingException {
var memory = new MemoryConfiguration().setEnabled(true);
String json = MAPPER.writeValueAsString(memory);
assertEquals("{\"enabled\":true}", json, "MemoryConfiguration should serialize the required enabled field");
var disabled = new MemoryConfiguration().setEnabled(false);
assertEquals("{\"enabled\":false}", MAPPER.writeValueAsString(disabled),
"MemoryConfiguration should serialize enabled even when false");
}
@Test
void sessionConfigOmitsMemoryWhenUnset() throws JsonProcessingException {
var config = new SessionConfig();
String json = MAPPER.writeValueAsString(config);
assertFalse(json.contains("\"memory\""), "Unset memory should be omitted from SessionConfig JSON");
}
private void assertHasNonNullInclude(Class<?> clazz) {
JsonInclude annotation = clazz.getAnnotation(JsonInclude.class);
assertNotNull(annotation, clazz.getSimpleName() + " should be annotated with @JsonInclude");
assertEquals(JsonInclude.Include.NON_NULL, annotation.value(),
clazz.getSimpleName() + " @JsonInclude should use Include.NON_NULL");
}
}