I’m working on contract tests using Pact in a Java application with Quarkus. However, I’m running into a snag: I need to modify the requests before sending them, since authentication is required for the endpoint. I found a solution in the documentation that involves sending an authorization with each request, but the token has an expiration period. This means that the current solution is not ideal, as I have to manually generate and replace the token in the pom.xml file every time I run the tests.
To solve this problem, I found a code that theoretically allows you to import a package into pom.xml, which contains a class with a method to return the token. However, when trying to verify the contract, the request fails because it can’t resolve the class.
Below is the folder structure I have in the project, note that inside the structure I have a file called OAuthTokenRequest.java, which is responsible for generating the token needed for the request. When compiling using the ./mvnw compile quarkus:dev command, the classpah is updated and from there we have the OAuthTokenRequest.class file which is what I’m trying to import into pom.xml.
.
├── ...
├── src
│ ├── main
│ │ ├── docker
│ │ │ └── ...
│ │ ├── java
│ │ │ └── ...
│ │ └── resources
│ │ └── ...
│ └── test
│ └── java
│ ├── com
│ │ └── company
│ │ ├── pact
│ │ │ ├── README.MD
│ │ │ ├── consumer
│ │ │ │ └── ContractConsumerTest.java
│ │ │ └── provider
│ │ │ └── ContractProviderTest.java
│ │ └── resources
│ │ └── ...
│ └── it
│ └── util
│ ├── OAuthConfig.java
│ ├── OAuthTokenRequest.java
│ └── OAuthTokenRequestTest.java
└── target
├── classes
│ └── ...
└── test-classes
├── com
│ └── company
│ ├── pact
│ │ ├── README.MD
│ │ ├── consumer
│ │ │ └── ContractConsumerTest.class
│ │ └── provider
│ │ └── ContractProviderTest.class
│ └── resources
│ └── ...
└── it
└── util
├── OAuthConfig.class
├── OAuthTokenRequest.class
└── OAuthTokenRequestTest.class
Now, below is the code snippet with the “attempt” to import the method into the class to generate the token and use it in the application.
<plugin>
<groupId>au.com.dius.pact.provider</groupId>
<artifactId>maven</artifactId>
<version>4.6.7</version>
<configuration>
<serviceProviders>
<serviceProvider>
<name>ConnectorsProvider</name>
<requestFilter>
// This is a Groovy script that adds an Authorization header to each request
import it.util.OAuthTokenRequest
String authHeader = "Bearer " + new OAuthTokenRequest.getToken()
request.addHeader('Authorization', authHeader)
</requestFilter>
<protocol>https</protocol>
<host>gateway.production.io</host>
<port>443</port>
</serviceProvider>
</serviceProviders>
<pactDirectory>target/pacts</pactDirectory>
<pactBrokerUrl>http://localhost:9292</pactBrokerUrl>
<pactBrokerUsername>pactBrokerUsername</pactBrokerUsername>
<pactBrokerPassword>pactBrokerPassword</pactBrokerPassword>
<trimSnapshot>true</trimSnapshot>
<configuration>
<pact.verifier.publishResults>true</pact.verifier.publishResults>
</configuration>
</configuration>
</plugin>
One detail about this section is that when I use something with an explicit token, it works perfectly.
I’ve tried rewriting it in other ways like:
<requestFilter>
// This is a Groovy script that adds an Authorization header to each request
import it.util
String authHeader = "Bearer " + new OAuthTokenRequest.getToken()
request.addHeader('Authorization', authHeader)
</requestFilter>
or
<requestFilter>
// This is a Groovy script that adds an Authorization header to each request
import it.util
request.addHeader('Authorization', OAuthTokenRequest.getToken())
</requestFilter>
But the error like is displayed:
Request Failed - startup failed:
Script1.groovy: 3: unable to resolve class it.util.OAuthTokenRequest
@ line 3, column 17.
import it.util.OAuthTokenRequest
^
1 error
I appreciate any help you can offer.
5