Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<version>5.3.20</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -92,7 +98,7 @@
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>3.9</version>
<version>3.10.3</version>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
});
}
}
Original file line number Diff line number Diff line change
@@ -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<NameValuePair> parameters = new ArrayList<NameValuePair>();
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<NameValuePair> parameters = new ArrayList<NameValuePair>();
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<NameValuePair> parameters = new ArrayList<NameValuePair>();
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<NameValuePair> parameters = new ArrayList<NameValuePair>();
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;
});
}
}
47 changes: 47 additions & 0 deletions src/test/java/com/acme/modres/servlet/LogoutServletGetIT.java
Original file line number Diff line number Diff line change
@@ -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;
});
}

}
Loading