Skip to content

Commit

Permalink
Use Hamcrest assertions instead of JUnit in tests/apps/bookstore/book…
Browse files Browse the repository at this point in the history
…store-se - backport main (#1749) (#6142)
  • Loading branch information
Captain1653 authored Feb 10, 2023
1 parent ba0f612 commit 234bca5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
7 changes: 6 additions & 1 deletion tests/apps/bookstore/bookstore-se/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018, 2022 Oracle and/or its affiliates.
Copyright (c) 2018, 2023 Oracle and/or its affiliates.
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 @@ -80,6 +80,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* 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 @@ -35,7 +35,8 @@
import org.junit.jupiter.api.Test;

import static io.helidon.tests.apps.bookstore.se.TestServer.APPLICATION_JSON;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand Down Expand Up @@ -73,15 +74,15 @@ public void testHelloWorldHttp2SslPostFirst() throws Exception {
Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
assertEquals(postBookRes.code(), 200);
assertEquals(postBookRes.protocol(), Protocol.HTTP_2);
assertThat(postBookRes.code(), is(200));
assertThat(postBookRes.protocol(), is(Protocol.HTTP_2));
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", true);
Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
assertEquals(deleteBookRes.code(), 200);
assertEquals(deleteBookRes.protocol(), Protocol.HTTP_2);
assertThat(deleteBookRes.code(), is(200));
assertThat(deleteBookRes.protocol(), is(Protocol.HTTP_2));
}
}

Expand All @@ -95,8 +96,8 @@ public void testHelloWorldHttp2SslConcurrent() throws Exception {
executor.invokeAll(tasks).forEach(f -> {
try {
Response r = f.get(1, TimeUnit.SECONDS);
assertEquals(r.code(), 200);
assertEquals(r.protocol(), Protocol.HTTP_2);
assertThat(r.code(), is(200));
assertThat(r.protocol(), is(Protocol.HTTP_2));
} catch (Exception e) {
fail(e);
}
Expand All @@ -108,35 +109,35 @@ private void testHelloWorld(boolean compression) throws Exception {

Request getBooks = builder.build();
try (Response getBooksRes = client.newCall(getBooks).execute()) {
assertEquals(getBooksRes.code(), 200);
assertEquals(getBooksRes.protocol(), Protocol.HTTP_2);
assertThat(getBooksRes.code(), is(200));
assertThat(getBooksRes.protocol(), is(Protocol.HTTP_2));
}

Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
assertEquals(postBookRes.code(), 200);
assertEquals(postBookRes.protocol(), Protocol.HTTP_2);
assertThat(postBookRes.code(), is(200));
assertThat(postBookRes.protocol(), is(Protocol.HTTP_2));
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", true, compression);
Request getBook = builder.build();
try (Response getBookRes = client.newCall(getBook).execute()) {
assertEquals(getBookRes.code(), 200);
assertEquals(getBookRes.protocol(), Protocol.HTTP_2);
assertThat(getBookRes.code(), is(200));
assertThat(getBookRes.protocol(), is(Protocol.HTTP_2));
}

Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
assertEquals(deleteBookRes.code(), 200);
assertEquals(deleteBookRes.protocol(), Protocol.HTTP_2);
assertThat(deleteBookRes.code(), is(200));
assertThat(deleteBookRes.protocol(), is(Protocol.HTTP_2));

}

Request getNoBook = builder.build();
try (Response getNoBookRes = client.newCall(getNoBook).execute()) {
assertEquals(getNoBookRes.code(), 404);
assertEquals(getNoBookRes.protocol(), Protocol.HTTP_2);
assertThat(getNoBookRes.code(), is(404));
assertThat(getNoBookRes.protocol(), is(Protocol.HTTP_2));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,11 +26,13 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.helidon.tests.apps.bookstore.se.TestServer.APPLICATION_JSON;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class MainTest {

Expand All @@ -54,37 +56,37 @@ public void testHelloWorld() throws Exception {

Request getBooks = builder.build();
try (Response getBooksRes = client.newCall(getBooks).execute()) {
Assertions.assertEquals(200, getBooksRes.code());
Assertions.assertNotNull(getBooksRes.header("content-length"));
assertThat(getBooksRes.code(), is(200));
assertThat(getBooksRes.header("content-length"), notNullValue());
String body = getBooksRes.body().string();
Assertions.assertEquals(body, "[]");
assertThat(body, is("[]"));
}

Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
Assertions.assertEquals(200, postBookRes.code());
assertThat(postBookRes.code(), is(200));
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", false);
Request getBook = builder.build();
try (Response getBookRes = client.newCall(getBook).execute()) {
Assertions.assertEquals(200, getBookRes.code());
Assertions.assertNotNull(getBookRes.header("content-length"));
assertThat(getBookRes.code(), is(200));
assertThat(getBookRes.header("content-length"), notNullValue());
JsonReader jsonReader = Json.createReader(getBookRes.body().byteStream());
JsonObject jsonObject = jsonReader.readObject();
Assertions.assertEquals("123456", jsonObject.getString("isbn"),
"Checking if correct ISBN");
assertThat("Checking if correct ISBN", jsonObject.getString("isbn"),
is("123456"));
}

Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
Assertions.assertEquals(200, deleteBookRes.code());
assertThat(deleteBookRes.code(), is(200));
}

Request getNoBook = builder.build();
try (Response getNoBookRes = client.newCall(getNoBook).execute()) {
Assertions.assertEquals(getNoBookRes.code(), 404);
assertThat(getNoBookRes.code(), is(404));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,11 +23,12 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.helidon.tests.apps.bookstore.se.TestServer.APPLICATION_JSON;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Tests SSL/TLS with HTTP 1.1.
Expand All @@ -54,29 +55,29 @@ public void testHelloWorldSsl() throws Exception {

Request getBooks = builder.build();
try (Response getBooksRes = client.newCall(getBooks).execute()) {
Assertions.assertEquals(getBooksRes.code(), 200);
assertThat(getBooksRes.code(), is(200));
}

Request postBook = builder.post(
RequestBody.create(APPLICATION_JSON, TestServer.getBookAsJson())).build();
try (Response postBookRes = client.newCall(postBook).execute()) {
Assertions.assertEquals(postBookRes.code(), 200);
assertThat(postBookRes.code(), is(200));
}

builder = TestServer.newRequestBuilder(webServer, "/books/123456", true);
Request getBook = builder.build();
try (Response getBookRes = client.newCall(getBook).execute()) {
Assertions.assertEquals(getBookRes.code(), 200);
assertThat(getBookRes.code(), is(200));
}

Request deleteBook = builder.delete().build();
try (Response deleteBookRes = client.newCall(deleteBook).execute()) {
Assertions.assertEquals(deleteBookRes.code(), 200);
assertThat(deleteBookRes.code(), is(200));
}

Request getNoBook = builder.build();
try (Response getNoBookRes = client.newCall(getNoBook).execute()) {
Assertions.assertEquals(getNoBookRes.code(), 404);
assertThat(getNoBookRes.code(), is(404));
}
}
}

0 comments on commit 234bca5

Please sign in to comment.