How do I import jwt.io Java library to JMeter - java

I am trying to import the jjwt Java library into JMeter but I get the following error;
ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223
Sampler, message: javax.script.ScriptException:
groovy.lang.MissingPropertyException: No such property: io for class:
Script4
I am attempting to use the jjwt Java library https://github.com/jwtk/jjwt ... which can be found on the https://jwt.io/ website. I've added the files to the JMeter classpath and used a JSR223 sampler to write a script in Groovy... but it doesn't seem to accept it. I'm guessing that this is because it's expecting .jar files but the library is .java files, maybe?
Any theories on how to import this jjwt library would be greatly appreciated.
There is another post here; How to generate JWT token on JMETER using a RSA 256 private key Required library or jar file? that discusses this method and shows that it should work.

.java files are "text" files, they need to be compiled into .class files before you can run them in JVM. You can download pre-built .jar from Maven Central (make sure to fetch all the dependencies as well), or if you prefer the "hard" way:
Install Apache Maven
Create a file called pom.xml somewhere with the following content
<?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>jwt</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Open that folder in the terminal application
Execute mvn dependency:copy-dependencies command
Copy everything from target/dependency folder to "lib" folder of your JMeter installation (or other location in JMeter Classpath)
Restart JMeter
Add JSR223 Sampler to your Test Plan and put your Groovy code using the jjwt library functions there.

Related

Hamcrest Matcher signer information does not match signer information of other classes in the same package

I'm trying to write some integration tests in my Spring Boot application using REST-Assured and JUnit5 but when I run the following:
#SpringBootTest(classes = ProductsApplication.class)
class ProductsApiTest {
#Before
public void setup() {
RestAssured.baseURI = "http://localhost:8080/test/api/products";
}
#Test
public void test1() {
ValidatableResponse statusCode = given().when().get().then().statusCode(200);
}
}
A nasty error comes up:
java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer
information does not match signer information of other classes in the
same package
Please take a look at 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<scope>test</scope>
</dependency>
...
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
</dependencies>
<build>
...
</build>
</project>
Here are the Order and Export + the Libraries the Eclipse project uses:
How do I set up the Eclipse environment to work with REST-Assured and Hamcrest? Why would this exception be thrown?
I deleted the org.hamcrest.core_1.3.0.v201303031735.jar from my .p2 plugins folder and it worked for me.
I searched for "hamcrest" in "C:\Users\Dell.p2\pool\plugins" folder and found "org.hamcrest.core_1.3.0.v201303031735.jar" there.
I deleted it for this path.
Tried to run the test cases and it got passed with no failure in statusCode() method line.
Please suggest if anyone has a better solution.
In my case, the problem was solved by adding an explicit dependency to Hamcrest at the top of the dependency list in my pom.xml:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
This issue can also occur within Eclipse plugin development if you define your own dependency on a later version of hamcrest.
I worked around this by removing the hamcrest plugin from the platform definition:
Select Windows -> Preferences -> Plugin Development -> Running Platform -> Edit -> Content
Filter for "hamcrest"
Untick org.hamcrest.core
If you get this error in eclipse the problem is that eclipse already has a hamcrest-core library in its plugins folder which has priority over your maven hamcrest library.
What you can do is overwrite the eclipse one (careful to have the same version with the one you have in your maven repositories folder). I am using hamcrest-core 1.3.
I'll show the steps for MacOS but should be similar on Windows you just need to find the eclipse plugins folder and maven repositories folder on your own machine.
Close Eclipse.
Go to Eclipse plugins folder (on MacOS: ~/.p2/pool/plugins)
Find your hamcrest-core library (for me: org.hamcrest.core_1.3.0.v20180420-1519.jar)
Make a backup so that you can revert it - rename the file with a different extension (ex: org.hamcrest.core_1.3.0.v20180420-1519.bck)
Copy from the maven repositories (on MacOS: ~/.m2/repository) the hamcrest-core library into the plugins folder with the same name (on MacOS: cp ~/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar org.hamcrest.core_1.3.0.v20180420-1519.jar)
Open Eclipse and run the test, the test should now run correctly.
In my case I just removed the Eclipse's JUnit5 library from classpath.

IntelliJ can't find google-cloud-storage classes

I'm trying to use Google cloud storage and I'm following this guide: https://cloud.google.com/storage/docs/reference/libraries#using_the_client_library
I'm using Java with Maven, and this is 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.findwise.app</groupId>
<artifactId>data-pipeline</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.32.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.cloud.dataflow</groupId>
<artifactId>google-cloud-dataflow-java-sdk-all</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.70.0</version>
</dependency>
</dependencies>
</project>
As you can see I've added the google-cloud-storage dependency. My problem is that the classes used in the Java part of the guide (Storage, StorageOptions, Bucket, BucketInfo) are unfindable by IntelliJ. I can't import them. In fact, when I try to import via the absolute path:
import com.google.cloud.storage.Storage;
IntelliJ complains that the symbol google cannot be resolved. What is wrong here?
There are a few places to look:
Find your local .m2 repository and navigate to the com.google.cloud dependencies. If you don't find it, or the JARs aren't there, IntelliJ won't be able to access them.
IntelliJ will show you the External Libraries it is using under your source code in the project panel on the left.
If IntelliJ cannot import the class, it suggests that your Maven pull isn't working.

Basic maven question: Does maven transitively install dependencies?

I've looked here https://blog.packagecloud.io/eng/2017/03/09/how-does-a-maven-repository-work/ and that does seem to be the case.
However, I tried to experiment with mvn install and I'm not sure if it's worked as expected. Here's what I did
(1) I created a lib.
(2) Ran mvn install from the command line
(3) Copied the path of my newly created jar
(4) Opened a new maven project, stuck the path into my pom.xml
I'm able to reuse my library methods, BUT: one of my library methods returns a TransportClient which is part of the elasticsearch api. Using intellij inside my new project, it seems like I don't have elasticsearch even though I'm referencing the jar.
Is this expected? I was expecting it to have transitively installed elasticsearch when it referenced my jar.
I'd love a pointer or two in the right direction, I'm completely new to this. :)
My pom.xml for the lib that uses elasticsearch as dependency.
<?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>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<groupId>estutorial</groupId>
<artifactId>estutorial</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
</dependencies>
</project>
My pom.xml for the new maven project that tries to reference the lib for the above 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>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<groupId>sth</groupId>
<artifactId>sth</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>estutorial</groupId>
<artifactId>estutorial</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>/home/dell/.m2/repository/estutorial/estutorial/1.0-SNAPSHOT/estutorial-1.0-SNAPSHOT.jar
</systemPath>
</dependency>
</dependencies>
</project>
So, if I understand your steps, your dependency declaration in your referencing application uses a direct classpath to the jar file in your local repository? If so, this is unusual. You shouldn't need to know direct file locations for any of your dependencies of a Maven project. What you should be doing.
In the referenced project (that which requires the Elasticsearch library), it's pom.xml file would defined the elasticsearch dependency itself. This should follow maven standards for dependency declaration (groupId, artifactId and artifactVerion). If you don't have the elasticsearch artifact, maven will attempt to find it and store it in your local repository. You shouldn't have to have any path in your pom.xml file.
When you install the referenced project, it will install into your local repostory both the JAR file and the pom.xml.
In the referencing project, you should define the dependency to your referenced artifact in it's pom file. Same format: groupId, artifactId and artifactVersion. You shouldn't need to provide a specific path. What maven will do is find your referenced jar, but also use the installed POM.xml file for the referenced jar to find the transitive dependencies and include them in your classpath.
From what you've described, your dependency declarations aren't correct. If you can provide your POM file more details can be provided. Otherwise, review the maven intro to dependencies.
No. mvn install is a nearly useless command. It stuffs a jar file into your local repository, for subsequent use by other maven builds. You use the term 'path'. If you run mvn install:install-file to put a jar into your local repo under some coordinates, you can reference those coordinates from another pom; but it will generally lead to future problems as compare to deploying the jar into a proper repository manager.

Setting up cucumber in intellij

I'm spinning my wheels trying to run tests in intellij. I'm using the basic framework of a cucumber skeleton project as such.
I have also updated my POM file as such:
<?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>MaritProject</groupId>
<artifactId>MaritProject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
</project>
Environment: I am on jdk 1.8 and the JDK has been selected in settings. I've also gone to settings, plug ins and installed the Cucumber for Java plug in. I'm on windows 10.
Requirement: I want to run a basic test of the feature file, which without having anything yet in my runner class, should tell me that zero tests are working and give me the basic output of a step definition file to cut and paste and start fleshing out details in. Then, I want to write and execute the runner file.
Problem: But when I run this feature file, either nothing happens or when I fill out some of the edit configuration, I get "Error running: 'Unnamed: No module defined'. I am following some video tutorials where the run output window looks much different than what I had, so I upgraded my IntellJ to the latest version, 2017.3.5
I have checked the markings on my directory and it looks like Java is correctly marked as Sources under main, java is marked as test Sources under src/test and resources are marked as resources under main and resources root under test. This probably should be all I need, as the video said to do even less than this. When I run mvn clean test in the directory of my project, I get BUILD SUCCESS back.
But, since I upgraded IntelliJ in attempt to correct a different issue, now I cannot run any feature or class file. It wants me to edit my configurations first. These configurations include a main class, glue, feature or folder path, VM options, program arguments, working directory, environment variables and classpath of module. This window was not there previously when I tried to run. I don't know what to put in any of these input boxes and google results have so far been unhelpful. How do I tell what my main class is or what the glue is? Glue looks like a required field, but I don't know what path corresponds to the glue. I can guess that my working directory is where the files are stored. I have my java and maven home environment variables saved on my machine, but do I also need them in intellij?
I have had some troubles getting this stuff to work. For this configuration works:
<properties>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
<cucumber.version>1.2.5</cucumber.version>
<maven.compiler.version>3.3</maven.compiler.version>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Note that I have the versions specified a inside , they could also be provided inline.
Other than that you have to specify glue. This is the directory where your stepDefinition files are. Your glue and features and so on can be specified in a testrunnerclass or something like that. However note that this class HAS TO HAVE "Test" in the name. Something like TestRunner or CukesRunTest will do. RunnerClass will not be recognized.
Please remove
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
as you have two versions of cucumber currently in your pom.xml

maven build error: package org.apache.http does not exist

I am attempting to send a simple HTTP post from a java program (for deploying on Heroku).
I started with the demo project here.
Using mvn package builds the project successfully.
I then added my own additional file TestPost.java with a few lines of code, added it to the pom.xml, and still built fine.
Then I tried to add the HTTP code from this example (minus the package line), which uses the Apache HttpClient library.
Using mvn package results in the following error:
package org.apache.http does not exist
After searching for solutions I tried including a dependency in the pom.xml:
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
My understanding was that this should force a download of the necessary package but no download was shown on the next compile (just the same error), and the package is not visible in my user .m2\repository\ folder.
I tried inserting this dependency at different points in my pom.xml without success.
Why is the apache library not being downloaded? Please note that I am new to maven.
Here is the pom.xml that you should have if indeed you need to depend on httpclient.
<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>demo</groupId>
<artifactId>httpclient-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>httpclient-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
</project>
Now if you put your Java sources in src/main/java, where src and pom.xml are in the same directory, Maven should resolve the dependency from your local repository, and download it, should it not already be there. Your local repository is defined in the conf/settings.xml in your Maven installation directory.

Categories