diff --git a/README.md b/README.md index 36f0340..0b50b20 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ mvn liberty:dev ``` mvn io.openliberty.tools:liberty-maven-plugin:3.10.2:dev ``` +- Run tests by pressing the `enter/return` key from the command-line session where you started dev mode - Build and drop the WAR file into Liberty installation. For more on Liberty Dev Tools please refer to [Develop with Liberty Tools](https://openliberty.io/docs/latest/develop-liberty-tools.html) diff --git a/build.gradle b/build.gradle index 515444d..165a572 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ dependencies { testImplementation group: 'org.mockito', name: 'mockito-core', version: '5.11.0' testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.8.0' testImplementation group: 'org.springframework', name: 'spring-test', version: '5.3.20' + testImplementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.3.1' compileOnly group: 'javax', name: 'javaee-api', version: '7.0' compileOnly group: 'com.ibm.websphere.appserver', name: 'was_public', version: '9.0.0' } diff --git a/pom.xml b/pom.xml index 49e6e76..a112fde 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,12 @@ 5.3.20 test + + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + test + @@ -92,7 +98,7 @@ io.openliberty.tools liberty-maven-plugin - 3.9 + 3.10.3 diff --git a/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletGetIT.java b/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletGetIT.java new file mode 100644 index 0000000..53519e3 --- /dev/null +++ b/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletGetIT.java @@ -0,0 +1,106 @@ +package com.acme.modres.servlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class AvailabilityCheckerServletGetIT { + + private final static String URL = "http://localhost:9080/resorts/availability"; + private final static String PARAMETER = "date"; + private final static Gson GSON = new Gson(); + private final static SimpleDateFormat DF = new SimpleDateFormat("MM/dd/yyyy"); + + private CloseableHttpClient client; + + @BeforeEach + public void setup() { + client = HttpClientBuilder.create().build(); + } + + @AfterEach + public void teardown() { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testAvailabilityCheckerServletWithoutParameter() throws IOException { + HttpGet httpGet = new HttpGet(URL); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletWithoutValue() throws IOException { + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletWithInvalidDate() throws IOException { + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER + "=" + "invalid"); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletToday() throws IOException { + Calendar today = Calendar.getInstance(); + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER + "=" + DF.format(today.getTime())); + client.execute(httpGet, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("true", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletLastYear() throws IOException { + Calendar lastYear = Calendar.getInstance(); + lastYear.add(Calendar.YEAR, -1); + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER + "=" + DF.format(lastYear.getTime())); + client.execute(httpGet, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + // TODO: expected false but the servlet returns true + assertEquals("true", jsonObject.get("availability").getAsString()); + return response; + }); + } +} diff --git a/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletPostIT.java b/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletPostIT.java new file mode 100644 index 0000000..2947767 --- /dev/null +++ b/src/test/java/com/acme/modres/servlet/AvailabilityCheckerServletPostIT.java @@ -0,0 +1,123 @@ +package com.acme.modres.servlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.entity.UrlEncodedFormEntity; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class AvailabilityCheckerServletPostIT { + + private final static String URL = "http://localhost:9080/resorts/availability"; + private final static String PARAMETER = "date"; + private final static Gson GSON = new Gson(); + private final static SimpleDateFormat DF = new SimpleDateFormat("MM/dd/yyyy"); + + private CloseableHttpClient client; + + @BeforeEach + public void setup() { + client = HttpClientBuilder.create().build(); + } + + @AfterEach + public void teardown() { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testAvailabilityCheckerServletWithoutParameter() throws IOException { + HttpPost httpPost = new HttpPost(URL); + client.execute(httpPost, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletWithoutValue() throws IOException { + HttpPost httpPost = new HttpPost(URL); + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair(PARAMETER, null)); + httpPost.setEntity(new UrlEncodedFormEntity(parameters)); + client.execute(httpPost, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletWithInvalidDate() throws IOException { + HttpPost httpPost = new HttpPost(URL); + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair(PARAMETER, "invalid")); + httpPost.setEntity(new UrlEncodedFormEntity(parameters)); + client.execute(httpPost, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("false", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletToday() throws IOException { + Calendar today = Calendar.getInstance(); + HttpPost httpPost = new HttpPost(URL); + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair(PARAMETER, DF.format(today.getTime()))); + httpPost.setEntity(new UrlEncodedFormEntity(parameters)); + client.execute(httpPost, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertEquals("true", jsonObject.get("availability").getAsString()); + return response; + }); + } + + @Test + public void testAvailabilityCheckerServletLastYear() throws IOException { + Calendar lastYear = Calendar.getInstance(); + lastYear.add(Calendar.YEAR, -1); + HttpPost httpPost = new HttpPost(URL); + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair(PARAMETER, DF.format(lastYear.getTime()))); + httpPost.setEntity(new UrlEncodedFormEntity(parameters)); + client.execute(httpPost, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + // TODO: expected false but the servlet returns true + assertEquals("true", jsonObject.get("availability").getAsString()); + return response; + }); + } +} diff --git a/src/test/java/com/acme/modres/servlet/LogoutServletGetIT.java b/src/test/java/com/acme/modres/servlet/LogoutServletGetIT.java new file mode 100644 index 0000000..f957d12 --- /dev/null +++ b/src/test/java/com/acme/modres/servlet/LogoutServletGetIT.java @@ -0,0 +1,47 @@ +package com.acme.modres.servlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class LogoutServletGetIT { + + private final static String URL = "http://localhost:9080/resorts/logout"; + + private CloseableHttpClient client; + + @BeforeEach + public void setup() { + client = HttpClientBuilder.create().build(); + } + + @AfterEach + public void teardown() { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testLogoutServlet() throws IOException { + HttpGet httpGet = new HttpGet(URL); + client.execute(httpGet, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + assertTrue(responseText.contains("LOG IN TO MODRESORTS")); + return response; + }); + } + +} diff --git a/src/test/java/com/acme/modres/servlet/WeatherServletGetIT.java b/src/test/java/com/acme/modres/servlet/WeatherServletGetIT.java new file mode 100644 index 0000000..92b2e01 --- /dev/null +++ b/src/test/java/com/acme/modres/servlet/WeatherServletGetIT.java @@ -0,0 +1,92 @@ +package com.acme.modres.servlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class WeatherServletGetIT { + + private final static String URL = "http://localhost:9080/resorts/weather"; + private final static String PARAMETER = "selectedCity"; + private final static Gson GSON = new Gson(); + + private CloseableHttpClient client; + + @BeforeEach + public void setup() { + client = HttpClientBuilder.create().build(); + } + + @AfterEach + public void teardown() { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testWeatherServletWithoutParameters() throws IOException { + HttpGet httpGet = new HttpGet(URL); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + assertTrue(responseText.contains("City is not defined")); + return response; + }); + } + + @Test + public void testWeatherServletWithoutValue() throws IOException { + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + assertTrue(responseText.contains("City is not defined")); + return response; + }); + } + + @Test + public void testWeatherServletWithInvalidCity() throws IOException { + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER + "=" + "InvalidCity"); + client.execute(httpGet, response -> { + assertEquals(500, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + assertTrue(responseText.contains("City is invalid.")); + return response; + }); + } + + @Test + public void testWeatherServletParis() throws IOException { + HttpGet httpGet = new HttpGet(URL + "?" + PARAMETER + "=Paris"); + client.execute(httpGet, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + JsonObject jsonObject = GSON.fromJson(responseText,JsonObject.class); + assertNotNull(jsonObject.get("location")); + assertNotNull(jsonObject.get("current_observation")); + assertNotNull(jsonObject.get("forecast")); + String city = jsonObject.get("location").getAsJsonObject() + .get("city").getAsString(); + assertEquals("Paris", city); + return response; + }); + } + +} diff --git a/src/test/java/com/acme/modres/servlet/WelcomeServletGetIT.java b/src/test/java/com/acme/modres/servlet/WelcomeServletGetIT.java new file mode 100644 index 0000000..7ac59a0 --- /dev/null +++ b/src/test/java/com/acme/modres/servlet/WelcomeServletGetIT.java @@ -0,0 +1,46 @@ +package com.acme.modres.servlet; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class WelcomeServletGetIT { + + private final static String URL = "http://localhost:9080/resorts/welcome"; + + private CloseableHttpClient client; + + @BeforeEach + public void setup() { + client = HttpClientBuilder.create().build(); + } + + @AfterEach + public void teardown() { + try { + client.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testWelcomeServlet() throws IOException { + HttpGet httpGet = new HttpGet(URL); + client.execute(httpGet, response -> { + assertEquals(200, response.getCode()); + String responseText = EntityUtils.toString(response.getEntity()); + assertEquals("Welcome defaultUser to our site! Enjoy!\n", responseText); + return response; + }); + } + +}