When I run my test project, the reports are genereted in directory target/jbehave/view as expected.
My problem is that the stylesheets are not found... In the directory target/jbehave/view/style I have a css jbehave.css but the reports generated look for jbehave-core.css
I don't know if I have some versions problem or something else...
Here are the dependencies I use in my pom.xml:
<dependencies>
...
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>${jbehave.version}</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-spring</artifactId>
<version>${jbehave.version}</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-gherkin</artifactId>
<version>${jbehave.version}</version>
</dependency>
<dependency>
<groupId>org.jbehave.site</groupId>
<artifactId>jbehave-site-resources</artifactId>
<version>3.3</version>
<type>zip</type>
</dependency>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.version}</version>
<executions>
<execution>
<id>unpack-view-resources</id>
<phase>process-resources</phase>
<goals>
<goal>unpack-view-resources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
with ${jbehave.version} = 4.1
For the JBehave configuration I use this class:
public class TestRunner extends JUnitStories {
#Autowired
private ApplicationContext applicationContext;
public TestRunner() {
initJBehaveConfiguration();
}
private void initJBehaveConfiguration() {
Class<?> thisClass = this.getClass();
Properties properties = new Properties();
properties.setProperty("encoding", "UTF-8");
useConfiguration(new MostUsefulConfiguration()
.useStoryLoader(new LoadFromClasspath(thisClass.getClassLoader()))
.usePendingStepStrategy(new FailingUponPendingStep())
.useStepdocReporter(new PrintStreamStepdocReporter())
.useStoryReporterBuilder(new StoryReporterBuilder()
.withCodeLocation(CodeLocations.codeLocationFromClass(thisClass))
.withDefaultFormats()
.withFormats(Format.CONSOLE, Format.TXT, Format.HTML, Format.XML, Format.STATS)
.withCrossReference(new CrossReference())
.withViewResources(properties)
.withFailureTrace(true))
.useParameterConverters(new ParameterConverters()
.addConverters(new ParameterConverters.DateConverter(new SimpleDateFormat("yyyy-MM-dd"))))
.useStoryParser(new GherkinStoryParser())
.useStepMonitor(new SilentStepMonitor()));
}
#Override
public InjectableStepsFactory stepsFactory() {
return new SpringStepsFactory(configuration(), applicationContext);
}
protected List<String> storyPaths() {
return new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()), "**/*.story", "**/excluded*.story");
}
}
Finally I made it work.
The probleme came from the pom.
Here is one working for me:
<dependencies>
...
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>${jbehave.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-spring</artifactId>
<version>${jbehave.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-gherkin</artifactId>
<version>${jbehave.version}</version>
<scope>test</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
...
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.version}</version>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/*Scenarios.java</include>
</includes>
<ignoreFailureInStories>true</ignoreFailureInStories>
<ignoreFailureInView>false</ignoreFailureInView>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-jbehave-site-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<overwriteReleases>false</overwriteReleases>
<overwriteSnapshots>true</overwriteSnapshots>
<artifactItems>
<artifactItem>
<groupId>org.jbehave.site</groupId>
<artifactId>jbehave-site-resources</artifactId>
<version>3.3</version>
<type>zip</type>
<outputDirectory>${project.build.directory}/jbehave/view</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
<execution>
<id>unpack-jbehave-reports-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<overwriteReleases>false</overwriteReleases>
<overwriteSnapshots>true</overwriteSnapshots>
<artifactItems>
<artifactItem>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>${jbehave.version}</version>
<outputDirectory>${project.build.directory}/jbehave/view</outputDirectory>
<includes>**\/*.css,**\/*.ftl,**\/*.js</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Related
im struggling implementing gRPC communication.
There is one maven project with two modules which should communicate using gRPC.
The classes generated from *.proto-files contains SessionServiceInterface, which is in fact a MutinyService-interface. (partial implementation below)
Running both projects results in the following Error:
Caused by: javax.enterprise.inject.spi.DeploymentException: de.ilem0n.SessionServiceInterface cannot be injected into de.ilem0n.sessions.FlinkSessionReconciler#sessionService - only Mutiny service interfaces, blocking stubs, reactive stubs based on Mutiny and io.grpc.Channel can be injected via #GrpcClient
The Error itself says that 'only Mutiny service interfaces [...] can be injected via #GrpcClient'
And now im confused. Am I doing anything basically wrong here or is this a strange behaviour?
Hopefully you have some ideas ;)
The interface
session.proto:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "de.ilem0n";
option java_outer_classname = "SessionProto";
package session;
service SessionServiceInterface {
rpc getSessionInfo(SessionRequest) returns (SessionInfoReply) {}
rpc createSession(SessionRequest) returns (SessionInfoReply) {}
}
message SessionRequest {
string sessionId = 1;
}
message SessionInfoReply {
bool isHealthy = 1;
string errorMessage = 2;
string sessionId = 3;
string dnsName = 4;
string ipAddress = 5;
}
SessionService.java:
#GrpcService
public class SessionService implements SessionServiceInterface {
#Inject KubernetesClient k8s;
#Inject SessionBootstrapper sessionBootstrapper;
#Override
public Uni<SessionInfoReply> getSessionInfo(SessionRequest request) {
/* ... boring implementation detail ..*/
}
#Override
public Uni<SessionInfoReply> createSession(SessionRequest request) {
/* ... boring implementation detail ..*/
}
Server-'application.properties':
quarkus.http.port=8090
flink.base-image=flink:1.14.2-scala_2.11-java11
operations.namespace=ngbm-gitops
operations.flink.config-map=flink-config
operations.flink.templates-config-map=flink-templates
operations.service-account.name=ngbm-flink-operator-sa
operations.clusterrole-binding.name=ngbm-flink-operator-crb
operations.k8s.default-timeout.value=1
operations.k8s.default-timeout.unit=MINUTES
session.bootstrap.override-namespace=true
session.bootstrap.labels.controlled-by=flink-operator
SessionClient.java:
public class FlinkSessionReconciler implements Reconciler<FlinkSession> {
#GrpcClient
SessionServiceInterface sessionService;
/* ... boring implementation detail ..*/
}
Client-'application.properties':
quarkus.grpc.clients.sessionService.host=localhost
quarkus.http.port=8080
default.timeout.value=10
default.timeout.unit=SECONDS
session.bootstrap.timeout.value=10
session.bootstrap.timeout.unit=MINUTES
And the pom.xml's:
Project:
<?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>de.ilem0n</groupId>
<artifactId>flink-operator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<maven.compiler.release>16</maven.compiler.release>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.6.3.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
</properties>
<modules>
<module>resource-handler</module>
<module>session-sidecar</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-fault-tolerance</artifactId>
</dependency>
</dependencies>
</project>
Client Module:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.ilem0n</groupId>
<artifactId>flink-operator</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>resource-handler</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus-sdk.version>3.0.1</quarkus-sdk.version>
</properties>
<dependencies>
<dependency>
<groupId>de.ilem0n</groupId>
<artifactId>session-sidecar</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.operatorsdk</groupId>
<artifactId>quarkus-operator-sdk</artifactId>
<version>${quarkus-sdk.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Server Project:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>de.ilem0n</groupId>
<artifactId>flink-operator</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>session-sidecar</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.platform.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager
</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
Pre Quarkus 2.8 you need to copy the proto files into the client module. This is not required anymore in 2.8, as you can indicate in which dependency you may have proto files and Quarkus will automatically generate the code from these files.
I'm getting compilation errors now that I am trying to use junit and mockito to unit test my application.
I know my test is not finished, but I can't even get passed the build due to maven not being able to see these class symbols that should be coming from Mockito.
Here is the pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ets</artifactId>
<groupId>com.ets</groupId>
<version>1.2</version>
</parent>
<groupId>com.</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service</name>
<description>Informatica Service API</description>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.1.4.RELEASE</spring.boot.version>
<deployment-artifacts-plugin.version>1.0.0</deployment-artifacts-plugin.version>
<aws.sdk.version>1.11.313</aws.sdk.version>
<junit-platform.version>5.3.1</junit-platform.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.53</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0.pr1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.forge</groupId>
<artifactId>deployment-artifacts-plugin</artifactId>
<version>${deployment-artifacts-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>attach</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
Here is the test
class
import org.mockito.InjectMocks;
import org.mockito.Mock;
import com.service.api.LinuxController;
import com.service.services.LinuxCommandService;
public class LinuxControllerTest {
#InjectMocks
LinuxController controller;
#Mock
LinuxCommandService linuxCommandService;
#Mock
View mockView;
MockMvc mockMvc;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setSingleView(mockView)
.build();
}
#Test
public void testRunCommand() throws Exception {
mockMvc.perform(post("/api/linux/command"))
.andExpect(status().isOk());
// .andExpect(view().name("redirect:/customer/list"));
}}
Here is the error.
BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 56.753 s
[INFO] Finished at: 2019-08-19T10:02:20-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project service: Compilation failure: Compilation failure:
[ERROR] /C:/Users/guy/Desktop/git_repos/local-svc/src/test/java/LinuxControllerTest.java:[16,5] cannot find symbol
[ERROR] symbol: class View
[ERROR] location: class LinuxControllerTest
[ERROR] /C:/Users/guy/Desktop/git_repos/local-svc/src/test/java/LinuxControllerTest.java:[19,5] cannot find symbol
[ERROR] symbol: class MockMvc
[ERROR] location: class LinuxControllerTest
[ERROR] /C:/Users/guy/Desktop/git_repos/local-svc/src/test/java/LinuxControllerTest.java:[21,6] cannot find symbol
[ERROR] symbol: class Before
[ERROR] location: class LinuxControllerTest
[ERROR] /C:/Users/guy/Desktop/git_repos/local-svc/src/test/java/LinuxControllerTest.java:[30,6] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class LinuxControllerTest
[ERROR] -> [Help 1]
[ERROR]
can you please add all the missing import statement.
All four symbols mentioned in the error are not imported in your LinuxControllerTest.java file. You need to add the imports:
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.View;
You must inject dependency.
#Autowired
MockMvc mockMvc;
I'm trying to capture filed changes using Aspect auditing posibilities and custom anotations, but can't get the old value.
Here is my code:
public aspect FieldAuditAspect {
#Autowired
ActivityService activityService;
#Autowired
UserService userService;
pointcut auditField(Object t, Object value): set(#ge.shemo.model.Client.AopAudit * *) && args(value) && target(t);
before (Object target, Object newValue): auditField(target, newValue) {
FieldSignature sig = (FieldSignature) thisJoinPoint.getSignature();
Field field = sig.getField();
field.setAccessible(true);
Object oldValue;
try {
oldValue = field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException("Failed to create audit Action", e);
}
System.out.println("changed from " + oldValue + " to " + newValue);
}
}
This is my custom annotation that I use for mapping desired filed to capture changes:
#Retention(RUNTIME)
#Target(value = { TYPE, FIELD})
public #interface AopAudit {
}
Here is my pom.xml
<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>ge.shemo</groupId>
<artifactId>SHEMOProject</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<junit.version>4.11</junit.version>
<slf4j.version>1.7.5</slf4j.version>
<logback.version>1.0.13</logback.version>
<spring.version>4.1.9.RELEASE</spring.version>
<aspectj.version>1.8.2</aspectj.version>
<java.source>1.8</java.source>
<java.target>1.8</java.target>
<spring-security.version>3.2.0.RELEASE</spring-security.version>
<hibernate.version>5.0.1.Final</hibernate.version>
<jackson-json.version>2.3.1</jackson-json.version>
<commons-dbcp.version>1.2.2</commons-dbcp.version>
<commons-lang3.version>3.1</commons-lang3.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.source}</source>
<target>${java.target}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.source}</source>
<target>${java.target}</target>
<complianceLevel>${java.target}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>false</verbose>
<XnoInline>false</XnoInline>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<id>aspectj-weave</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>aspectj-weave-test</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!-- Spring Data JPA dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- SpringSecurity dependencies -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring-security.version}</version>
</dependency>
<!-- Testing dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- DB dependencies -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-json.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<!-- Web dependencies -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.1.Final</version>
</dependency>
<!-- optional -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sparkpost/sparkpost-lib -->
<dependency>
<groupId>com.sparkpost</groupId>
<artifactId>sparkpost-lib</artifactId>
<version>0.16.1</version>
</dependency>
<!-- scope provided because the processor is only needed for the compiler -->
<dependency>
<groupId>fr.xebia.extras</groupId>
<artifactId>selma-processor</artifactId>
<version>0.15</version>
<scope>provided</scope>
</dependency>
<!-- This is the only real dependency you will have in your binaries -->
<dependency>
<groupId>fr.xebia.extras</groupId>
<artifactId>selma</artifactId>
<version>0.15</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
This is my entity class:
#Entity
#EntityListeners(AuditingEntityListener.class)
public class Client {
#AopAudit
private String firstName;
#AopAudit
private String lastName;
#Enumerated(EnumType.STRING)
private Gender gender;
.....
This is my function where I update entity:
public Client saveClient(ClientDTO clientDTO) {
DozerBeanMapper mapper = new DozerBeanMapper();
Client client = getClient(clientDTO.getid); //Getting client from DB
client = mapper.map(clientDTO, Client.class); //casting DTO to Entity using DozerBeanMapper
saveClient(client);
Everything works fine except I don't get the old value - old value is always null. I need to get the old value to save it but don't understand why I'm not getting old value.
Can someone tell me what I'm doing wrong?
I'm using Spring MVC + JPA
Let me try to explain how this supposed to work. Your FieldAuditAspect triggered before set operations for fields having #AopAudit annotation, and target(t) in pointcut definition means the object instance that currently affected by aspect.
In your case you do the following:
client = mapper.map(clientDTO, Client.class);
This means that mapper create new instance of Client, which has empty field values, and then populate it with corresponding clientDTO field values. So this where are the null old values taken from.
To work it as you expect you should do the same but with previously fetched Client object, like follows:
client = mapper.map(clientDTO, client);
client object used here instead of Client.class
If you do so then you should see old field values, which is present in fetched client object, in combination with new one, which is present in clientDTO.
Quick reference for those looking to configure Spring MVC, Spring Data, and QueryDSL using XML config.
In pom.xml:
<properties>
<querydsl.version>3.6.7</querydsl.version>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In mvc-dispatcher-servlet.xml:
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="conversionService"/>
<mvc:annotation-driven conversion-service="conversionService">
<mvc:argument-resolvers>
<bean class="org.springframework.data.web.querydsl.QuerydslPredicateArgumentResolver">
<constructor-arg>
<bean class="org.springframework.data.querydsl.binding.QuerydslBindingsFactory">
<constructor-arg>
<value type="org.springframework.data.querydsl.SimpleEntityPathResolver">INSTANCE</value>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg ref="conversionService"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
In FooRepository.java:
public interface FooRepository extends JpaRepository<Foo, Long>, QueryDslPredicateExecutor<Foo> {
}
In FooController.java:
#RequestMapping("/foo")
String getPage(
#QuerydslPredicate(root = Foo.class) Predicate predicate,
Pageable pageable,
Model model
) {
Page<Foo> page = fooRepository.findAll(predicate, pageable);
model.addAttribute("page", page);
return "foo/index";
}
Then you can query your repository like this:
GET /foo?bar=baz
Assuming bar is a property of foo
I have a spring rest app.It is configured with annotations.
When I add spring-data-jpa dependency the application is falling with error on Tomcat 7(jdk 1.7):
Jun 22, 2015 5:05:18 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /var/lib/tomcat7/webapps/springRest-1.1.war
Jun 22, 2015 5:05:19 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/springRest-1.1] startup failed due to previous errors
I add logging.properties file to WEB-INF/classes:
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
But I didn't catch any additional info into log.
My source code:
MyWebApplicationInitializer.java:
import upc.ua.springrest.config.WebConfiguration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return null;
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfiguration.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
WebConfiguration.java:
package upc.ua.springrest.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "upc.ua.springrest")
public class WebConfiguration extends WebMvcConfigurerAdapter {
}
HelloWorldController.java:
package upc.ua.springrest.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloWorldController {
#RequestMapping("/helloWorld")
public String helloWorld() {
return "dataSource.toString()";
}
#RequestMapping("/")
public String root() {
return "Hello, Alex!";
}
}
pom.xml:
<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>upc.ua</groupId>
<artifactId>springRest</artifactId>
<version>1.1</version>
<packaging>war</packaging>
<name>springRest</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.6.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.6.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.2.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.5</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I changed version of jdk in pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
Now. It is work fine.