Skip to content

Commit 9853520

Browse files
committed
test: Introduce AdkWebServerTest
1 parent 417a8bc commit 9853520

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
mockMvc
57+
.perform(
58+
post("/apps/test-app/users/test-user/sessions")
59+
.contentType(MediaType.APPLICATION_JSON)
60+
.content("{}"))
61+
.andExpect(status().isOk())
62+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
63+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")));
64+
}
65+
66+
@Test
67+
public void createSessionWithId_shouldReturnCreated() throws Exception {
68+
mockMvc
69+
.perform(
70+
post("/apps/test-app/users/test-user/sessions/test-session")
71+
.contentType(MediaType.APPLICATION_JSON)
72+
.content("{}"))
73+
.andExpect(status().isOk())
74+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
75+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")))
76+
.andExpect(jsonPath("$.id", Matchers.is("test-session")));
77+
}
78+
79+
@Test
80+
public void deleteSession_shouldReturnNoContent() throws Exception {
81+
mockMvc.perform(
82+
post("/apps/test-app/users/test-user/sessions/test-session-to-delete")
83+
.contentType(MediaType.APPLICATION_JSON)
84+
.content("{}"));
85+
86+
mockMvc
87+
.perform(delete("/apps/test-app/users/test-user/sessions/test-session-to-delete"))
88+
.andExpect(status().isNoContent());
89+
}
90+
91+
@Test
92+
public void getSession_shouldReturnOk() throws Exception {
93+
mockMvc.perform(
94+
post("/apps/test-app/users/test-user/sessions/test-session")
95+
.contentType(MediaType.APPLICATION_JSON)
96+
.content("{}"));
97+
98+
mockMvc
99+
.perform(get("/apps/test-app/users/test-user/sessions/test-session"))
100+
.andExpect(status().isOk())
101+
.andExpect(jsonPath("$.appName", Matchers.is("test-app")))
102+
.andExpect(jsonPath("$.userId", Matchers.is("test-user")))
103+
.andExpect(jsonPath("$.id", Matchers.is("test-session")));
104+
}
105+
106+
@Test
107+
public void listSessions_shouldReturnOk() throws Exception {
108+
mockMvc.perform(
109+
post("/apps/test-app/users/test-user/sessions/test-session-1")
110+
.contentType(MediaType.APPLICATION_JSON)
111+
.content("{}"));
112+
mockMvc.perform(
113+
post("/apps/test-app/users/test-user/sessions/test-session-2")
114+
.contentType(MediaType.APPLICATION_JSON)
115+
.content("{}"));
116+
117+
mockMvc
118+
.perform(get("/apps/test-app/users/test-user/sessions"))
119+
.andExpect(status().isOk())
120+
.andExpect(
121+
jsonPath(
122+
"$[?(@.id == 'test-session-1' || @.id == 'test-session-2')].id",
123+
Matchers.containsInAnyOrder("test-session-1", "test-session-2")));
124+
125+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session-1"));
126+
mockMvc.perform(delete("/apps/test-app/users/test-user/sessions/test-session-2"));
127+
}
128+
}

0 commit comments

Comments
 (0)