Need help in REST ASSURED - rest

I am starting with REST Assured, getting error while executing below code :
Code 1-
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method equalTo(String) is undefined for the type
Code 2 -
RestAssured.expect().statusCode(200).
body(
"name", Matchers.equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
Exception-
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
Below are the only 2 methods in my class, I am having issue with first one, second one is running fine. Please let me know what I am missing in first method.
Method -1
public static void testCountriesCallingCode() {
RestAssured.expect().statusCode(200).
body(
"name", equalTo("Russia")
).
when().
get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7").asString());
}
Method-2
public static void testCountriesCallingCodeUsingJSONPATH(){
Response res = RestAssured.get("http://restcountries.eu/rest/v1/callingcode/7");
System.out.println(res.getStatusCode());
String json = res.asString();
JsonPath jp = new JsonPath(json);
System.out.println(jp.get("name"));
}

Thanks Hti, your answer worked. Without the other dependencies, Rest Assured kind of works. I have no idea why Rest Assured website does not note this. Following in pom.xml worked
<properties>
<rest-assured.version>3.0.2</rest-assured.version>
<resteasy.version>3.0.17.Final</resteasy.version>
</properties>
...
<!-- Jackson is for allowing you to convert pojo (plain old Java object) into JSON -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>2.4.11</version>
<scope>test</scope>
</dependency>

Change the body of your first example to:
body(
"[0].name", equalTo("Russia")
)
That is because the JSON response from the server is not an object, but an array, and you have to query for the first object ([0]), then the name (.name).

For the Code-1, for equalTo() method you have to import org.hamcrest.Matchers.*;
For the exception in code 2, it is very hard to mention without looking at the RESPONSE but try to follow below link if you have nested generic parameters in your response.
How to validate nested response using REST Assured?
Please let me know if you have any issue or question. Thanks!

Even though this question is old, I just stumpled upon the second problem:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.jayway.restassured.internal.ContentParser.parse() is applicable for argument types: (com.jayway.restassured.internal.RestAssuredResponseImpl, com.jayway.restassured.internal.ResponseParserRegistrar, com.jayway.restassured.config.RestAssuredConfig, java.lang.Boolean) values: [com.jayway.restassured.internal.RestAssuredResponseImpl#753455ab, ...] Possible solutions: wait(), any(), grep()
This is due to missing dependencies. In my case I needed to add the dependencies for xml-path and groovy-xml, even though I'm just working with JSON data. So the best thing to do is resolving the dependencies transitively.

equalTo comes from Hamcrest which is a JUnit dependency contained within the JUnit jar. You probably just need to import the static method for it from Hamcrest.
import static org.hamcrest.core.IsEqual.*;

Add a static package for equal to:
import static org.hamcrest.Matchers.*;

Related

camel 3.8 generates REST routes twice

I faced a problem with camel REST routes when I switched from camel version 3.5.0 to version 3.8.0.
In the version 3.8.0 all routes from the REST service are generated twice! So at start of the camel context the exception is thrown:
Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
In detail:
I have a simple RouteBuilder to generate REST routes:
#Component
public class RestInterface extends RouteBuilder {
#Override
public void configure() throws Exception {
restConfiguration()
.component("jetty")
.host(urlInfo.getHost())
.port(urlInfo.getPort())
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true")
.clientRequestValidation(true);
rest("/")
.get("/start").outType(RepositoryPackageInfo.class).to("direct:start")
.get("/stop").outType(RepositoryPackageInfo.class).to("direct:stop");
// ...
}
}
When I start this code with camel 3.8.0 I get following console output:
...
jetty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#5f65e0c0{/,null,AVAILABLE}
Started ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4095ms
Error starting CamelContext (camel-1) due to exception thrown: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
org.apache.camel.FailedToStartRouteException: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
...
Apache Camel 3.8.0 (camel-1) shutting down (timeout:45s)
Stopped ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Stopped o.e.j.s.ServletContextHandler#5f65e0c0{/,null,UNAVAILABLE}
Routes shutdown summary (total:40 stopped:40) <-------------- total 40 routes!!!
...
Stopped route8 (rest://get:/:/stop)
Stopped route7 (rest://get:/:/start)
Stopped route2 (rest://get:/:/stop)
Stopped route1 (rest://get:/:/start) <-------------- the routes "stop" and "start" are doubled!!!
...
If I run the same code with camel 3.5.0, the application starts without exceptions, and camel has only 38 routes:
etty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#6cbb6c41{/,null,AVAILABLE}
Started ServerConnector#42172065{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4034ms
Route: route1 started and consuming from: jetty:http://0.0.0.0:6060/start
Route: route2 started and consuming from: jetty:http://0.0.0.0:6060/stop
...
Total 38 routes, of which 38 are started <-------------- total 38routes!!!
...
Completed initialization in 16 ms
Thank you in advance. I hope somebody can help me with this issue.
For completeness, here is a part of my pom.xml
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rest</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-zipfile-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
Check if configure method on your RestInterface RouteBuilder is getting called more than once. Multiple calls cause duplicate RouteDefinitions to be added to the RouteBuilder instance which can lead to this error.
One way to solve this is to use boolean value in your RouteBuilder to check if configure method has already been called. Then use this value to early out or to throw an exception.
boolean isConfigured = false;
#Override
public void configure() throws Exception {
if(isConfigured){
return;
}
// Configure routes
isConfigured = true;
}
Encountered similar problem with Camel 2.x running in Karaf + OSGi where modifying config file lead to same error.
Basically configuring CamelContext service caused it to restart while the RouteBuilder services it referenced kept their state leading to situation where their configure method got called the second time leading to same error.

Integrating Spring-Shell with the MongoDb driver

Is it me, or are the MongoDb drivers and Spring-Shell deeply incompatible? To start, I'm not talking about the Spring-Data-Mongo stuff, I'm talking about the actual java client that the MongoDb folks put out.
My Pom is as follows:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.11.0</version>
</dependency>
</dependencies>
If I try to use the MongoDb client from the Spring shell, I consistenty get noclassdeffound errors all over the place. A simplified bare bones shell method is as follows:
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import java.util.Date;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
#ShellComponent
public class AuditCommands {
#ShellMethod("Just testing here")
public int cube(int number)
{
return number*number*number;
}
#ShellMethod("Sends a test document to mongo")
public void mgo()
{
System.out.println("Hello there. Doing some mongo stuff");
//MongoClient mongoClient = MongoClients.create();
MongoClient mongoClient = MongoClients.create("mongodb://whateversite:12345");
// New up a registry to automatically handle pojos
CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder().automatic(true).build()));
// Grep database instance
MongoDatabase database = mongoClient.getDatabase("MyDb");
database = database.withCodecRegistry(pojoCodecRegistry);
MongoCollection<Audit> collection = database.getCollection("MyCollection", Audit.class);
Audit audit = new Audit();
audit.setAuditId(1);
audit.setAuditTypeId(5);
audit.setCreatedOn(new Date());
audit.setMessage("Making mongo great again..");
collection.insertOne(audit);
System.out.println("Done..!!..");
}
}
I receive the following error if I try to execute the "mgo" ShellMethod in my example I get the following error.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-02 18:10:54.634 ERROR 18848 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.mongodb.client.internal.MongoClientImpl.<init>(MongoClientImpl.java:67)
The following method did not exist:
com.mongodb.MongoClientSettings.getAutoEncryptionSettings()Lcom/mongodb/AutoEncryptionSettings;
The method's class, com.mongodb.MongoClientSettings, is available from the following locations:
jar:file:/C:/Users/xxxxx/.m2/repository/org/mongodb/mongodb-driver-core/3.8.2/mongodb-driver-core-3.8.2.jar!/com/mongodb/MongoClientSettings.class
It was loaded from the following location:
file:/C:/Users/xxxxx/.m2/repository/org/mongodb/mongodb-driver-core/3.8.2/mongodb-driver-core-3.8.2.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.MongoClientSettings
Process finished with exit code 1
If I remove Spring-Shell and Spring-Boot, that MongoDb code works fine.
So what gives here? Am I missing some essential point here or is this stuff essentially broken? I'm not a Java/Spring native, so I'm sure it won't come as a surprise when I say that connecting to Mongo and throwing a couple of documents around comes off muuuuuuch cleaner in C#, Python, and node. (And yes I know I can use spring-data-mongo, but that just seems like a really opinionated API for someone coming from a different language background)
Okay, I'm going to answer my own question here because I've learned a little more about this.
I wound up giving up on trying to make spring boot exclude all the mongodb dependencies that I didn't want and didn't ask for. So this wasn't really a spring-shell issue. I wound up just matching the version of the driver that spring boot is using in my own pom.
As in..
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.8.2</version>
</dependency>
Once you do that, you get a bunch "connecting to localhost:27017...." issues. You can fix this by excluding the ludicrous MongoAutoConfigure that spring defaults to. More specifically, you have to go with a parameterized SpringBootApplication annotation like this:
#SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class Main {
public static void main(String[] args)
{
SpringApplication.run(Main.class);
}
}
As I mentioned earlier, I'm not a Java native, so my opinions are heavily flavored by the frameworks I grew up on. But.. The idea that spring just automatically tries to connect to a potentially networked resource is completely asinine to me. It's one thing if I'm actually trying to use spring-mongodb and it's super opinionated pattern, but I'm not in my case. The equivalent would be if I pulled the Dapper assemblies from NuGet and they tried to log into the nearest local instance of sql server just for the heck of it. Very sketchy value proposition at best, and surface area for some sort of creative exploit at worst. I just don't see what "I" get out of this behavior.

Restlet client resource "not modified" condition

I've got a Restlet based application, and I'm trying to use Restlet client resources to test certain parts of it.
Since upgrading from Restlet 2.2.3 to 2.3.4, my ETag verification tests have started failing. Here's how I was adding the header in the old version:
Series<Header> headers = (Series<Header>) currentClientResource.getRequest().getAttributes().get("org.restlet.http.headers");
if (headers == null) {
headers = new Series<Header>(Header.class);
}
headers.add("If-None-Match", "\"" + eTag + "\"");
currentClientResource.getRequestAttributes().put("org.restlet.http.headers", headers);
Then when calling represent() again on the wrapped clientResource I was getting a 304 Not Modified response (which is what I want).
In 2.3.4 this started returning a 200 OK instead, and I noticed a log message about not setting the If-None-Match header directly.
Instead I'm now trying this:
currentClientResource.getRequest().getConditions().getNoneMatch().add(new Tag(eTag));
However this is still giving me a 200 OK. If I do the request manually through a REST client I can get a 304 Not Modified, so the server is still doing the right behavior. What do I need to do in the tests to see what I want to see?
I made a try and it works for me with version 2.3.4 of Restlet.
Here is what I did:
The Maven dependencies for my test
<project>
<modelVersion>4.0.0</modelVersion>
(...)
<properties>
<java-version>1.7</java-version>
<restlet-version>2.3.4</restlet-version>
</properties>
<dependencies>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet-version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.jetty</artifactId>
<version>${restlet-version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.jackson</artifactId>
<version>${restlet-version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.crypto</artifactId>
<version>${restlet-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.com</url>
</repository>
</repositories>
</project>
A server resource that sets the etag on the returned representation:
public class ETagServerResource extends ServerResource {
#Get
public Representation test() {
String test = "test";
String md5 = DigestUtils.toMd5(test);
StringRepresentation repr = new StringRepresentation(test);
repr.setTag(new Tag(md5));
return repr;
}
}
The client that makes two calls: a first one without the etag and a second one with the etag that should return a 304 status code.
// First call
ClientResource cr
= new ClientResource("http://localhost:8182/test");
Representation repr = cr.get();
Tag tag = repr.getTag();
System.out.println(">> cr = "+cr); // Status code: 200
// Second call
cr.getRequest().getConditions().getNoneMatch().add(tag);
cr.get();
System.out.println(">> cr = "+cr); // Status code: 304
I don't know what you use within the server resource. Feel free to tell me.
Hope it helps you,
Thierry

Unable to add label using Neo4j Rest API - Error reading as JSON ''

The neo4j rest api throws runtime exception (error reading as JSON '') when trying to add a label.
My current set up
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.neo4j.app</groupId>
<artifactId>neo4j-server</artifactId>
<version>2.0.0</version>
</dependency>
Code that tries to create a new node and add a property and label. The runtime exception is thrown when we try to add the label. The rollback is working fine though. It appears that the API is trying to get details for the resource that is not yet created and trying to parse the response.
try ( Transaction tx = db.beginTx() ) {
//create new user
Node userNode = db.createNode();
userNode.setProperty( "id", id );
userNode.addLabel(DynamicLabel.label("GuestUser")); //throws runtime exception
tx.success();
}
Stack trace
java.lang.RuntimeException: Error reading as JSON ''
at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:57)
at org.neo4j.rest.graphdb.util.JsonHelper.jsonToSingleValue(JsonHelper.java:62)
at org.neo4j.rest.graphdb.RequestResult.toEntity(RequestResult.java:114)
at org.neo4j.rest.graphdb.RequestResult.toMap(RequestResult.java:120)
at org.neo4j.rest.graphdb.ExecutingRestAPI.getData(ExecutingRestAPI.java:501)
at org.neo4j.rest.graphdb.RestAPIFacade.getData(RestAPIFacade.java:179)
at org.neo4j.rest.graphdb.entity.RestEntity.getStructuralData(RestEntity.java:75)
at org.neo4j.rest.graphdb.entity.RestNode.labelsPath(RestNode.java:188)
at org.neo4j.rest.graphdb.entity.RestNode.addLabel(RestNode.java:147)
Caused by: java.io.EOFException: No content to map to Object due to end of input
at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:2775)
...
at org.neo4j.rest.graphdb.util.JsonHelper.readJson(JsonHelper.java:55)
... 43 more
Has anyone seen this problem so far.
This is a problem as those "pseudo" transactions only aggregate operations to send them at once at commit.
So you cannot do "read your writes" or make decisions on them.
And the addLabel operation uses the path returned from the structural info of the node which does not yet exist.
Don't think it's worth fixing. If you think so, please raise an issue at https://github.com/neo4j/java-rest-binding/issues

JAX-RS Jersey Error java.lang.NoSuchMethodError

I wanted to implement a file-upload function for my jersey based rest server.
when i set my pom.xml (using maven) to:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.0</version>
</dependency>
i get following error:
JAX-RS Jersey Error java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.ReflectionHelper.getContextClassLoaderPA()Ljava/security/PrivilegedAction;
without the "jersey-media-multipart"-dependency the rest server is working but i cant use the file-upload functions.
Following the important part of source code:
ResourceConfig resourceConfig = new ResourceConfig(RestWebServer.class);
//resourceConfig.register(MultiPartFeature.class);
URI endPoint = new URI(pathServer);
server = JdkHttpServerFactory.createHttpServer( endPoint, resourceConfig );
RestWebserver.java:
#Path("/fileupload")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormParam("file") InputStream uploadedInputStream,
#FormParam("file") FormDataContentDisposition fileDetail)
{
String uploadedFileLocation = "c://" + fileDetail.getFileName();
// save it
saveToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
Not a Jersey user so I am just guessing, but you probably have a jar mismatch.
Try replacing your second entry with this:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.4.1</version>
</dependency>
Basing my guess on the chapter 8, have you registered your client/server code?