/
sharp1024
/
homework7
Обзор
Документация
Войти
/
sharp1024
/
homework7
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
feature/homework7
src/test/java/WebServiceTests.java
108 строк
4 KB
Single
homework7
08 янв 2026, 21:51
08 янв 2026, 21:51
23d4636
Код
Авторство
О чём код?
import org.junit.Assert; import org.junit.Test; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import sbp.server.WebService; import java.time.Duration; public class WebServiceTests { private static final String LOCALHOST = "http://localhost:12345"; private void startServer() throws Exception { WebService webService = new WebService(); Thread serverThread = new Thread(() -> webService.start()); serverThread.start(); int maxAttempts = 10; int currentAttempt = 0; boolean serverIsReady = false; while (currentAttempt < maxAttempts && !serverIsReady) { try { sendRequest("/", "ping", null); serverIsReady = true; } catch (Exception e) { currentAttempt++; try { Thread.sleep(500); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } } } if (!serverIsReady) { throw new Exception("Server is not ready"); } } private static HttpResponse<String> sendRequest(String path, String method, String body) throws IOException, InterruptedException { HttpResponse<String> response = null; HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(LOCALHOST + path)) .header("Content-Type", "application/json") .timeout(Duration.ofSeconds(5)) .method(method, body != null ? HttpRequest.BodyPublishers.ofString(body) : HttpRequest.BodyPublishers.noBody()) .build(); response = client.send(request, HttpResponse.BodyHandlers.ofString()); return response; } @Test public void testAdd() throws Exception { startServer(); HttpResponse<String> response = sendRequest("/", "add", "{ \"name\": \"Neo\", \"city\": \"Sydney\", \"age\": 32 }"); Assert.assertEquals("Saved: Neo", response.body()); Assert.assertEquals(200, response.statusCode()); } @Test public void testDelete() throws Exception { startServer(); sendRequest("/", "add", "{ \"name\": \"Neo\", \"city\": \"Sydney\", \"age\": 32 }"); sendRequest("/", "add", "{ \"name\": \"MeO\", \"city\": \"Sydney\", \"age\": 38 }"); HttpResponse<String> response = sendRequest("/", "delete", "{ \"name\": \"Neo\" }"); Assert.assertEquals("Deleted: Neo", response.body()); Assert.assertEquals(200, response.statusCode()); } @Test public void testDeleteFail() throws Exception { startServer(); sendRequest("/", "add", "{ \"name\": \"Neo\", \"city\": \"Sydney\", \"age\": 32 }"); sendRequest("/", "add", "{ \"name\": \"MeO\", \"city\": \"Sydney\", \"age\": 38 }"); HttpResponse<String> response = sendRequest("/", "delete", "{ \"name\": \"N1eo\" }"); Assert.assertEquals("Error not found: N1eo", response.body()); Assert.assertEquals(200, response.statusCode()); } @Test public void testList() throws Exception { startServer(); sendRequest("/", "add", "{ \"name\": \"Neo\", \"city\": \"Sydney\", \"age\": 32 }"); sendRequest("/", "add", "{ \"name\": \"MeO\", \"city\": \"Sydney\", \"age\": 38 }"); HttpResponse<String> response = sendRequest("/", "list", "{ \"name\": \"Neo\" }"); Assert.assertNotEquals("<Empty>", response.body()); Assert.assertEquals(200, response.statusCode()); } @Test public void testEmptyList() throws Exception { startServer(); HttpResponse<String> response = sendRequest("/", "list", ""); Assert.assertEquals("<empty>", response.body()); Assert.assertEquals(200, response.statusCode()); } }