Skip to content

Commit

Permalink
integration test updated for /start
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruilin Ma authored and Ruilin Ma committed Oct 30, 2023
1 parent c133d2b commit 855857f
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 213 deletions.
42 changes: 32 additions & 10 deletions start/inventory/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,45 @@
<version>3.1.2</version>
</dependency>
<!-- end::rxjava[] -->
<!-- For tests -->
<dependency>
<groupId>org.microshed</groupId>
<artifactId>microshed-testing-liberty</artifactId>
<version>0.9.1</version>
<scope>test</scope>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<!-- For tests -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.16.2</version>
<version>1.19.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.0.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-json-binding-provider</artifactId>
<version>6.0.0.Final</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -104,21 +126,21 @@
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>3.8.2</version>
<version>3.9</version>
</plugin>

<!-- Plugin to run unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<version>3.1.2</version>
</plugin>

<!-- Plugin to run integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<version>3.1.2</version>
<executions>
<execution>
<goals>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// tag::copyright[]
/*******************************************************************************
* Copyright (c) 2020, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
// end::copyright[]
package it.io.openliberty.guides.inventory;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

import java.util.List;
import java.util.Properties;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/inventory")
public interface InventoryResourceCleint {
@GET
@Path("/systems")
@Produces(MediaType.APPLICATION_JSON)
List<String> getSystems();

@GET
@Path("/systems/{hostname}")
@Produces(MediaType.APPLICATION_JSON)
Properties getSystem(
@PathParam("hostname") String hostname);
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tag::copyright[]
/*******************************************************************************
* Copyright (c) 2020, 2021 IBM Corporation and others.
* Copyright (c) 2020, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -11,54 +11,130 @@
// end::copyright[]
package it.io.openliberty.guides.inventory;

import java.util.List;
import java.time.Duration;

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

import java.math.BigDecimal;
import java.util.List;
import java.nio.file.Paths;
import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import jakarta.ws.rs.core.UriBuilder;
import jakarta.ws.rs.client.ClientBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.microshed.testing.SharedContainerConfig;
import org.microshed.testing.jaxrs.RESTClient;
import org.microshed.testing.jupiter.MicroShedTest;
import org.microshed.testing.kafka.KafkaProducerClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.Network;
import org.testcontainers.utility.DockerImageName;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.images.builder.ImageFromDockerfile;
import org.apache.kafka.common.serialization.StringSerializer;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.output.Slf4jLogConsumer;

import io.openliberty.guides.inventory.InventoryResource;
import io.openliberty.guides.models.SystemLoad;
import io.openliberty.guides.models.SystemLoad.SystemLoadSerializer;

@MicroShedTest
@SharedContainerConfig(AppContainerConfig.class)
@TestMethodOrder(OrderAnnotation.class)
@Testcontainers
public class InventoryServiceIT {

@RESTClient
public static InventoryResource inventoryResource;
private static Logger logger = LoggerFactory.getLogger(InventoryServiceIT.class);

public static InventoryResourceCleint client;

private static Network network = Network.newNetwork();

@KafkaProducerClient(valueSerializer = SystemLoadSerializer.class)
public static KafkaProducer<String, SystemLoad> producer;

private static ImageFromDockerfile inventoryImage
= new ImageFromDockerfile("inventory:1.0-SNAPSHOT")
.withDockerfile(Paths.get("./Dockerfile"));

private static KafkaContainer kafkaContainer = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:latest"))
.withListener(() -> "kafka:19092")
.withNetwork(network);

private static GenericContainer<?> inventoryContainer =
new GenericContainer(inventoryImage)
.withNetwork(network)
.withExposedPorts(9085)
.waitingFor(Wait.forHttp("/health/ready").forPort(9085))
.withStartupTimeout(Duration.ofMinutes(2))
.withLogConsumer(new Slf4jLogConsumer(logger))
.dependsOn(kafkaContainer);

private static InventoryResourceCleint createRestClient(String urlPath) {
ClientBuilder builder = ResteasyClientBuilder.newBuilder();
ResteasyClient client = (ResteasyClient) builder.build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath(urlPath));
return target.proxy(InventoryResourceCleint.class);
}

@BeforeAll
public static void startContainers() {
kafkaContainer.start();
inventoryContainer.withEnv(
"mp.messaging.connector.liberty-kafka.bootstrap.servers", "kafka:19092");
inventoryContainer.start();
client = createRestClient("http://"
+ inventoryContainer.getHost()
+ ":" + inventoryContainer.getFirstMappedPort());
}

@BeforeEach
public void setUp() {
Properties producerProps = new Properties();
producerProps.put(
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
kafkaContainer.getBootstrapServers());
producerProps.put(
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());
producerProps.put(
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
SystemLoadSerializer.class.getName());

producer = new KafkaProducer<String, SystemLoad>(producerProps);
}

@AfterAll
public static void cleanup() {
inventoryResource.resetSystems();
public static void stopContainers() {
inventoryContainer.stop();
kafkaContainer.stop();
network.close();
}

@AfterEach
public void tearDown() {
producer.close();
}

@Test
public void testCpuUsage() throws InterruptedException {
SystemLoad sl = new SystemLoad("localhost", 1.1);
producer.send(new ProducerRecord<String, SystemLoad>("system.load", sl));
Thread.sleep(5000);
List<String> response = inventoryResource.getSystems();
List<String> response = client.getSystems();
assertNotNull(response);
assertEquals(response.size(), 1);
for (String system : response) {
Properties sp = inventoryResource.getSystem(system);
Properties sp = client.getSystem(system);
assertEquals(sl.hostname, sp.get("hostname"),
"Hostname doesn't match!");
BigDecimal systemLoad = (BigDecimal) sp.get("systemLoad");
Expand Down
5 changes: 5 additions & 0 deletions start/models/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>

<properties>
Expand Down
Loading

0 comments on commit 855857f

Please sign in to comment.