Spring Boot Profile Switch Real or Mock API - spring

I have application.yml
spring.active.profile=default,dev
api-url: http://dev-api.com
I have another file application-mock.yml which has below api-url related to wiremock.
application-mock.yml
api-url: http://localhost:7845
I have requirement to provide the developer/tester to use mock api-url or real api url from test environment based on their needs.
We have central puppet configuration file test.yml which has below content which will call real api properly and it works as expected currently.
test.yml
spring.active.profile=default,test
api-url: http://test-api.com
Now, i need to provide the option to use mock-api url from test envionment. I cant edit api-url in test.yaml in puppet configuration to point to http://localhost:7845 because it affect functionality on calling real api.
I want to make real api and mock api also should work based on developer/tester needs instead of changing or committing test.yml.
my spring java service contains value annotation for api-url to retrieve url.
Kindly provide me your valuable input.

Related

How to generate an OpenAPI from a Spring app without running the app?

I can run a Spring Boot application and then use the springdoc-openapi-maven-plugin artifact to generate an OpenAPI spec.
However, is there also a way to generate the spec without running the application first?
I currently want to generate the spec in a GitHub Action and a direct way would simplify this a lot.
you can use swagger editor: https://editor.swagger.io/
It allows you to write a json/yaml with your specifications and at the same time you can view the result.
Also, in the upper part there are 2 features, which allow you to generate the code based on the json/yaml made.
For example you can create a spring application with all the endpoints you go to specify in your json/yaml ( server).
But you can also generate HTML. (Client)

Spring boot best practises

I have a spring boot application that reads from an excel document. This is currently being done by a service that my controller uses.
Currently the path of the document is hard coded in the Service class. I would like to know, if this is the best way to do it.
I would also like to know the best practises for unit testing my code. How do I ensure that I have no dependency with the actual file. My helper method is private. I am having troubles mocking it.
Configuration details should be externalized for both convenience (easy deploy, testing) and security (plain text in your code vs environment variables). Here's a good discussion on the subject.
Mocking is the way to unit-test it. You should be more specific on the problems you have with this and IMHO it would be best to ask that in another question.
You can provide the path in your "application.properties" file by any variable name.
For example :
my-file=/home/path/to/file.extension
Then, in your service class, declare a variable like :
#Value("${my-file}")
private String filePath;
Now file will have the value which you provide in the application properties. When your application starts, it will automatically binds the path of your file in your variable. In this way, it is easy for you to modify the path. Spring framework will magically manages it out of the box.

JMeter - Test all APIs in the deployed war file

I want to do load testing of all APIs in the deployed war file of my Spring-boot project. Is it possible to configure JMeter to test all APIs in the war ? Or is there a way to programatically get all API names (RequestMapping) in the war file ?
If it is running API already (as you said it is deployed war file) you have to know the endpoints (in case you don't, you have to refer to documentation of API, if you are developer of that API you have to have documentation and of course know the endpoints, for documentation refer to here https://www.blazemeter.com/blog/how-to-automatically-document-api-endpoints-via-swagger as a starter)
When you find out the information about your endpoints (endpoints, authentication, expected body in case of post, accepted content type and etc) you can use JMetter UI and create your scenarios and also, you can refer to BlazeMeter and get much more help in real time.
Hope it answered your question and solved your problem

Mocking Rest Api with Swagger Documentation

Whole idea is to create Mock service for UI guys to test without actual api.
- We have define specification in .ymcl files.
- Now want to generate Swagger-UI and Mock rest URI with mock data.
- Using spring-boot.
Please suggest some way to generate the same.
Given that you've the API documented in Swagger/OpenAPI spec (YAML format), you can use Swagger Codegen to generate Spring boot server stub.
Please pull the latest master of https://github.com/swagger-api/swagger-codegen and follow the instruction in the wiki:
https://github.com/swagger-api/swagger-codegen/wiki/Server-stub-generator-HOWTO#java-springboot

How to set up a swagger-ui standalone server/application?

I would like to set up a standalone swagger-ui application, to view the different APIs from different servers in one central place.
In a second step I would like to customise swagger-ui to show multiple APIs at once.
I don't want to add swagger-ui to all the servers that provide swagger api-docs though.
To do so I would like to use spring boot and thought this should be an easy task. However, I have trouble getting it to work.
Here is what I did:
Generated a Spring Boot application using https://start.spring.io
included spring-boot-starter-web
added io.springfox:springfox-swagger-ui:2.3.1 dependency
When opening http://localhost:8080/swagger-ui.html I see a 404 error and UI seems broken:
Is there any reason for using Spring-boot instead of a simple web server for this?
See for example here with Nginx, including some basic authentication (pretty old link but still looking alright), or in the ReadMe of the swagger-ui github reposiory directly for easily serving with Connect/gulp-serve inside Docker (the setup can also be reproduced directly without Docker if wanted).
Also I have no idea why you're getting resources requested by the page on a different port... Just ask in case you still need help now on this topic.

Resources