Skip to content

Commit

Permalink
Added tests for update-page REST API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Jan 23, 2025
1 parent a958ebe commit aec2851
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ spec:
* `page-name` - name of the new page (optional). If you miss this argument, name is generated automatically
* `page-start` - start time of the new page (required)
* `page-comment` - comment for the new page (optional)
* `/update-page?book-id=test_book&page-name=first_page&new-page-comment=my_second_page&new-page-name=second_page` - update existed page by arguments:
* `book-id` - name (id) where the new page will be created (required).
* `page-name` - name of the updated page (required).
* `new-page-name` - new name for updated page (optional). Only pages in future can be renamed.
* `new-page-comment` - new comment for updated page (optional). Comment of any page can be updated
* `/remove-page?book-id=test_book&page-name=first_page` - removes a page by arguments:
* `book-id` - name (id) where the removed page is (required).
* `page-name` - name of the removed page (required).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Exactpro (Exactpro Systems Limited)
* Copyright 2022-2025 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,14 +30,17 @@
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.LocalConnector;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;

import java.io.IOException;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Tag("integration")
@Th2IntegrationTest
public class AbstractHttpTest {
Expand Down Expand Up @@ -81,14 +84,18 @@ protected HttpTester.Response executeGet(String query) throws Exception {

protected void checkPlainResponse(String str, boolean status, String test) {
PlainResponse rsp = new PlainResponse(str.trim());
Assertions.assertEquals(status ? "Success": "Failed", rsp.status);
Assertions.assertEquals(test, rsp.comment);
assertAll(
() -> assertEquals(status ? "Success" : "Failed", rsp.status, rsp.toString()),
() -> assertEquals(test, rsp.comment)
);
}

protected void checkPlainResponseContains(String body, boolean expected, String test) {
protected void checkPlainResponseContains(String body, boolean status, String test) {
PlainResponse rsp = new PlainResponse(body.trim());
Assertions.assertEquals(rsp.status, expected ? "Success": "Failed", rsp.toString());
Assertions.assertTrue(rsp.comment.contains(test), () -> String.format("Comment should contain text: %s but actual text is %s", test, rsp.comment));
assertAll(
() -> assertEquals(status ? "Success" : "Failed", rsp.status, rsp.toString()),
() -> assertTrue(rsp.comment.contains(test), () -> String.format("Comment should contain text: %s but actual text is %s", test, rsp.comment))
);
}

protected void addBook(BookToAdd bookToAdd) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Exactpro (Exactpro Systems Limited)
* Copyright 2022-2025 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -124,6 +124,140 @@ public void addPageWithParamsTest() throws Exception {
assertEquals(comment, page.getComment());
}

@Test
public void updatePageCommentTest() throws Exception {
String bookName = "updatePageCommentTest";
String pageName = "page2";
String pageComment = "update-page-comment-test";
Instant time = Instant.now().minus(2, ChronoUnit.MINUTES);
addBook(new BookToAdd(bookName, Instant.now()));

BookId bookId = new BookId(bookName);
PageId pageId = new PageId(bookId, time, pageName);
BookInfo bookInfo = this.storage.getBook(bookId);

HttpTester.Response response = this.executeGet(String.format("/new-page?book-id=%s&page-name=%s&page-start=%s",
bookName, pageName, time));
assertEquals(200, response.getStatus());

bookInfo.refresh();
PageInfo pageInfo = bookInfo.getPage(pageId);
assertNotNull(pageInfo);
assertNull(pageInfo.getComment());

response = this.executeGet(String.format("/update-page?book-id=%s&page-name=%s&new-page-comment=%s",
bookName, pageName, pageComment));
assertEquals(200, response.getStatus());
this.checkPlainResponseContains(response.getContent(), true,
String.format("Page updated bookId = %s,pageName = %s,pageStart = %s,pageComment = %s,updated = ", bookName.toLowerCase(), pageName, time, pageComment));

bookInfo.refresh();
pageInfo = bookInfo.getPage(pageId);
assertNotNull(pageInfo);
assertEquals(pageComment, pageInfo.getComment());
}

@Test
public void updatePageNameTest() throws Exception {
String bookName = "updatePageNameTest";
String pageNameFirst = "page-first";
String pageNameSecond = "page-second";
Instant time = Instant.now().plus(2, ChronoUnit.MINUTES);
addBook(new BookToAdd(bookName, Instant.now()));

BookId bookId = new BookId(bookName);
PageId pageIdFirst = new PageId(bookId, time, pageNameFirst);
PageId pageIdSecond = new PageId(bookId, time, pageNameSecond);
BookInfo bookInfo = this.storage.getBook(bookId);

HttpTester.Response response = this.executeGet(String.format("/new-page?book-id=%s&page-name=%s&page-start=%s",
bookName, pageNameFirst, time));
assertEquals(200, response.getStatus());

bookInfo.refresh();
assertNotNull(bookInfo.getPage(pageIdFirst));
assertNull(bookInfo.getPage(pageIdSecond));

response = this.executeGet(String.format("/update-page?book-id=%s&page-name=%s&new-page-name=%s",
bookName, pageNameFirst, pageNameSecond));
assertEquals(200, response.getStatus());
this.checkPlainResponseContains(response.getContent(), true,
String.format("Page updated bookId = %s,pageName = %s,pageStart = %s,updated = ", bookName.toLowerCase(), pageIdSecond.getName(), time));

bookInfo.refresh();
assertNull(bookInfo.getPage(pageIdFirst));
assertNotNull(bookInfo.getPage(pageIdSecond));
}

@Test
public void updatePageNameInPastTest() throws Exception {
String bookName = "updatePageNameInPastTest";
String pageNameFirst = "page-first";
String pageNameSecond = "page-second";
Instant time = Instant.now().minus(2, ChronoUnit.MINUTES);
addBook(new BookToAdd(bookName, Instant.now()));

BookId bookId = new BookId(bookName);
PageId pageIdFirst = new PageId(bookId, time, pageNameFirst);
PageId pageIdSecond = new PageId(bookId, time, pageNameSecond);
BookInfo bookInfo = this.storage.getBook(bookId);

HttpTester.Response response = this.executeGet(String.format("/new-page?book-id=%s&page-name=%s&page-start=%s",
bookName, pageNameFirst, time));
assertEquals(200, response.getStatus());

bookInfo.refresh();
assertNotNull(bookInfo.getPage(pageIdFirst));
assertNull(bookInfo.getPage(pageIdSecond));

response = this.executeGet(String.format("/update-page?book-id=%s&page-name=%s&new-page-name=%s",
bookName, pageNameFirst, pageNameSecond));
assertEquals(200, response.getStatus());
this.checkPlainResponseContains(response.getContent(), false,
String.format("You can only rename pages which start more than 200 ms in future: pageStart - %s, now + threshold - ", time));

bookInfo.refresh();
assertNotNull(bookInfo.getPage(pageIdFirst));
assertNull(bookInfo.getPage(pageIdSecond));
}

@Test
public void updatePageTest() throws Exception {
String bookName = "updatePageTest";
String pageNameFirst = "page-first";
String pageNameSecond = "page-second";
String pageComment = "update-page-comment-test";
Instant time = Instant.now().plus(2, ChronoUnit.MINUTES);
addBook(new BookToAdd(bookName, Instant.now()));

BookId bookId = new BookId(bookName);
PageId pageIdFirst = new PageId(bookId, time, pageNameFirst);
PageId pageIdSecond = new PageId(bookId, time, pageNameSecond);
BookInfo bookInfo = this.storage.getBook(bookId);

HttpTester.Response response = this.executeGet(String.format("/new-page?book-id=%s&page-name=%s&page-start=%s",
bookName, pageNameFirst, time));
assertEquals(200, response.getStatus());

bookInfo.refresh();
PageInfo pageInfo = bookInfo.getPage(pageIdFirst);
assertNotNull(pageInfo);
assertNull(bookInfo.getPage(pageIdSecond));
assertNull(pageInfo.getComment());

response = this.executeGet(String.format("/update-page?book-id=%s&page-name=%s&new-page-name=%s&new-page-comment=%s",
bookName, pageNameFirst, pageNameSecond, pageComment));
assertEquals(200, response.getStatus());
this.checkPlainResponseContains(response.getContent(), true,
String.format("Page updated bookId = %s,pageName = %s,pageStart = %s,pageComment = %s,updated = ", bookName.toLowerCase(), pageIdSecond.getName(), time, pageComment));

bookInfo.refresh();
pageInfo = bookInfo.getPage(pageIdSecond);
assertNull(bookInfo.getPage(pageIdFirst));
assertNotNull(pageInfo);
assertEquals(pageComment, pageInfo.getComment());
}

@Test
public void removePageTest() throws Exception {
int initNumberOfBooks = storage.listBooks().size();
Expand Down Expand Up @@ -219,7 +353,7 @@ public void insertIntoGageGapTest() throws Exception {
assertEquals(2, bookInfo.getPages().size());

HttpTester.Response response = this.executeGet(String.format("/new-page?book-id=%s&page-name=%s&page-start=%s",
bookName, pageToInsert, page2Start.toString()));
bookName, pageToInsert, page2Start));
assertEquals(200, response.getStatus());
this.checkPlainResponse(response.getContent(), true,
String.format("Page created bookId = %s,pageName = %s,pageStart = %s", bookName.toLowerCase(), pageToInsert, page2Start));
Expand Down

0 comments on commit aec2851

Please sign in to comment.