Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover todo app with tests #2784

Merged
merged 7 commits into from
Oct 7, 2021
Merged
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
5 changes: 5 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,11 @@
<artifactId>logback-classic</artifactId>
<version>${version.lib.logback}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${version.lib.logback}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
Expand Down
43 changes: 43 additions & 0 deletions examples/todo-app/backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<properties>
<mainClass>io.helidon.demo.todos.backend.Main</mainClass>
<version.lib.cassandra>3.10.2</version.lib.cassandra>
<version.cassandra.unit>4.3.1.0</version.cassandra.unit>
<version.datastax.driver.core>4.9.0</version.datastax.driver.core>
<version.datastax.driver.query.builder>4.9.0</version.datastax.driver.query.builder>
<version.codahale.metrics.core>3.0.2</version.codahale.metrics.core>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -85,6 +89,45 @@
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.tests</groupId>
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-mp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit</artifactId>
<version>${version.cassandra.unit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
<version>${version.datastax.driver.core}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-query-builder</artifactId>
<version>${version.datastax.driver.query.builder}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${version.codahale.metrics.core}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2021 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.demo.todos.backend;

import java.io.IOException;
import java.util.Base64;
import java.util.Properties;

import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import io.helidon.common.http.Http;
import io.helidon.config.mp.MpConfigSources;
import io.helidon.config.yaml.mp.YamlMpConfigSource;
import io.helidon.microprofile.tests.junit5.Configuration;
import io.helidon.microprofile.tests.junit5.HelidonTest;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@HelidonTest
@Configuration(useExisting = true)
class BackendTests {

private final static String CASSANDRA_HOST = "127.0.0.1";

@Inject
private WebTarget webTarget;

@BeforeAll
static void init() throws IOException {
Properties cassandraProperties = initCassandra();

ClassLoader cl = Thread.currentThread().getContextClassLoader();
ConfigProviderResolver configResolver = ConfigProviderResolver.instance();

org.eclipse.microprofile.config.Config mpConfig = configResolver.getBuilder()
.withSources(YamlMpConfigSource.create(cl.getResource("test-application.yaml")),
MpConfigSources.create(cassandraProperties))
.build();

configResolver.registerConfig(mpConfig, null);
}

@AfterAll
static void stopServer() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}

private static Properties initCassandra() throws IOException {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE,
20000L);
Properties prop = new Properties();
prop.put("cassandra.port", String.valueOf(EmbeddedCassandraServerHelper.getNativeTransportPort()));
prop.put("cassandra.servers.host.host", CASSANDRA_HOST);

Cluster cluster = Cluster.builder()
.withoutMetrics()
.addContactPoint(CASSANDRA_HOST)
.withPort(EmbeddedCassandraServerHelper.getNativeTransportPort())
.build();

Session session = cluster.connect();
session.execute("CREATE KEYSPACE backend WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};");
session.execute(
"CREATE TABLE backend.backend (id ascii, user ascii, message ascii, completed Boolean, created timestamp, "
+ "PRIMARY KEY (id));");
session.execute("select * from backend.backend;");

session.close();
cluster.close();

return prop;
}

@Test
void testTodoScenario() {
String basicAuth = "Basic " + Base64.getEncoder().encodeToString("john:password".getBytes());
JsonObject todo = Json.createObjectBuilder()
.add("title", "todo title")
.build();

// Add a new todo
JsonObject returnedTodo = webTarget
.path("/api/backend")
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.post(Entity.json(todo), JsonObject.class);

assertEquals("john", returnedTodo.getString("user"));
assertEquals(todo.getString("title"), returnedTodo.getString("title"));

// Get the todo created earlier
JsonObject fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id"))
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.get(JsonObject.class);

assertEquals(returnedTodo, fromServer);

// Update the todo created earlier
JsonObject updatedTodo = Json.createObjectBuilder()
.add("title", "updated title")
.add("completed", false)
.build();

fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id"))
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.put(Entity.json(updatedTodo), JsonObject.class);

assertEquals(updatedTodo.getString("title"), fromServer.getString("title"));

// Delete the todo created earlier
fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id"))
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.delete(JsonObject.class);

assertEquals(returnedTodo.getString("id"), fromServer.getString("id"));

// Get list of todos
JsonArray jsonValues = webTarget.path("/api/backend")
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.get(JsonArray.class);

assertEquals(0, jsonValues.size(), "There should be no todos on server");
}

}
40 changes: 40 additions & 0 deletions examples/todo-app/backend/src/test/resources/test-application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) 2021 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# increase importance
config_ordinal: 500

# we use custom config and Helidon JUnit integration, must allow initializer
mp:
initializer:
allow: true
no-warn: true

server:
port: 0
host: localhost

tracing:
service: "todo:back"
enabled: false

security:
providers:
- http-basic-auth:
realm: "helidon"
users:
- login: "john"
password: "password"
15 changes: 15 additions & 0 deletions examples/todo-app/frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-jersey</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading