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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
postgresql:
primary:
podSecurityContext:
enabled: true
fsGroup: 1001
containerSecurityContext:
enabled: true
runAsUser: 1001
runAsGroup: 0
runAsNonRoot: true
allowPrivilegeEscalation: false
seccompProfile:
type: RuntimeDefault
capabilities:
drop:
- ALL
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function installPrerequisites()
{
echo "Adding bitnami repository"
helm repo add bitnami https://charts.bitnami.com/bitnami

helm dependency update charts/
}

function helmInstall() {
application="${1}"
helm_set_arguments="$2"

# TODO https://issues.redhat.com/browse/WFLY-18574 remove this when persistence is working
helm_set_arguments="${helm_set_arguments} -f ../.ci/openshift-ci/build-root/scripts/qs-overrides/todo-jakarta-data/ci.yaml"

helm install "${application}" charts/ --wait --timeout="${helm_install_timeout}" ${helm_set_arguments}
echo $?
}



function cleanPrerequisites()
{
helm uninstall "${application}"
helm repo remove bitnami
}

function getHelmSetVariablePrefix() {
echo "wildfly."
}

function helmInstallFailed() {
echo "----> Getting status of all pods"
kubectl get pods
echo "----> Checking logs for postgres pod"
kubectl logs todo-backend-postgresql-0
echo "----> Checking events"
kubectl get events
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
<module>tasks-jsf</module>
<module>temperature-converter</module>
<module>todo-backend</module>
<module>todo-jakarta-data</module>
<module>thread-racing</module>
<module>websocket-endpoint</module>
<module>websocket-hello</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ public void setUrl(URL url) {
this.url = url;
}

public void update(ToDo newTodo) {
this.title = ofNullable(newTodo.title).orElse(title);
this.completed = ofNullable(newTodo.completed).orElse(completed);
this.order = ofNullable(newTodo.order).orElse(order);
this.url = ofNullable(newTodo.url).orElse(url);
/**
* Update the fields of this object with the values of any non-null fields in the given {@code patch}.
* @param patch the source of updated values
*/
public void update(ToDo patch) {
this.title = ofNullable(patch.title).orElse(title);
this.completed = ofNullable(patch.completed).orElse(completed);
this.order = ofNullable(patch.order).orElse(order);
this.url = ofNullable(patch.url).orElse(url);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ public ToDo addTodo(ToDo todo) throws MalformedURLException {
@Consumes(APPLICATION_JSON)
@Transactional
public ToDo updateTodo(@PathParam("id") long id, ToDo update) {
return todoDAO.update(id, update).orElseThrow(() -> new NotFoundException("ToDo does not exist!"));
return todoDAO.patch(id, update).orElseThrow(() -> new NotFoundException("ToDo does not exist!"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,16 @@ public interface ToDoDAO {

void insert(ToDo todo);

Optional<ToDo> update(Long id, ToDo todo);
/**
* Patch the {@code ToDo} with the given id, if one exists. "Patch" here is in the
* general sense of the HTTP PATCH method, where parts of an existing resource are modified
* based on a provided entity, but others are left as is. Here if the provided {@code patch}
* {@code ToDo} has a non-null value for a field, this object's field is updated to match,
* but for null fields this object's current value is retained.
*
* @param id the id of the ToDo to update. Cannot be {@code null}
* @param patch a ToDo to persist if one with the id exists. Cannot be {@code null}
* @return an {@link Optional} containing the updated ToDo if a ToDo with {@code id} existed
*/
Optional<ToDo> patch(Long id, ToDo patch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void insert(ToDo todo) {
}

@Override
public Optional<ToDo> update(Long id, ToDo newTodo) {
public Optional<ToDo> patch(Long id, ToDo newTodo) {
Optional<ToDo> optional = findById(id);
if (optional.isPresent()) {
optional.get().update(newTodo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.wildfly.quickstarts.todos;

import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
Expand All @@ -30,9 +32,13 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

import jakarta.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
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 org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -63,29 +69,84 @@ URL getRequestUrl() {
}
}

URL getRequestUrl(Long id) {
String host = getServerHost();
if (host == null) {
host = REST_TARGET_URL;
}
try {
return new URI(host+"/todo-backend/" + id).toURL();
} catch (URISyntaxException | MalformedURLException ex) {
throw new RuntimeException(ex);
}
}

@Test
public void testCRUD() throws Exception {
WebTarget client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).setFollowRedirects(true).build().target(getRequestUrl().toURI());
try (Client client = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).setFollowRedirects(true).build()) {
WebTarget rootTarget = client.target(getRequestUrl().toURI());

GenericType<List<ToDo>> todosListType = new GenericType<>() {
};
List<ToDo> allTodos = rootTarget.request().get(todosListType);
int initialSize = allTodos.size();

ToDo toDo = new ToDo();
toDo.setTitle("My First ToDo");
toDo.setOrder(1);
ToDo persistedTodo = rootTarget.request().post(Entity.entity(toDo, MediaType.APPLICATION_JSON_TYPE), ToDo.class);
Long id = persistedTodo.getId();
assertNotNull(id);

GenericType<List<ToDo>> todosListType = new GenericType<List<ToDo>>() {
};
List<ToDo> allTodos = client.request().get(todosListType);
int initialSize = allTodos.size();
allTodos = rootTarget.request().get(todosListType);
assertEquals(initialSize + 1, allTodos.size());
ToDo fetchedToDo = allTodos.get(initialSize);
assertEquals(toDo.getTitle(), fetchedToDo.getTitle());

ToDo toDo = new ToDo();
toDo.setTitle("My First ToDo");
toDo.setOrder(1);
ToDo persistedTodo = client.request().post(Entity.entity(toDo, MediaType.APPLICATION_JSON_TYPE), ToDo.class);
assertNotNull(persistedTodo.getId());
try (Client idClient = ((ResteasyClientBuilder) ClientBuilder.newBuilder()).setFollowRedirects(true).build()) {
WebTarget idTarget = idClient.target(getRequestUrl(id).toURI());
toDo.setTitle(null); // to demonstrate that null fields in the update ToDo result in the existing value being kept
toDo.setCompleted(true);
try (Response response = idTarget.request().build(HttpMethod.PATCH, Entity.entity(toDo, MediaType.APPLICATION_JSON_TYPE)).invoke()) {
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
ToDo updated = response.readEntity(ToDo.class);
assertEquals(1, updated.getOrder());
assertEquals("My First ToDo", updated.getTitle());
assertTrue(updated.isCompleted());
}
persistedTodo = idTarget.request().get(ToDo.class);
assertEquals(1, persistedTodo.getOrder());
assertEquals("My First ToDo", persistedTodo.getTitle());
assertTrue(persistedTodo.isCompleted());

allTodos = client.request().get(todosListType);
assertEquals(initialSize+1, allTodos.size());
ToDo fetchedToDo = allTodos.get(initialSize);
assertEquals(toDo.getTitle(), fetchedToDo.getTitle());
// Delete a single ToDo, the one with the id we added
try (Response response = idTarget.request().delete()) {
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
}

client.request().delete();
allTodos = rootTarget.request().get(todosListType);
assertEquals(initialSize, allTodos.size());

allTodos = client.request().get(todosListType);
assertEquals(0, allTodos.size());
toDo = new ToDo();
toDo.setTitle("My Second ToDo");
toDo.setOrder(2);
persistedTodo = rootTarget.request().post(Entity.entity(toDo, MediaType.APPLICATION_JSON_TYPE), ToDo.class);
id = persistedTodo.getId();
assertNotNull(id);

allTodos = rootTarget.request().get(todosListType);
assertEquals(initialSize + 1, allTodos.size());
fetchedToDo = allTodos.get(initialSize);
assertEquals(toDo.getTitle(), fetchedToDo.getTitle());

// Delete all ToDos
try (Response response = rootTarget.request().delete()) {
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatus());
}

allTodos = rootTarget.request().get(todosListType);
assertEquals(0, allTodos.size());
}
}
}
3 changes: 3 additions & 0 deletions todo-jakarta-data/.ci/after-test-quickstart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# todo-jakarta-data/.ci/after-test-quickstart.sh
docker stop todo-jakarta-data-db
3 changes: 3 additions & 0 deletions todo-jakarta-data/.ci/before-test-quickstart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# todo-jakarta-data/.ci/before-test-quickstart.sh
docker run --rm -d --name todo-jakarta-data-db -e POSTGRES_USER=todos -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgres
2 changes: 2 additions & 0 deletions todo-jakarta-data/.ci/test-quickstart.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
QS_LINUX_ONLY=true
QS_EXTRA_RUN_ARGS="-DPOSTGRESQL_DATABASE=todos -DPOSTGRESQL_DATASOURCE=ToDos -DPOSTGRESQL_SERVICE_HOST=localhost -DPOSTGRESQL_SERVICE_PORT=5432 -DPOSTGRESQL_USER=todos -DPOSTGRESQL_PASSWORD=mysecretpassword"
Loading
Loading