View Javadoc
1   package de.dlr.shepard.integrationtests;
2   
3   import static io.restassured.RestAssured.given;
4   import static org.assertj.core.api.Assertions.assertThat;
5   
6   import de.dlr.shepard.ErrorResponse;
7   import de.dlr.shepard.common.util.Constants;
8   import de.dlr.shepard.context.collection.io.CollectionIO;
9   import de.dlr.shepard.context.collection.io.DataObjectIO;
10  import de.dlr.shepard.context.labJournal.io.LabJournalEntryIO;
11  import io.quarkus.test.junit.QuarkusIntegrationTest;
12  import org.junit.jupiter.api.BeforeAll;
13  import org.junit.jupiter.api.MethodOrderer;
14  import org.junit.jupiter.api.Order;
15  import org.junit.jupiter.api.Test;
16  import org.junit.jupiter.api.TestMethodOrder;
17  
18  @QuarkusIntegrationTest
19  @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
20  public class LabJournalIT extends BaseTestCaseIT {
21  
22    private static String labJournalURL;
23    private static CollectionIO collection;
24    private static DataObjectIO dataObject;
25    private static LabJournalEntryIO labJournal;
26  
27    private static String validJournalEntryHtml =
28      """
29      <h3>This is my heading</h3>
30      <p>Here some <strong>bold text</strong>, some <em>italic text</em>, some <u>underline text</u>, some <code>code text</code></p>
31      <p>left</p><p style="text-align: center">center</p><p style="text-align: right">right</p><p></p>
32      <p><a target="_blank" rel="noopener noreferrer nofollow" href="https://shepard.xyz">This is a link</a></p>
33      <p></p>
34      <ul><li><p>List 1</p><ul><li><p>List 2</p></li></ul></li></ul>
35      <ol><li><p>List 1.1</p><ol><li><p>List 2.2</p></li></ol></li></ol><p></p>
36      <table style="min-width: 75px"><colgroup><col style="min-width: 25px">
37      <col style="min-width: 25px">
38      <col style="min-width: 25px"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>1</p></th>
39      <th colspan="1" rowspan="1"><p>2</p></th><th colspan="1" rowspan="1"><p>3</p></th></tr><tr>
40      <td colspan="1" rowspan="1"><p>3</p></td><td colspan="1" rowspan="1"><p>2</p></td>
41      <td colspan="1" rowspan="1"><p>1</p></td></tr><tr><td colspan="1" rowspan="1"><p>c</p></td>
42      <td colspan="1" rowspan="1"><p>b</p></td><td colspan="1" rowspan="1"><p>a</p></td></tr></tbody></table>
43      """;
44  
45    private static String invalidJournalEntryHtml =
46      """
47      <h1>This is my heading</h1>
48      <p>Here some <strong>bold text</strong>, some <em>italic text</em>, some <u>underline text</u>, some <code>code text</code></p>
49      <p>left</p><p style="text-align: center">center</p><p style="text-align: right">right</p><p></p>
50      <p><a target="_blank" rel="noopener noreferrer nofollow" href="https://shepard.xyz">This is a link</a></p>
51      <p></p>
52      <ul><li><p>List 1</p><ul><li><p>List 2</p></li></ul></li></ul>
53      <ol><li><p>List 1.1</p><ol><li><p>List 2.2</p></li></ol></li></ol><p></p>
54      <table style="min-width: 75px"><colgroup><col style="min-width: 25px">
55      <col style="min-width: 25px">
56      <script>alert("dangerous")</script>
57      """;
58  
59    @BeforeAll
60    public static void setUp() {
61      collection = createCollection("labJournalTestCollection");
62      dataObject = createDataObject("TimeseriesReferenceTestDataObject", collection.getId());
63  
64      labJournalURL = "/" + Constants.LAB_JOURNAL_ENTRIES;
65    }
66  
67    @Test
68    @Order(1)
69    public void postLabJournal_Success() {
70      LabJournalEntryIO payload = new LabJournalEntryIO();
71      payload.setJournalContent(validJournalEntryHtml);
72      LabJournalEntryIO actual = given()
73        .spec(requestSpecOfDefaultUser)
74        .body(payload)
75        .queryParam("dataObjectId", dataObject.getId())
76        .when()
77        .post(labJournalURL)
78        .then()
79        .statusCode(201)
80        .extract()
81        .as(LabJournalEntryIO.class);
82      labJournal = actual;
83    }
84  
85    @Test
86    @Order(2)
87    public void postLabJournal_Failure_invalidHtml() {
88      LabJournalEntryIO payload = new LabJournalEntryIO();
89      payload.setJournalContent(invalidJournalEntryHtml);
90      given()
91        .spec(requestSpecOfDefaultUser)
92        .body(payload)
93        .queryParam("dataObjectId", dataObject.getId())
94        .when()
95        .post(labJournalURL)
96        .then()
97        .statusCode(400);
98    }
99  
100   @Test
101   @Order(3)
102   public void getLabJournal_exists_success() {
103     LabJournalEntryIO actual = given()
104       .spec(requestSpecOfDefaultUser)
105       .when()
106       .get(labJournalURL + "/" + labJournal.getId())
107       .then()
108       .statusCode(200)
109       .extract()
110       .as(LabJournalEntryIO.class);
111 
112     assertThat(actual).isEqualTo(labJournal);
113   }
114 
115   @Test
116   @Order(4)
117   public void getLabJournal_doesNotExist_notFound() {
118     ErrorResponse actual = given()
119       .spec(requestSpecOfDefaultUser)
120       .when()
121       .get(labJournalURL + "/99999")
122       .then()
123       .statusCode(404)
124       .extract()
125       .as(ErrorResponse.class);
126 
127     assertThat(actual.getMessage()).isEqualTo("LabJournal with Id 99999 cannot be found or is deleted");
128   }
129 
130   @Test
131   @Order(5)
132   public void getLabJournals_noQueryParam_badRequest() {
133     given().spec(requestSpecOfDefaultUser).when().get(labJournalURL).then().statusCode(400);
134   }
135 
136   @Test
137   @Order(6)
138   public void getLabJournals_dataObjectDoesExist_success() {
139     var actual = given()
140       .spec(requestSpecOfDefaultUser)
141       .when()
142       .get(labJournalURL + "?dataObjectId=" + dataObject.getId())
143       .then()
144       .statusCode(200)
145       .extract()
146       .as(LabJournalEntryIO[].class);
147 
148     assertThat(actual).contains(labJournal);
149   }
150 
151   @Test
152   @Order(7)
153   public void getLabJournals_dataObjectDoesNotExist_notFound() {
154     given().spec(requestSpecOfDefaultUser).when().get(labJournalURL + "?dataObjectId=99999").then().statusCode(404);
155   }
156 
157   @Test
158   @Order(8)
159   public void deleteLabJournal_Success() {
160     given()
161       .spec(requestSpecOfDefaultUser)
162       .when()
163       .delete(labJournalURL + "/" + labJournal.getId())
164       .then()
165       .statusCode(204);
166   }
167 }