Skip to content

Commit 67d1c3c

Browse files
Merge pull request #342 from vorburger:AdkWebServerTest
PiperOrigin-RevId: 797348659
2 parents 082e00d + c4b6b7c commit 67d1c3c

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.adk.web;
18+
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
25+
26+
import org.hamcrest.Matchers;
27+
import org.junit.jupiter.api.Test;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.http.MediaType;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
34+
/**
35+
* Integration tests for the {@link AdkWebServer}.
36+
*
37+
* <p>These tests use MockMvc to simulate HTTP requests and then verify the expected responses from
38+
* the ADK API server.
39+
*
40+
* @author <a href="http://www.vorburger.ch">Michael Vorburger.ch</a>, with Google Gemini Code
41+
* Assist in Agent mode
42+
*/
43+
@SpringBootTest
44+
@AutoConfigureMockMvc
45+
public class AdkWebServerTest {
46+
47+
@Autowired private MockMvc mockMvc;
48+
49+
@Test
50+
public void listApps_shouldReturnOkAndEmptyList() throws Exception {
51+
mockMvc.perform(get("/list-apps")).andExpect(status().isOk()).andExpect(content().json("[]"));
52+
}
53+
54+
@Test
55+
public void createSession_shouldReturnCreated() throws Exception {
56+
var result =
57+
mockMvc
58+
.perform(
59+
post("/apps/test-app/users/test-user/sessions")
60+
.contentType(MediaType.APPLICATION_JSON)
61+
.content("{}"))
62+
.andExpect(status().isOk())
63+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
64+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")))
65+
.andReturn();
66+
67+
var responseBody = result.getResponse().getContentAsString();
68+
var sessionId = com.jayway.jsonpath.JsonPath.read(responseBody, "$.id");
69+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/" + sessionId));
70+
}
71+
72+
@Test
73+
public void createSessionWithId_shouldReturnCreated() throws Exception {
74+
try {
75+
mockMvc
76+
.perform(
77+
post("/apps/test-app/users/test-user/sessions/test-session")
78+
.contentType(MediaType.APPLICATION_JSON)
79+
.content("{}"))
80+
.andExpect(status().isOk())
81+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
82+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")))
83+
.andExpect(jsonPath("$.id", Matchers.is("test-session")));
84+
} finally {
85+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session"));
86+
}
87+
}
88+
89+
@Test
90+
public void deleteSession_shouldReturnNoContent() throws Exception {
91+
mockMvc.perform(
92+
post("/apps/test-app/users/test-user/sessions/test-session-to-delete")
93+
.contentType(MediaType.APPLICATION_JSON)
94+
.content("{}"));
95+
96+
mockMvc
97+
.perform(delete("/apps/test-app/users/test-user/sessions/test-session-to-delete"))
98+
.andExpect(status().isNoContent());
99+
}
100+
101+
@Test
102+
public void getSession_shouldReturnOk() throws Exception {
103+
mockMvc.perform(
104+
post("/apps/test-app/users/test-user/sessions/test-session")
105+
.contentType(MediaType.APPLICATION_JSON)
106+
.content("{}"));
107+
108+
try {
109+
mockMvc
110+
.perform(get("/apps/test-app/users/test-user/sessions/test-session"))
111+
.andExpect(status().isOk())
112+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
113+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")))
114+
.andExpect(jsonPath("$.id", Matchers.is("test-session")));
115+
} finally {
116+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session"));
117+
}
118+
}
119+
120+
@Test
121+
public void listSessions_shouldReturnOk() throws Exception {
122+
mockMvc.perform(
123+
post("/apps/test-app/users/test-user/sessions/test-session-1")
124+
.contentType(MediaType.APPLICATION_JSON)
125+
.content("{}"));
126+
mockMvc.perform(
127+
post("/apps/test-app/users/test-user/sessions/test-session-2")
128+
.contentType(MediaType.APPLICATION_JSON)
129+
.content("{}"));
130+
131+
mockMvc
132+
.perform(get("/apps/test-app/users/test-user/sessions"))
133+
.andExpect(status().isOk())
134+
.andExpect(
135+
jsonPath(
136+
"$[?(@.id == 'test-session-1' || @.id == 'test-session-2')].id",
137+
Matchers.containsInAnyOrder("test-session-1", "test-session-2")));
138+
139+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session-1"));
140+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session-2"));
141+
}
142+
}

0 commit comments

Comments
 (0)