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

Source code for all chapters #20

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
115 changes: 115 additions & 0 deletions code/chapter02/mp-ecomm-store/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.microprofile.tutorial</groupId>
<artifactId>mp-ecomm-store</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>mp-ecomm-store</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<!-- Setting the source and target of the Java Compiler -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

<!-- Liberty configuration -->
<liberty.var.default.http.port>9080</liberty.var.default.http.port>
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
<liberty.var.app.context.root>mp-ecomm-store</liberty.var.app.context.root>
</properties>

<dependencies>

<!-- Add Lombok dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>

<!-- Adding Jakarta EE dependencies -->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>

<!-- Adding MicroProfie dependency -->
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>6.1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>

<!-- JUnit Jupiter API for writing tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<!-- JUnit Jupiter Engine for running tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>

<!-- Enable liberty-maven plugin -->
<plugin>
<groupId>io.openliberty.tools</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<serverName>mpServer</serverName>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>

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

<!-- Plugin to run functional tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<systemPropertyVariables>
<http.port>${liberty.var.default.http.port}</http.port>
<war.name>${liberty.var.app.context.root}</war.name>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.microprofile.tutorial.store.product;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/api")
public class ProductRestApplication extends Application{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.microprofile.tutorial.store.product.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Product {
private Long id;
private String name;
private String description;
private Double price;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.microprofile.tutorial.store.product.resource;

import java.util.ArrayList;
import java.util.List;

import io.microprofile.tutorial.store.product.entity.Product;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/products")
@ApplicationScoped
public class ProductResource {
private List<Product> products;

public ProductResource() {
products = new ArrayList<>();

products.add(new Product(Long.valueOf(1L), "iPhone", "Apple iPhone 15", Double.valueOf(999.99)));
products.add(new Product(Long.valueOf(2L), "MacBook", "Apple MacBook Air", Double.valueOf(1299.99)));
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Product> getProducts() {
// Return a list of products
return products;
}
}

10 changes: 10 additions & 0 deletions code/chapter02/mp-ecomm-store/src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<server description="MicroProfile Tutorial Liberty Server">
<featureManager>
<feature>restfulWS-3.1</feature>
<feature>jsonb-3.0</feature>
</featureManager>

<httpEndpoint httpPort="${default.http.port}" httpsPort="${default.https.port}"
id="defaultHttpEndpoint" host="*" />
<webApplication location="mp-ecomm-store.war" contextRoot="${app.context.root}"/>
</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.microprofile.tutorial;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.microprofile.tutorial.store.product.resource;

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

import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.microprofile.tutorial.store.product.entity.Product;

public class ProductResourceTest {
private ProductResource productResource;

@BeforeEach
void setUp() {
productResource = new ProductResource();
}

@AfterEach
void tearDown() {
productResource = null;
}

@Test
void testGetProducts() {
List<Product> products = productResource.getProducts();

assertNotNull(products);
assertEquals(2, products.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.microprofile.tutorial.store.logging;

import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.METHOD;

import jakarta.interceptor.InterceptorBinding;
import java.lang.annotation.Target;

@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD})
public @interface Loggable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.microprofile.tutorial.store.logging;

import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

import java.util.logging.Logger;

@Interceptor // Declare as an interceptor
public class LoggingInterceptor {

private static final Logger LOGGER = Logger.getLogger(LoggingInterceptor.class.getName());

@AroundInvoke // Method to execute around the intercepted method
public Object logMethodInvocation(InvocationContext ctx) throws Exception {
LOGGER.info( "Entering method: " + ctx.getMethod().getName());

Object result = ctx.proceed(); // Proceed to the original method

LOGGER.info("Exiting method: " + ctx.getMethod().getName());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.microprofile.tutorial.store.product;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/api")
public class ProductRestApplication extends Application{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.microprofile.tutorial.store.product.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "Product")
@NamedQuery(name = "Product.findAllProducts", query = "SELECT p FROM Product p")
@NamedQuery(name = "Product.findProductById", query = "SELECT p FROM Product p WHERE p.id = :id")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {

@Id
@GeneratedValue
private Long id;

@NotNull
private String name;

@NotNull
private String description;

@NotNull
private Double price;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.microprofile.tutorial.store.product.repository;

import java.util.List;

import io.microprofile.tutorial.store.product.entity.Product;
import jakarta.enterprise.context.RequestScoped;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

@RequestScoped
public class ProductRepository {

// tag::PersistenceContext[]
@PersistenceContext(unitName = "product-unit")
// end::PersistenceContext[]
private EntityManager em;

// tag::createProduct[]
public void createProduct(Product product) {
em.persist(product);
}

public Product updateProduct(Product product) {
return em.merge(product);
}

public void deleteProduct(Product product) {
em.remove(product);
}

// tag::findAllProducts[]
public List<Product> findAllProducts() {
return em.createNamedQuery("Product.findAllProducts",
Product.class).getResultList();
}

public Product findProductById(Long id) {
return em.find(Product.class, id);
}

public List<Product> findProduct(String name, String description, Double price) {
return em.createNamedQuery("Event.findProduct", Product.class)
.setParameter("name", name)
.setParameter("description", description)
.setParameter("price", price).getResultList();
}

}
Loading