for example connecting to client:
val client = Cluster.open(MapConfiguration(config)).connect()
and executing the query
client.submitAsync(PreparedStatement.addUser, map).await().all().join()
PreparedStatement.addUser - gremlin string
The question is: how to unit test client queries in Java. I thought of unit testing our queries with inMemoryDB. Can we run inMemoryDB as a client, because by default return traversal() if connecting to memory DB? Or can we execute a gremlin query string on traversal?
If you are sending Gremlin strings to Gremlin Server, you could treat your tests as integration tests and actually start a Gremlin Server instance with a JanusGraph in-memory instance configured perhaps. In some cases, a TinkerGraph might suffice as well depending on what you need to test.
If you wanted more pure unit tests you would have to do some mocking, but that will be complicated as there are some non-public classes involved and the class dependencies are non-trivial. I think the first problem would be with the ResultSet and ResultQueue classes that the Client needs. I created this issue TINKERPOP-2428.
I think that bytecode based requests are less challenging that scripts as you can mock the RemoteConnection to return your own Traversal instance. There is even an EmbeddedRemoteConnection that would let you define a local GraphTraversalSource to test against (if that situation suited you).
Related
I have an app that connects to an API endpoint and displays a list of people. I want to write a unit test for the app. But I am not sure what to test here. There is no arithmetic operation happening It's just fetching the data from API and displaying it.
What should the unit test cover in such a scenario?
If a test can never fail, then it's not really testing anything. In your case though, a network api call is being made. And network calls can fail all the time. Depending on how you are making your network call you can either:
create a fake web server that can return a variety of error codes
create a mock api service that can return a variety of error codes
don't test anything
There are all kinds of tests you can use, behavioural, unit, functional, integration, black box, user acceptance testing.
What does testing do for you? Does it document code behaviour? Does it lock in the behavior of a function? Does it ensure that something works?
Depending on your needs, you may not need a test. Or, you may need a lot more. It's up to you.
Unit tests are designed to ensure that a behavior or set of behaviors occur(s) when you invoke a unit of code.
In this case, you have code that is fetching the data from an API and returning it. You might want to test the following:
Your code makes a network call to the API.
When the API returns a successful response, your app renders the data.
When the API returns a failed response, you gracefully handle the failure.
Of course these steps will probably vary depending on your use case. You can look into stubbing the API to understand how you can simulate API invocation failures.
I've a RESTFul microservice written in Grails. This microservice has it's own DB. It also depends on two other microservices.
A typical workflow of the service is:
Receives a GET request from a client e.g. browser
Calls another microservice throw http to get some information
Queries it's own DB to get some data
Send a response to the client
I want to write automated tests for this service. Whenever I'll run the test, it'll do the following:
Mock the external microservices
Create a database and populate it with test data
Run the application and configure it to use the mock services and DB
Run the test cases by sending http requests and matching responses
Here are my questions:
What type of test is this?
What are some good frameworks or tools to write this type of test?
I've a very little knowledge on testing so may be I'm asking stupid questions.
My first suggestion is that your service not know about HTTP requests. It should be a provider to a higher level layer like https://jersey.java.net that can make requests and map the POJO's your service class returns into JSON or whatever to return in HTTP responses.
Your method for #4, "Send a response to the client" should use those external dependencies to gather information and then make a decision.
Some cases to cover:
If the other micro service can return A, B, or C and the DB can return D, E, or F, then you have 9 scenarios to test. Use mocks to provide all those varieties of data and then make assertions about the result. If there are many more possibilities, then consider adding extra methods for information-gathetering and testing them on their own.
I'm developing a web service in Java EE using Apache Tomcat and so far I have written some basic server side methods and a test client. I can successfully invoke methods and get results but every time I invoke a method, the server constructor gets called again, and I also can't modify the instance variables of the server using the set methods. Is there a particular way to make my server stateful without using JAX-WS or EJB #Stateful tags?
This is a little bit of misconception here. The stateful EJB would maintain session between one client and server, so still the EJB state wouldn't be shared between various clients.
You can expose only stateless and singleton EJBs as a JAX-WS web service.
The best option is to use database for storing all bids and when the auction is finished choose the winning one.
If you want to use a file it is fine, as long as you like to play with issues like:
synchronizing access to that file from many clients
handling transactional reads and writes
resolve file corruption problems
a bunch of other problems that might happen if you are sufficiently unlucky
Sounds like a lot of work, which can be done by any sane database engine.
I am looking for a clean way to add service oriented access to an existing GWT application (client + RemoteService based server). The thing is that all the services are already in place, described by the #RemoteServiceRelativePath notation. It would be nice to be able to actually add the #WebService notation and have access to them both with RPC and XML/JSON/..
The real problem is that extending a current application to support other clients than the existing GWT one is a bit hard because of the GWT obfuscation. This also leads to an unneeded coupling between client and server since they both need to be deployed at the same time, because of the .gwt.rpc generated files.
I would like to reuse the existing RemoteService interfaces to define web services and connect to them with new clients via a plain-text protocol. Additionally, I would like to port the existing GWT client to the same protocol.
Is it possible to do this while using the same interfaces and implementation just by annotation?
What would be the best way to port the existing client to use a plain text protocol, RequestBuilder? Or just inject a new serialization implementation that does xml / json?
I don't even know where to start with this, this is why I'm asking. Maybe it is better to rewrite all the services and port everything at once but it will break everything until this is finished.
We've had a different approach since GWT the coupling of GWT between server and client side is not all bad but gives you a nice integration and you don't have to think too much about communication issues etc.
For that, our application had a frontend tier which consisted of the full gwt stack (client + server-coupling) and on the server-side, we connected via spring and RPC to the service layer.
On that way you can use on the benefits of spring and you don't loose the comfort of GWT.
But I Would like to hear if somebody already has gone other ways ;)
This is rather late and GWT is not the wonderchild it once was. However, for the sake of tying loose ends here's the solution I went for:
create a Java generator that parses all model (shared client/server classes) files through reflection and generates a Java file that reads/writes SOAP objects
bootstrap the above into a generic Java handler that handles native objects + array, sets, maps
write the service that can deal with the generated XML from the files above
It sounds a bit terse and a bit complicated but it 'only' took ~1 month to write the code to reliably convert >200 objects to their XML representation, automatically. The added benefit is that it allows mocking and cross-platform clients/servers.
As a summary, the generated code creates new methods 'fromXML' and 'toXML' that feed the fields that are public members (get/set) in the given class. So, given MyClass it would generate the MyClassSerializer and MyClassDeserializer Java classes that implement those SOAP-specific methods and also publish themselves to a 'dispatcher'. So whenever that dispatcher sees 'MyClass' it would know where to get the ser/deser functions from.
As the headline says, how would you test a client/server application, that is written in C/C++, that talks through a protocol over a network? Im a bit confused on how to do this. I have thought about making some mocking, but I have never tried mocking, so I dont know if this is the best way.
How should I do this? I have written many unit tests, but never tried to test something that interact over a network.
I use the command pattern in the unit test driver (client) to send test commands to the server. The advantage of this is that the test is coded in one place.
Example for testing a request timeout:
Client sends a sleep command to server and then the request. The request times out and the test case is passed.
Typically you'll want to use mocking to verify that each side reacts as required to messages from the other side (e.g., that when the client receives a response from the server that it processes that response correctly).
To test the network functionality itself, you can test both running on the same machine, and you can run one (or both) inside a virtual machine. If you have two network adapters, you can even dedicate each to a virtual machine so the network traffic actually goes out one, to a switch/router, and comes back in the other (particularly useful when/if you want to capture and verify packets).
I have some client/server code that I unit test through the loopback address. I use some mocks when I have to test error conditions. So, I test with the real code when I can and the mocks when I need to trigger very specific conditions.