I am setting up a project for spasuk in parallel. For more flexibility, I decided to use testng. Faced the problem that the tests do not see the web driver. Have the following error
java.lang.IllegalStateException: No webdriver is bound to current thread: 14. You need to call open(url) first.
Below are the pom file one of the test classes so that it would be clear how it is called, the method that initializes the browser and one of the suites (they are the same in structure, they just refer to different packages)
Although browser was opened.
POM.xml file:
<?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>com.app</groupId>
<artifactId>e2e-ui-cashier</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<junit-platform-surefire-provider.version>1.3.1</junit-platform-surefire-provider.version>
<aspectj.version>1.8.10</aspectj.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
<junit.jupiter.engine.version>5.3.1</junit.jupiter.engine.version>
<junit.jupiter.api.version>5.4.2</junit.jupiter.api.version>
<selenide.version>5.2.7</selenide.version>
</properties>
<dependencies>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>${selenide.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-selenide</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.artsok</groupId>
<artifactId>rerunner-jupiter</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.aeonbits.owner</groupId>
<artifactId>owner</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<useSystemClassLoader>false</useSystemClassLoader>
<testFailureIgnore>false</testFailureIgnore>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<systemProperties>
<property>
<name>junit.jupiter.extensions.autodetection.enabled</name>
<value>true</value>
</property>
</systemProperties>
<systemPropertyVariables>
<propertyName>allure-results</propertyName>
<allure.results.directory>target/allure-results</allure.results.directory>
<allure.link.issue.pattern>https://arammeem.atlassian.net/browse/{}</allure.link.issue.pattern>
</systemPropertyVariables>
<properties>
<property>
<name>suitethreadpoolsize</name>
<value>2</value>
</property>
<property>
<name>parallel</name>
<value>suites</value>
</property>
<property>
<name>dataproviderthreadcount</name>
<value>2</value>
</property>
</properties>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suite1.xml</suiteXmlFile>
<suiteXmlFile>src/test/resources/suite2.xml</suiteXmlFile>
<!-- <suiteXmlFile>src/test/resources/suite3.xml</suiteXmlFile>-->
<!-- <suiteXmlFile>src/test/resources/suite4.xml</suiteXmlFile>-->
</suiteXmlFiles>
</configuration>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.junit.platform</groupId>-->
<!-- <artifactId>junit-platform-surefire-provider</artifactId>-->
<!-- <version>${junit-platform-surefire-provider.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Test class example:
// ~ Copyright © 2019 Aram Meem Company Limited. All Rights Reserved.
package pages.thread_1;
import io.qameta.allure.Description;
import io.qameta.allure.Issue;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import steps.*;
import steps.modalPagesSteps.*;
import utils.BaseTest;
import utils.annotations.Bug;
import utils.enums.OrderStatus;
import utils.enums.Settlement;
import static com.codeborne.selenide.Selenide.sleep;
import static core.TestStepLogger.logPreconditionStep;
import static core.TestStepLogger.logStep;
import static steps.HelperSteps.openSecondTab;
import static steps.HelperSteps.resizeBrowserWindow;
import static utils.enums.Locale.AR;
import static utils.enums.Locale.EN;
import static utils.enums.Localization.VALID_NUMBER;
import static utils.enums.NotificationMessages.MERCHANT_ORDER_REFERENCE_ALREADY_IN_USE;
import static utils.enums.PageTitles.*;
public class BookADeliveryOrdersTest extends BaseTest {
#BeforeMethod(alwaysRun=true)
public void goToLoginPage() {
logPreconditionStep(1, "Open 'Login' page");
configure();
logPreconditionStep(2, "Check that 'email' and 'password' placeholders are displayed");
loginPageSteps.checkThatEmailAndPasswordPlaceholdersAreDisplayed();
logPreconditionStep(3, "Enter valid credentials");
loginPageSteps.enterValidCredentials(merUser2);
logPreconditionStep(4, "Press 'OK' on the Warning notification");
warningModalPageSteps.checkThatSoundNotificationWarningIsDisplayedAndClickOk();
logPreconditionStep(5, "Check that 'NEW ORDERS' title appears");
titlePageSteps.pageTitleShouldHaveText(MAIN);
}
#Test
#Description("Orders creation functionality verification")
public void ordersCreationFunctionalityVerification() {
logStep(1, "Switch POS to " + pos1.getName());
headerPageSteps.setAndCheckPOS(pos1);
logStep(2, "Go to 'BOOK A DELIVERY' page");
mainPageSteps.clickBookADeliveryButton();
titlePageSteps.pageTitleShouldHaveText(BOOK_A_DELIVERY);
logStep(3, "Fill all mandatory fields");
bookADeliveryPageSteps.fillAllMandatoryFields();
logStep(4, "Press 'Book order' button");
bookADeliveryPageSteps.clickBtnDeliveryOrder();
logStep(5, "Click 'Confirm order' button");
confirmOrderModalPageStep.clickConfirmOrderBtn();
logStep(6, "Check confirmation message");
notificationModalPageSteps.createOrderMessageShouldBeEnglishDisplayed();
}
}
Configuration:
public void configure() {
System.setProperty("selenide.browser", "chrome");
ChromeDriverManager.chromedriver().version(testProp.chromeDriverVersion()).setup();
SelenideLogger.addListener(testProp.selenideListener(), new AllureSelenide().screenshots(true).savePageSource(false));
System.out.println("DDDDDDDDDDDDDDDDDDDDDDDD");
Configuration.browserSize = testProp.browserSize();
Configuration.timeout = testProp.timeout();
Configuration.headless = testProp.headless();
clearBrowserCache();
BASE_LOCALE = Locale.EN;
Selenide.open(testCred.BASE_URL());
}
One of the suites:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<!-- verbose="1" parallel="false"-->
<suite name="Suite1" parallel="tests" verbose="2">
<test name="Regression1" >
<packages>
<package name="pages.thread_1"></package>
</packages>
</test>
</suite>
Probably the problem was already fixed in latest Selenide version.
If not, please provide the full stacktrace of your IllegalStateException.
Related
I am getting nullpointerException while running my cucumber tests by mvn clean test.
Scenario: Run atlas # src/test/java/Atlas/Features/Atlas.feature:3
Given Go to atlas and check title # Atlas.PageObjects.AtlasPage.openAtlas()
java.lang.NullPointerException
at Atlas.PageObjects.AtlasPage.openAtlas(AtlasPage.java:12)
at âś˝.Go to atlas and check title(file:///C:/atlas%20qa/src/test/java/Atlas/Features/Atlas.feature:4)
I created WebDriverConfig.java file and I am using pico container to share this class.
Code is working until classes are in the same package but I want to separate them.
Please ask me for more information if it's needed.
package Atlas;
import io.cucumber.java.Before;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.Arrays;
import java.util.List;
public class WebDriverConfig {
public WebDriver driver;
#Before
public void setup(){
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
List<String> browserArguments = Arrays.asList(
"--window-size=1920,1080",
// "--start-maximized",
"--no-sandbox",
"--ignore-certificate-errors",
"--disable-popup-blocking",
//"--incognito",
"--allow-no-sandbox-job",
"--proxy-bypass-list=*");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments(browserArguments);
this.driver = new ChromeDriver(chromeOptions);
}
public WebDriver getDriver(){
return driver;
}
}
package Atlas.PageObjects;
import Atlas.WebDriverConfig;
import io.cucumber.java.en.Given;
import org.openqa.selenium.WebDriver;
public class AtlasPage {
WebDriver driver;
#Given("Go to atlas and check title")
public void openAtlas() {
driver.get("test env bla bla bla");
}
public AtlasPage(WebDriverConfig webDriverConfig) {
this.driver = webDriverConfig.getDriver();
}
}
pom:
<?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>org.example</groupId>
<artifactId>atlas-automation-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>**/common.RunCucumberTest*.java</includes>
<forkCount>2</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<!-- Jar file entry point -->
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.9.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>6.9.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Thanks to Alexey R.
Issue is resolved.
To solve this problem I had to add classes with #Before / #After annotations to glue property in CucumberRuner class:
glue = {"Atlas.PageObjects", "Atlas.common"}
i am trying to print the output written in the step definitions into eclipse console. but i do not see any output in the console
i have added these below dependencies into my project and i have also included "pretty" and monochrome :true in the runner class
<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>sampleProject</groupId>
<artifactId>sampleProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sampleProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>4.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}\lib\tools-1.8.0.jar</systemPath>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.7.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>2.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.6</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>4.7.1</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>gherkin</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit-dep -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.11</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.7.1</version>
</dependency>
</dependencies>
</project>
for example if i print any thing in my step definitions like
System.out.println("2+2");
when i ran this code, only see the below lines in the console
[RemoteTestNG] detected TestNG version 6.14.3
Starting ChromeDriver 76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-refs/branch-heads/3809#{#864}) on port 9315
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Aug 02, 2019 11:54:35 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition # src/main/java/Features/sample.feature:6
Given I open Google # SampleStepDef.I_open_google()
When I enter "2+2" in search textbox # SampleStepDef.I_enter_in_search_textbox(String)
Then I should get result as "4" # SampleStepDef.I_should_get_correct_result(String)
PASSED: runScenario("Addition", "Check addition in Google calculatorcontent")
Runs Cucumber Scenarios
===============================================
the console does not display anything else other than this.
it also does not display the number of scenarios and steps passed or failed like
1 Scenarios ([33m1 undefined[0m)
3 Steps ([33m3 undefined[0m)
0m0.000s
And here is my tesRunner code
package Runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
#CucumberOptions(
features = "src/main/java/Features",
glue = {"StepDefinitions"},
// tags = "#SmokeTest",
monochrome = true,
dryRun = false,
strict = true,
plugin = {
"pretty",
"html:target/cucumber-reports/cucumber-pretty",
"json:target/cucumber-reports/CucumberTestReport.json",
"rerun:target/cucumber-reports/rerun.txt"
}
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
and stepDefifnition code
package StepDefinitions;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class SampleStepDef {
protected WebDriver driver;
private static Logger Log = Logger.getLogger(Logger.class.getName());
#Before
public void setup() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver.exe");
driver = new ChromeDriver();
}
#Given("^I open Google$")
public void I_open_google() {
//Set implicit wait of 10 seconds and launch google
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
#When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String additionTerms) {
//Write term in google textbox
WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
googleTextBox.sendKeys(additionTerms);
//Click on searchButton
WebElement searchButton = driver.findElement(By.id("gbqfb"));
searchButton.click();
}
#Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String expectedResult) {
//Get result from calculator
WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
String result = calculatorTextBox.getText();
//Verify that result of 2+2 is 4
Assert.assertEquals(result, expectedResult);
driver.close();
}
#After
public void closeBrowser() {
driver.quit();
}
}
With cucumber-jvm 4.5.0 and up, in order to see the summary statistics at the end of a run you need to explicit ask for it via the plugins you define in your runner class. If you add the "summary" (as you have already "pretty") you should be able to see again the totals of Scenarios/Steps and time it took.
This has changed via https://github.com/cucumber/cucumber-jvm/pull/1670
Is it opening the google chrome browser?.
I think your code is not reaching to the point where there are print statements, Please share your test runner and step-definition file.
I'm working with Adobe Experience Manager (AEM) 6.4 with Service Pack 1, and the Forms Package.
I have a lot of extended Properties/attributes, so I made a Database diagram. I don't want to save all the additional stuff in crx I want to save it in an Oracle database.
The Database diagram is complex, so I want to USE JPA (Hibernate), at minimum. If Spring would help to make it easier to use, than that would be fine for me.
I read a lot that the OSGI is working with blueprint instead of Spring, but you can combine it.
I'm really searching for an good example how to make things work with JPA and a Oracle db.
Adobe is no Help at all, they can't show an example how to work with AEM and JPA/Hibernate/Spring/blueprint.
Can anybody help me to make things working? with AEM and JPA?
I think what I need is:
persistence.xml (RESOURCE_LOCAL or JTA?)
Entity classes with Annotation #Entity and other JPA annotations
Service classes with Transaction control and maybe manager classes to submit to work with the entity classes and get result from queries, maybe I could put it all in the service classes
hibernate-osgi (dependency)
ojdbc7 for connection (dependency)
org.apache.aries.jpa.api (dependency)
But how do I have to make things happen? Nothing works. I even don't know if this is the right way.
should I use blueprint or Spring or both?
I found this Aries thing from apache.
http://aries.apache.org
And weard different samples that I don't really understand how they work.
https://github.com/apache/aries-jpa/tree/master/examples
And some OSGI sample, they look very incomplete for my situation.
https://enroute.osgi.org/tutorial/032-tutorial_microservice-jpa.html
So is there anybody with some experience with AEM and JPA?
let me describe how it is implemented on our project. We are using AEM 6.3 with SP2.
We have next dependencies in the root pom.xml:
<!-- JPA -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>11.1</version>
<scope>system</scope>
<systemPath>${project.root.path}/lib/db2jcc4.jar</systemPath>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<!-- local development database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>
<!-- /JPA-->
Then we have the bundle to provide JPA dependencies and the OSGI service that allows to get hibernate Session. The bundle pom.xml
<?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>
<parent>
<groupId>com.myproject</groupId>
<artifactId>myproject-bundles</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>com.myproject.db</artifactId>
<packaging>bundle</packaging>
<name>myproject - DB bundle</name>
<description>OSGI bundle to work with a database</description>
<properties>
<bundle.export>
com.myproject.db.*,
javax.persistence,
org.hibernate,
org.hibernate.cfg,
org.hibernate.proxy,
org.hibernate.boot.registry,
org.hibernate.annotations,
org.hibernate.service,
org.hibernate.criterion,
org.hibernate.transform
</bundle.export>
<bundle.import>*;resolution:=optional</bundle.import>
<!-- Import JDBC driver dynamically -->
<bundle.dynamic.import>com.ibm.*,javassist.util.*</bundle.dynamic.import>
<bundle.embed>
hibernate-jpa-2.1-api,hibernate-core,hibernate-entitymanager,hibernate-commons-annotations,jboss-logging,antlr
</bundle.embed>
<project.root.path>${project.basedir}/../..</project.root.path>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>com.myproject.common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
In the bundle we have next service:
package com.myproject.db;
import org.hibernate.Session;
public interface JPASessionFactory {
Session openSession();
void closeSession(Session session);
}
implementation:
package com.myproject.db.impl;
import com.myproject.db.JPASessionFactory;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import javax.sql.DataSource;
import java.util.Map;
#Service
#Component(metatype = true, policy = ConfigurationPolicy.REQUIRE)
#Properties({
#Property(label = "Hibernate SQL dialect", name = Environment.DIALECT),
#Property(label = "Show SQL", name = Environment.SHOW_SQL, boolValue = false),
#Property(label = "Bulk ID Strategy", name = Environment.HQL_BULK_ID_STRATEGY)
})
public class JPASessionFactoryImpl implements JPASessionFactory {
#Reference(target = "(datasource.name=myproject)")
private DataSource dataSource;
private SessionFactory sessionFactory;
#Activate
protected void activate(Map<String, Object> properties) {
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.configure()
.applySetting(Environment.DIALECT, PropertiesUtil.toString(properties.get(Environment.DIALECT), ""))
.applySetting(Environment.SHOW_SQL, PropertiesUtil.toBoolean(properties.get(Environment.SHOW_SQL), false))
.applySetting(Environment.DATASOURCE, dataSource);
String bulkIdStrategy = PropertiesUtil.toString(properties.get(Environment.HQL_BULK_ID_STRATEGY), "");
if (!bulkIdStrategy.isEmpty()) {
builder.applySetting(Environment.HQL_BULK_ID_STRATEGY, bulkIdStrategy);
}
sessionFactory = new Configuration().buildSessionFactory(builder.build());
}
#Deactivate
protected void deactivate() {
if (sessionFactory != null) {
sessionFactory.close();
}
}
#Override
public Session openSession() {
return sessionFactory.openSession();
}
#Override
public void closeSession(Session session) {
if (session != null && session.isOpen()) {
session.close();
}
}
}
osgi config for the service:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
hibernate.dialect="org.hibernate.dialect.H2Dialect"
hibernate.show_sql="{Boolean}true"/>
Configuration for DataSourceFactory apps/myproject-forms/configuration/config.local/org.apache.sling.datasource.DataSourceFactory-localh2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
datasource.name="myproject"
driverClassName="org.h2.Driver"
url="jdbc:h2:./myprojectlocal;AUTO_SERVER=TRUE"
username="sa"
password=""
testOnBorrow="{Boolean}true"
testOnReturn="{Boolean}true"
testWhileIdle="{Boolean}true"
validationQuery="SELECT 1"/>
Also we have the Hibernate config file hibernate.cfg.xml in "resources" folder of the bundle
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.myproject.db.Entity1"/>
<mapping class="com.myproject.db.Entity2"/>
</session-factory>
</hibernate-configuration>
I've found the solution and made a post here on the site.
https://forums.adobe.com/message/10640295#10640295
I feel like I'm the First Guy on earth which combined AEM with JPA/Hibernate. Now I could check if it's a benefit to work with spring, to operate with the Transactions.
And those two who voted the question down, I'm sure you are from adobe --> thanks, now I feel more expert than you on your own cms, because before my post there was no solution for this problem.
Update: Maven Dependencies. If you have problems with the dependencies you have to Embed them and add the property "Embed-Transitive to true"
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<inherited>true</inherited>
<configuration>
<instructions>
<Embed-Dependency>
*;scope=compile|runtime
</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Export-Package>
<!--TODO export packages -->
</Export-Package>
<Private-Package>todo</Private-Package>
<Import-Package>
<!-- Attention this is really sensitive!!. -->
*;resolution:="optional"
</Import-Package>
<Bundle-Activator>path.to.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
There are some blog posts describing how to realise JPA persistence in AEM/CQ. In using-jpa-to-write-database-applications-in-an-osgi-container-e-g-felix-or-adobe-aem-cq5 they describe how to setup a JNDI datasource with a PersistenceUnit and how to #Reference a EntityManagerFactory.
We have integrated Hibernate 5.4.5 and JPA 2.2 into Adobe Experience Manager 6.4.4 using two approaches below. Please, check https://soft-werke.com/en/archives/news-en/how-to-install-hibernate-5-4-5-jpa-2-2-in-adobe-experience-manager-6-4-4/ for more details.
Classic approach - install zip package with all embedded Hibernate dependencies in AEM Package Manager with content-package-maven-plugin. Below is an example of Maven profile which can be later excluded to avoid installing Hibernate in each AEM deployment.
<profiles>
<profile>
<id>installHibernate</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.day.jcr.vault</groupId>
<artifactId>content-package-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<embeddedTarget>/apps/hibernate/install</embeddedTarget>
<embeddeds>
<embedded>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.antlr</artifactId>
</embedded>
<embedded>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
</embedded>
<embedded>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
</embedded>
<embedded>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
</embedded>
<embedded>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</embedded>
<embedded>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</embedded>
<embedded>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</embedded>
<embedded>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.dom4j</artifactId>
</embedded>
<embedded>
<groupId>com.sun.istack</groupId>
<artifactId>istack-commons-runtime</artifactId>
</embedded>
<embedded>
<groupId>com.sun.xml.fastinfoset</groupId>
<artifactId>FastInfoset</artifactId>
</embedded>
<embedded>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jaxb-runtime</artifactId>
</embedded>
<embedded>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</embedded>
<embedded>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</embedded>
<embedded>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
</embedded>
<embedded>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
</embedded>
<embedded>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
</embedded>
<embedded>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.javax-inject</artifactId>
</embedded>
<embedded>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
</embedded>
<embedded>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</embedded>
<embedded>
<groupId>org.jboss.spec.javax.interceptor</groupId>
<artifactId>jboss-interceptors-api_1.2_spec</artifactId>
</embedded>
<embedded>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
</embedded>
<embedded>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</embedded>
<embedded>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</embedded>
<embedded>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
</embedded>
</embeddeds>
</configuration>
<executions>
<execution>
<id>install-package-hibernate</id>
<goals>
<goal>install</goal>
</goals>
<configuration>
<targetURL>http://${aem.host}:${aem.port}/crx/packmgr/service.jsp</targetURL>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<!-- ====================================================================== -->
<!-- D E P E N D E N C I E S -->
<!-- ====================================================================== -->
<dependencies>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.antlr</artifactId>
<version>2.7.7_5</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.dom4j</artifactId>
<version>2.1.1_1</version>
</dependency>
<dependency>
<groupId>com.sun.istack</groupId>
<artifactId>istack-commons-runtime</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.fastinfoset</groupId>
<artifactId>FastInfoset</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jaxb-runtime</artifactId>
<version>2.3.1_1</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.0-GA</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>2.0.5.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.javax-inject</artifactId>
<version>1_2</version>
</dependency>
<dependency>
<groupId>javax.interceptor</groupId>
<artifactId>javax.interceptor-api</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.interceptor</groupId>
<artifactId>jboss-interceptors-api_1.2_spec</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.2_spec</artifactId>
<version>1.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.4.5.Final</version>
</dependency>
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>uber-jar</artifactId>
<classifier>apis</classifier>
</dependency>
<dependency>
<groupId>javax.jcr</groupId>
<artifactId>jcr</artifactId>
</dependency>
</dependencies>
Another approach - install all Hibernate bundles in AEM Felix Console one-by-one in predefined order with sling-maven-plugin called by Java Maven Invoker API. Below is an example of Maven profile.
<profiles>
<profile>
<id>installHibernate</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>rg</groupId>
<artifactId>com.softwerke.jpa.hibernate.bundles</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${basedir}/src/main/content/jcr_root/apps/hibernate/install/
</outputDirectory>
<includeArtifactIds>
org.apache.servicemix.bundles.antlr,
classmate,
javax.activation,
javax.activation-api,
javax.persistence-api,
jaxb-api,
byte-buddy,
org.apache.servicemix.bundles.dom4j,
istack-commons-runtime,
FastInfoset,
org.apache.servicemix.bundles.jaxb-runtime,
javassist,
jboss-logging,
hibernate-commons-annotations,
jandex,
javax.el-api,
org.apache.servicemix.bundles.javax-inject,
javax.interceptor-api,
cdi-api,
jboss-interceptors-api_1.2_spec,
jboss-transaction-api_1.2_spec,
mysql-connector-java,
hibernate-core,
hibernate-osgi
</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.softwerke.build.utils.HibernateInstaller</mainClass>
<classpathScope>compile</classpathScope>
<arguments>
<!-- Path to load Hibernate OSGi dependencies from -->
<argument>${basedir}/src/main/content/jcr_root/apps/hibernate/install</argument>
<!-- Path to XML file with a list of Hibernate bundles -->
<argument>${basedir}/src/main/content/META-INF/resources/hibernate-bundles.xml</argument>
<!-- User login to install bundle in AEM -->
<argument>${sling.user}</argument>
<!-- User password to install bundle in AEM -->
<argument>${sling.password}</argument>
<!-- Path to AEM Felix Console -->
<argument>http://${aem.host}:${aem.port}/system/console</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Java code for Hibernate bundle installation with Maven Invoker API:
public class HibernateInstaller {
public static void main(String[] args) throws Exception {
String bundleFolder,
xmlFilePath,
slingUser,
slingPassword,
url;
if (args != null && args.length == 5) {
bundleFolder = args[0];
xmlFilePath = args[1];
slingUser = args[2];
slingPassword = args[3];
url = args[4];
} else {
throw new Exception("Can't execute Hibernate install!\nPlease specify five arguments.");
}
StringBuilder mvnCmd = new StringBuilder();
mvnCmd.append("org.apache.sling:sling-maven-plugin:2.4.2:install-file");
mvnCmd.append(" -Dsling.user=" + slingUser);
mvnCmd.append(" -Dsling.password=" + slingPassword);
mvnCmd.append(" -Dsling.url=" + url);
mvnCmd.append(" -Dsling.deploy.method=WebConsole");
mvnCmd.append(" -Dsling.file=");
mvnCmd.append(bundleFolder);
mvnCmd.append("/");
// Read XML file with a list of the Hibernate OSGi dependencies (bundles)
File file = new File(xmlFilePath);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
NodeList bundlesList = document.getElementsByTagName("bundle");
final InvocationRequest invocationRequest = new DefaultInvocationRequest();
// invocationRequest.setDebug(true);
final Invoker invoker = new DefaultInvoker();
for (int i = 0; i < bundlesList.getLength(); i++) {
String goal = mvnCmd.toString() + bundlesList.item(i).getTextContent();
installBundle(invocationRequest, invoker, goal);
}
}
private static void installBundle(InvocationRequest invocationRequest, Invoker invoker, String goal)
throws MavenInvocationException, CommandLineException {
invocationRequest.setGoals(Collections.singletonList(goal));
final InvocationResult invocationResult = invoker.execute(invocationRequest);
if (invocationResult.getExitCode() != 0) {
String msg = "Invocation Exception";
if (invocationResult.getExecutionException() != null) {
msg = invocationResult.getExecutionException().getMessage();
}
throw new CommandLineException(msg);
}
}
}
P.S.
In case you don't need different versions of same dependency on AEM instance, it's better to avoid using OSGi <Embed-Transitive>true</Embed-Transitive> as it might introduce security/performance/OSGi issues - see Embedding_Transitive_Dependencies_Into_a_Mega-Bundle_With_Maven-Bundle-Plugin for details.
In case you once decide to switch to another ORM framework, it's better to prefer JPA EntityManagerFactory approach instead of Hibernate-specific SessionFactory - check https://stackoverflow.com/a/5640796/12547140.
I use Spring Boot 2.0, Java 8, JUnit 5 and Maven in IDE Intellij IDEA 2017.2.4.
Here's my pom.xml:
<?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>com.datastech</groupId>
<artifactId>cra</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>internal</module>
<module>external</module>
</modules>
<name>cra</name>
<description>CRA connector for Oschadbank</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lombok.version>1.16.20</lombok.version>
<elasticsearch.version>6.2.4</elasticsearch.version>
<junit.jupiter.version>5.2.0</junit.jupiter.version>
<junit.platform.version>1.0.3</junit.platform.version>
<!-- Environment variable -->
<!-- TODO добавить пользователя для БКИ коннекторов -->
<mongo.uri>resourceServer:sjv99gKm#192.168.3.43:27017,192.168.3.70:27017</mongo.uri>
<elasticsearch.host>192.168.3.43</elasticsearch.host>
<elasticsearch.port>9200</elasticsearch.port>
<ldap.url>ldap://192.168.3.90:389</ldap.url>
<ldap.base>DC=contoso,DC=local</ldap.base>
<ldap.user>CN=Artur,CN=Users,DC=contoso,DC=local</ldap.user>
<ldap.password>zaq1#WSX</ldap.password>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- AOP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- AOP -->
<!-- Util dependencies -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!-- Util dependencies -->
<!-- Elasticsearch -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- Elasticsearch -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 -->
</dependencies>
<build>
<plugins>
<!-- Checkstyle -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<configLocation>checks.xml</configLocation>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<failOnViolation>true</failOnViolation>
</configuration>
</execution>
</executions>
</plugin>
<!-- Checkstyle -->
<!-- Static code analysis -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<targetJdk>9</targetJdk>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<linkXRef>false</linkXRef>
<analysisCache>true</analysisCache> <!-- enable incremental analysis -->
<minimumTokens>100</minimumTokens> <!-- minimum code size which trips the CPD -->
<rulesets>
<!-- A rule set, that comes bundled with PMD -->
<ruleset>/category/java/bestpractices.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Static code analysis -->
<!-- Runs the unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
</dependencies>
<configuration>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
<!-- Runs the unit tests -->
</plugins>
</build>
</project>
Here's my class:
#Service
#Profile("!local")
public class CraConfigurationServiceProduction implements CraConfigurationService {
private static final String SERVICE_NAME = "CraConfigurationService";
/**
* Шаблон адреса сервиса поиска конфигурации коннектора БКИ
*/
private final String internalModuleUrl;
/**
* REST Template для взаимодействия с внутренним модулем
*/
private final RestTemplate restTemplate;
/**
* Репозиторий с настройками подключения к БКИ в распределенном кеше
*/
private final CraConfigImdgRepository craConfigImdgRepository;
/**
* Сервис логирования
*/
private final ElasticsearchService logService;
public CraConfigurationServiceProduction(RestTemplate restTemplate,
#Value("${system.url.internal}") final String internalUrl,
CraConfigImdgRepository craConfigImdgRepository,
ElasticsearchService logService) {
this.restTemplate = restTemplate;
internalModuleUrl = internalUrl + "internal/api/cra/configs/{name}";
this.craConfigImdgRepository = craConfigImdgRepository;
this.logService = logService;
}
#Override
public Optional<CraConfigImdg> getConfiguration(String craName) {
CraConfigImdg result = craConfigImdgRepository.getCraConfigByName(craName);
if (result == null) {
GenericResponse<CraConfigDto> response;
try {
response = restTemplate.exchange(internalModuleUrl, HttpMethod.GET, null,
new ParameterizedTypeReference<GenericResponse<CraConfigDto>>() {
}, craName).getBody();
} catch (Exception e) {
// TODO Добавить логирование
return Optional.empty();
}
if (response == null || response.getData() == null) {
return Optional.empty();
} else {
CraConfigImdg craConfigImdg = new CraConfigImdg(response.getData());
updateConfiguration(craConfigImdg);
return Optional.of(craConfigImdg);
}
} else {
return Optional.of(result);
}
}
}
And here's my JUnit Test:
public class CraConfigurationServiceProductionTest extends AbstractSpringTest {
#Autowired
private RestTemplate restTemplate;
#Value("${system.url.internal}")
private String internalUrl;
#Autowired
private CraConfigImdgRepository craConfigImdgRepository;
#Autowired
private ElasticsearchService logService;
private CraConfigurationService craConfigurationService;
private MockRestServiceServer mockServer;
private String serviceUrl;
#BeforeEach
public void setUp() {
craConfigurationService = new CraConfigurationServiceProduction(restTemplate,
internalUrl, craConfigImdgRepository, logService);
mockServer = MockRestServiceServer.createServer(restTemplate);
serviceUrl = internalUrl + "internal/api/cra/configs/";
}
#Test
public void getConfigurationSuccessful() throws Exception {
String craConfigResponse = loadResourceAsString("responses/internal/get-cra-configuration-successful.json");
mockServer
.expect(requestTo(serviceUrl + "kyivstar"))
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(craConfigResponse, MediaType.APPLICATION_JSON));
Optional<CraConfigImdg> response = craConfigurationService.getConfiguration("kyivstar");
mockServer.verify();
assertThat(response.isPresent()).isTrue();
CraConfigImdg craConfigImdg = response.orElse(null);
assertThat(craConfigImdg).isNotNull();
assertThat(craConfigImdg.getName()).isNotNull().isEqualTo("kyivstar");
assertThat(craConfigImdg.getUrl()).isNotNull().isEqualTo("https://scoring.kyivstar.ua/rest/api/v1/scoring");
assertThat(craConfigImdg.getUser()).isNotNull().isEqualTo("user");
assertThat(craConfigImdg.getPassword()).isNotNull().isEqualTo("password");
mockServer.reset();
}
}
When I run just this test class, I get a successful result. But when I try to do clean-test-verify with the help of the maven surefire plugin, I get the error below:
Failed tests: CraConfigurationServiceProductionTest.getConfigurationSuccessful:67 Further request(s) expected leaving 1 unsatisfied expectation(s).
0 request(s) executed.
The issue is in mockServer.verify(); field.
With JUnit4 everything worked fine, and there were no errors. I tried to use #Test from JUnit4 because I have a junit-vintage-engine dependency in pom.xml, but it did not help.
According to the JUnit official documentation, there are some incompatibilities between JUnit 5 and IntelliJ IDEA. They suggest using IntelliJ IDEA 2017.3 or newer.
I even had some problems with version 2018.1 and it was resolved with the recommended additional Maven dependencies. I also had to set the junit-jupiter-api dependency version to 5.2.0 like others.
The section Build Support with Maven can be useful to configure maven-surefire-plugin.
Since my project is on AWS and I have to connect with other AWS related instances (such as API-Gateway, for example) I am trying to use a Lambda Function to connect to a RDS instance (and I know for a fact is up and running). Running in Eclipse with the AWS plugin, in java 8. So, the idea is I code on a local Eclipse, upload the function, run it, and then, on a serverless manner, it should execute the code.
No matter what combination of code I try, or what advice I follow, I do not get a succesful connection, so I can't query the database.
With the current iteration of code I have, I get
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
I can't for the life of me figure out why I can not connect from eclipse-java-awsLambdaFunction to a mysql-aws instance. If I try to access from terminal to the database, with the same credentials, I am allowed. I have read and followed the official documentation 4-5 times already (originally I was following this, now it is slightly tweaked, and I am unsure if with this is enough for a simple connection (I have been told and read that it is!), but I wonder if there is some other way of doing it).
This is what I execute, since the plugin makes me give "A" json, even though I do not need it, I give it one like this: {"key1" : "value1"}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class Adapt_LambdaConnection implements RequestHandler<Object, String> {
private static Logger log = Logger.getLogger(Adapt_LambdaConnection.class.getName());
#Override
public String handleRequest(Object input, Context context) {
context.getLogger().log("Input: " + input);
//TODO: implement your handler
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://cmf6lelghjzq.eu-west-1.rds.amazonaws.com:3306/botdb", "bot", "PassWordRandom");
}
catch (SQLException e) {
e.toString();
log.warn(e.toString());
System.out.println(e + "\nSQLException");
}catch (Exception e) {
e.toString();
System.out.println(e + "\ngenericException");
}
String status = null;
if (con != null) {
status = "connection stablished";
System.out.println(status);
}else status = "connetion failed";
return status;
}
And this is the POM (I was using a way lighter POM, but since nothing was working, I am now using one I know for sure is right since it is cloned from an unrelated proyect we run that has no issues.
<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>com.spa</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<jdk.version>1.8</jdk.version>
<encoding>UTF-8</encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hadoop.version>2.7.1</hadoop.version>
<scala.version>2.11.8</scala.version>
<scala.tools.version>2.11</scala.tools.version>
<spark.version>2.2.1</spark.version>
<aws.version>1.11.191</aws.version>
<spark-csv.version>1.5.0</spark-csv.version>
<commons-codec.version>1.10</commons-codec.version>
<log4j.version>1.2.17</log4j.version>
<json.version>20160212</json.version>
<slack.version>1.3.0</slack.version>
</properties>
<repositories>
<repository>
<id>SparkPackagesRepo</id>
<url>http://dl.bintray.com/spark-packages/maven</url>
</repository>
</repositories>
<build>
<finalName>TypeformSurveysTransformer</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.283</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-reflect</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.tools.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.tools.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_${scala.tools.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>com.databricks</groupId>
<artifactId>spark-csv_2.10</artifactId>
<version>${spark-csv.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>net.gpedro.integrations.slack</groupId>
<artifactId>slack-webhook</artifactId>
<version>${slack.version}</version>
</dependency>
</dependencies>
</project>