Mock Session in Spring Boot and RestAssured - spring

I have a web application, running with Spring Boot. Now I have to write tests with Rest Assured.
However, for running some of them I have to be authenticated on the server. Server uses google oauth authentication. Is there any way to mock session with rest assured?
Documentation doesn't say a lot about this and ways covered there don't help.
when()
.sessionId("id here")
On the server side I'm using HttpSession with userId parameter inside.

I have found a solution.
RestAssured (since 3.0.0) has integration with MockMvc and session mocking could be reached through calling something like
given().sessionAttr("name", value)

Related

spring mock mvc tests with api being invoked from external system

I have an api(API 1) which is being stubbed through MockMvc.When I post on this API through this mock object, a request goes out to external system which in turn invokes api (API 2) of my system. Since this API 2 is invoked through http channel (host:port) and the container is not running, this breaks. How do I handle this scenario since I would not prefer to change the way external system invokes my API. Hope I have clarified.
If you're using MockMvc, you cannot test calls over the network.
So in that case, you would need to mock or stub the components that perform external network calls.
On the other hand, if you are using Spring Boot... you can then have Spring Boot's testing support launch the embedded Servlet container for the test, and externals calls can connect to the running Servlet container over HTTP. For that however, you would typically use something like Spring Boot's TestRestTemplate or core Spring's WebTestClient (available since Spring Framework 5.0) instead of MockMvc.

Using Gateway to consume Spring Boot application from Spring Integration application

I am just starting with Spring Integration with Spring Boot 2.
I am working on a application which uses Spring Integration's HTTP outbound gateway to consume a Spring boot Service.
I am consuming the spring boot service from Spring Integration application using Gateway.
When I call the Gateway method which in turn will use the outbound gateway to call the spring boot service, the request does not seem to be completed. It is just going on when I make the HTTP GET request via the browser.
The request is also not received by the Spring Boot service.
I am not able to identify what is wrong in my Integration application when using gateway to consume a Spring Boot 2 service.
I have shared my Spring Boot 2 Application and also the Integration application which I am using to consume it in the below github folder. it contains 2 folders, one for the spring Integration application and the other for the spring boot application.
https://github.com/gsamartian/spring-int-demos
I have exposed a REST interface for the Integration application using RestController.
I access the boot application from integration application via the url, http://localhost:8763/callExternalServiceViaGateway
I am able to access the spring boot application directly from its port.
If anyone can help me identify the cause it would be great.
Thanks,
Your problem that the gateway method is without any args:
#Gateway(requestChannel = "get.request.channel", replyChannel = "reply.channel")
String getCustomMessage();
In this case the gateway works as receiving. No any requests is send because nothing to wrap to the payload. See more info on the matter in the Reference Manual.
Right now I see several bugs with the payloadExpression and no arg, so I suggest you to add some String payload arg to the getCustomMessage() gateway method and perform it with an empty string.
Will take a look into the bugs and will fix them soon.
Thank you for a good sample how to catch and reproduce!
https://jira.spring.io/browse/INT-4448
https://jira.spring.io/browse/INT-4449

Spring boot stateless security with redis cache data

While doing some practice examples to learn spring security, I come up with some following use case. But I did not find best possible approach using spring security. Could some one please help me on this
I have angularJs application, Spring boot application running on different servers . In redis cache I have user info(role, and some other info) with gsid as key. In each rest call I am passing gsid as cookie. Now I want to validate each request in the Spring security Filter by fetching user info from the redis cache before sending to #Restcontroller.
what could be best approach to authenticate and authorize the request using spring boot security.
Use spring-session with redis, it also provides integration with spring-security.

Can I use Spring's MockMvc with Jersey resources?

I have successfully integrated Spring and Jersey via Spring Boot' starter POMs, and I have a couple Jersey endpoints. Now I'd like to unit test the resources. I can't seem to get MockMvc working. I get a 404 error when attempting to GET a resource endpoint.
I know there is a Jersey test framework out there, but it appears to launch a server. I'm hoping to avoid "integration" type tests and keep this as simple as possible. Can I do this with MockMvc?
Unfortunately doesn't seem to be possible as MockMvc doesn't actually start servlet container.
You can use
RestTemplate to fire requests against server started on localhost. Here are examples from Spring Boot repo: Example 1 and Example 2.
Rest Assured library

Junit for spring security

I've used the spring security http element to enforce security, in the securityContext.xml. I am able to successful secure my web app. I want to write a junit for the same. Will I be able to write it.

Resources