Unit Test Swagger Output - unit-testing

I am using Swagger in an ASP.NET MVC WebAPI project. This project has Swashbuckle nugget package installed and generates Swagger UI and Swagger/docs/v1. A consistent problem I'm having is developers will break the swagger file by not carefully naming their webAPI operations. I would like to add a unit test to prevent me from finding out that swagger/docs/v1 isn't available by going to the Swagger UI site after deployment and seeing an HTTP 500 displayed in the swagger UI. Does anybody know how to write a unit test to validate that Swashbuckle can successfully generate the swagger docs?

Found a great way to do what I want:
[Fact]
public async Task TestSwagger()
{
var webHostBuilder = new WebHostBuilder()
.UseEnvironment("Test") // You can set the environment you want (development, staging, production)
.UseStartup<Startup>(); // Startup class of your web app project
using (var server = new TestServer(webHostBuilder))
{
using (var client = server.CreateClient())
{
string result = await client.GetStringAsync("/swagger/v1/swagger.json");
JObject root = JObject.Parse(result);
}
}
}
This uses the following nuget package:
Microsoft.AspNetCore.TestHost

Look at the UnitTests we have on Swashbuckle :
https://github.com/domaindrivendev/Swashbuckle/tree/master/Swashbuckle.Tests/Swagger
I'm sure one of those is really close to what you are looking for.
...But if is just the naming of the webAPI operations you could just loop over those using GetApiExplorer and make sure they follow your rules

To whom may be looking for a Asp.Net Core Swagger testing solution, I suggest taking a closer look at Cristophe Blin's aproach

Related

MobileFirst Adapters and Unit Test

IBM MobileFirst offers out of the box the possibility to invoke REST Services by server components written in plain Javascript, so called 'Adapters'. Since my app contains a lot of business logic inside them, I need to test it by Unit Test.
Could somebody suggest me a good framework how to create Unit Testing for 'server side' Javascript ?
Thanks.
You can write your own tests as mentioned in this post : https://mobilefirstplatform.ibmcloud.com/blog/2016/04/26/MobileFirst-adapters-automatic-testing/ .
Alternatively swagger UI and Postman can also be used as mentioned here : https://www.ibm.com/support/knowledgecenter/en/SSHS8R_8.0.0/com.ibm.worklight.dev.doc/devref/c_testing_adapters.html

Unit Testing Swagger Output

We are using Swashbuckle to generate the swagger output for the REST endpoints of our MVC api application. I'm wondering what options there are for unit testing the swagger in a test project. I want to verify things like method names, descriptions, parameters etc etc to minimize the possibility that breaking changes can be introduced.
Thanks,
Please try Swagger-Codegen, which is a free an open source project to automatically generate API clients, server stubs and API documnetation.
For the C# API client generated by Swagger Codegen, it comes with test files for unit testing and you can update it to detect breaking changes by reusing the same tests after updating the OpenAPI/Swagger Spec files.

How to write unit tests for server side Meteor code?

I have some server side code -- meteor methods and simple backend helpers -- that I would like to test. I've read the documentation testing with Meteor, but I am having a hard time connecting the documentation to my very simple use case. Can someone share with me how they've tested a meteor method or a simple backend JS function?
For instance, let's say you have some server method in, some_methods.js
function someHelper() {
// does lots of cool stuff
};
Meteor.methods({
'user/update' (userProperties) {
// updating some user properties
someHelper();
}
})
We have developed unit and integration tests for our open source application called RadGrad (https://radgrad.org).
For details on how we do unit and integration testing, please see:
https://www.radgrad.org/docs/developer-guide-testing.html
Here is an example of a unit (server-side only) test:
https://github.com/radgrad/radgrad/blob/master/app/imports/api/career/CareerGoalCollection.test.js
And here is an example of an integration (client + server) test:
https://github.com/radgrad/radgrad/blob/master/app/imports/api/career/CareerGoalCollection.methods.app-test.js
We do not have extensive UI tests; you'll need to use something like Selenium for that. UI testing in Meteor is no different from UI testing for any other web app.

Run hubot as a part of express app

I have a pretty standard express app, built using the express-generator. Now, I would like to automate some of the things in the app with hubot and I have managed to successfully perform testing and run hubot with slack adapter. However, I would like to have the bot be a part of a regular app.
How can I change the structure of the app (I have a pretty standard import of routes.js which has all of the routes for the app) to allow for the two to run together?
This is running on azure as a WebApp and I have set up a continuous integration with GitHub, so I pretty much just push code and it gets deployed, I don't run anything manually on the actual server. I would be able to run the hubot and server it on a different subdomain or path on the app if it was a regular VPS, but since the azure is taking care of those things, I would need the hubot somehow baked-in the actual express app.
As I know, Hubot has a build-in express web framework that can serve HTTP requests. So theoretically you can integrate hubot with your express webapp thru the router dispatch different urls between express app and hubot.
As references, there is a experimental package project hubot-express shows that hubot as a express app startup. you can try to refer to the code https://github.com/hubot-scripts/hubot-express/blob/master/src/hubot-express.coffee to implement the integration.
The key code: robot.express = app = express();
And the article "Automation and Monitoring with Hubot" show the code that how to serving http requests, please move to https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-serving-http-requests to review it.
The key code: robot.router.post('/hubot/notify/:room', function(req, res) {...});
To add to this, in the end I moved to botkit library that provides way easier and integrated way to have both the server and the actual app.

What is the best way to consume a web service in Grails?

I know there are a few web service plug-ins for Grails, some of them look like they aren't maintained. I have a jar with all the stubs generated from a wsdl and now I need to start integrating. Which plugin would serve best for this? Also, the web service uses SOAP, not REST.
I know your question asked about a plugin for consuming, but I've never used one of the Grails-WS plugins, so I can't comment there. Instead, if your stubs are compatible with JAXB marshalling you can use the Spring Web Services project. You'll just have to add a dependency in BuildConfig.groovy to import the appropriate jars.
http://static.springsource.org/spring-ws/sites/2.0/reference/html/client.html
So you just define some JAXB marshaller/unmarshaller beans and web service handlers. You can get as detailed as you want with this from the documentation above and define timeouts and security if you specify your own connection handler or interceptors.
myJaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) {
classesToBeBound = ['my.class.Class1','my.class.Class2']
}
myWebServiceMessageFactory(org.springframework.ws.soap.saaj.SaajSoapMessageFactory)
myWebServiceTemplate(org.springframework.ws.client.core.WebServiceTemplate, ref('myWebServiceMessageFactory')) {
marshaller = ref('myJaxb2Marshaller')
unmarshaller = ref('myJaxb2Marshaller')
}
At that point you can use Grail's dependency injection to use the WebServiceTemplate in your Grails code:
class myService {
def myWebServiceTemplate
void myMethod {
...
Class1 myRequestObject = new Class1(data:myData)
Class2 myResponseObject = myWebServiceTemplate.marshalSendAndReceive(mySoapEndpoint, myRequestObject)
...
}
}
I would recommend using Spring's way.
You're developing in grails after all, so maybie you don't need a plugin.
Read Spring docs on Remoting and webservices, chapter 19.5.2 "Accessing web services using JAX-RPC".
No need for any of your jar's stubs. Spring will generate everything for you...
P.S. : I assume you know how to declare spring beans in grails...