Skip to content

Latest commit

 

History

History
 
 

swagger-codegen-jmeter

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

This section describes how to generate everything JMeter needs to emulate a lot of clients calling a REST API service defined in Swagger, beginning with the example at http://petstore.swagger.io

Bear in mind that we are not generating server code to respond to request from clients. That can be quite complicated, with calls to databases, etc.

  1. Use swagger-codegen to create server program.

  2. Use swagger-codegen to create Java client program.

  3. Create JMeter resources for the sample API, manually.

  4. Edit the template to produce basic JMeter code.

  5. Edit the template to address more risks.

  6. Make custom JMeter generator web service.

  7. Make generator available from a website.

  8. Refine generator code.

  9. Run several at a time.


### Use swagger-codegen to generate Java client program.
  1. Open http://editor.swagger.io/#.

  2. Click "Got it" if it's your first time at the site.

  3. Copy to your invisible clipboard a Swagger-spec JSON file for your project or the sample file at http://petstore.swagger.io/v2/swagger.json.

  4. Click on the text page at the left of the swagger editor.

  5. Select Generate Client, then Java to follow this tutorial (or JMeter when it's ready). The file should be downloaded to your browser Downloads folder.

  6. Use the Mac Finder or Windows Explorer to locate the download.

  7. Unzip to produce the java-client folder.

  8. Copy the folder to your workspace folder.

  9. Open the folder to examine the files.

build.gradle
gradle.properties
pom.xml
settings.gradle
src
  1. Double-click to drill into the src folder: src/main/java/io/swagger/client

Notice that ASTW, the source code is not internationalized. English text is embedded into the programming source text rather than references to translations in a resource file.

  1. Install Gradle if you haven't already. On a Mac:
brew update
brew install gradle
gradle --help
gradle tasks

More info on Gradle:

  1. use gradle (instead of ant, maven) to build the client project run-time binary file referencing the build.gradle file (containing Groovy program) declarative code.
gradle build
  1. A build folder should now appear.
  2. Just to look, cd into the build/libs folder which contains file swagger-java-client-1.0.0.jar.
  3. Because a manifest is not generated:
jar cmvf META-INF/MANIFEST.MF <new-jar-filename>.jar <files to include>
  1. Use tab key to auto-complete command to execute the client program generated:
java -jar build/libs/swagger-java-client-1.0.0.jar

NOTE: "swagger-java-client" was specified in the settings.gradle file rootProject.name property value.

  1. If you don't want temp and the other files generated:
gradle clean
### 2). Create JMeter resources for the sample API, manually.

The example script is from ___

The JMeter script folder implements the tests below to address potential risks.

### 3). Edit the template to produce basic JMeter code. The sample clients are generated from a **template**.
  1. Define a folder to hold git repos locally, such as ~/gits.
mkdir gits
  1. Optionally, define a folder for a git repo user.
mkdir swagger-api
  1. At the user folder, download one of its git repos from the internet.
git clone https://github.com/swagger-api/swagger-codegen.git
  1. Press and type cd then tab to autocomplete swagger-codegen. Press Enter.
  2. cd modules/swagger-codegen/src/main/resources
  3. cd Java (with the capital J)

The run-time for JMeter is run like other languages generated:

./bin/android-petstore.sh
./bin/java-petstore.sh
./bin/objc-petstore.sh
./bin/jmeter-petstore.sh

QUESTION: Is there a tutorial on this?

### 4). Edit the template to address more risks.

1). Valid atomic calls with bad response codes.

2). Happy paths: Valid atomic calls with valid response codes.

3). Atomic run types.

4). Invalid field values.

5). Cross-field edits.

Videos


http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api

Examples of REST API usage:

4.1). Recognize and handle problem HTTP response codes

  1. 400 (not found)
  2. 500 (server issues)
  3. etc.
### 4.2). Clean happy atomic paths Valid atomic calls with valid response codes. Notice the object "tickets" is a plural noun. Other nouns include users, groups, people, etc.
  1. POST /tickets - creates new item (user registration)

    POST resulting in a creation should return a HTTP 201 status code and include a Location header that points to the URL of the new resource.

  2. GET /tickets - retrieve a list

  3. GET /tickets/12 - Retrieves a specific ticket

  4. PUT /tickets/12 - Updates ticket #12

  5. PATCH /tickets/12/activate - Partially updates ticket #12, such as activate or deactivate.

  6. DELETE /tickets/12 - Deletes ticket 12. This keeps database empty.

### 4.3). Relations Hierarchy:
  1. POST /tickets/12/messages - Creates a new message in ticket #12
  2. GET /tickets/12/messages - Retrieves list of messages for ticket #12
  3. GET /tickets/12/messages/5 - Retrieves message #5 for ticket #12
  4. PUT /tickets/12/messages/5 - Updates message #5 for ticket #12
  5. PATCH /tickets/12/messages/5 - Partially updates message #5 for ticket #12
  6. DELETE /tickets/12/messages/5 - Deletes message #5 for ticket #12
### 4.4). Filtering Each attribute
  1. GET /tickets - Retrieves an un-filtered list.
  2. GET /tickets?state=open - Retrieves a filtered list of only open tickets.
  3. GET /tickets?state=closed - Retrieves a filtered list of only closed tickets.

1 = 2 + 3 ?

### 4.5). "run-type" parameter defines repeating processing strategies

3.1) Repeat POST new to populate the database and identify how many can register all at once.

3.2) Repeat GETs to identify cache hits and impact of caching.

3.3) Repeat PUTs

3.4) Mix, to measure impact of database replication (log shipping).

### 4.6). Invalid field values There are different invalid values for each data type.
  • Currency number has negative
  • Phone number has too few numbers
  • Phone number has wrong area code
  • Zip code has text characters (as UK Postal code)
  • Credit card number has invalid checksum
### 4.7) Cross-field edits There are different invalid values for each data type.
  • Zip code out of state specified
  • Content-Type header doesn’t match the body

## 5). Make custom JMeter generator web service

http://www.lynda.com/JavaScript-tutorials/JavaScript-JSON/114901-2.html 1h 12m video course

  1. Test
  2. Pull request to incorporate into swagger-codegen.
## 6). Make generator available from a website.

Once done, create a UI front-end online generator web page like https://generator.swagger.io

swagger online-generator ## 7). Refine generator code. ## 8). Run several at a time.

This is covered in the Run variations page.

This approach would require interaction with a web service interface to Swagger.

  1. Open a command-line terminal window.

  2. In a browser, obtain a list of languages that the Swagger client generation web service can create.

https://generator.swagger.io/api/gen/clients

The response:

["android","async-scala","csharp","dart","flash","java","objc","perl","php","python","qt5cpp","ruby","scala","dynamic-html","html","swagger","swagger-yaml","swift","tizen","typescript-angular","typescript-node","akka-scala","CsharpDotNet2"]

  1. Copy a web service (such as "java") to your clipboard and paste into a terminal command line, but don't execute yet.
curl -X POST -H "content-type:application/json" \
-d '{"swaggerUrl":"http://petstore.swagger.io/v2/swagger.json"}' \
https://generator.swagger.io/api/gen/clients/java
  1. If you want to generate for a different language than java, backspace to change the language specification (instead of "java" in the sample request above), then press Enter to execute.

  2. In the JSON response is a link to a zip file containing the ruby client specified, like this one:

{"code":"1446559952387","link":"https://generator.swagger.io/api/gen/download/1446559952387"}
  1. Copy the link returned and paste it in the address of an internet browser:
https://generator.swagger.io/api/gen/download/1446559952387
## API design tutorials

http://www.lynda.com/API-tutorials/Effective-Design-RESTful-APIs/166777-2.html 44m Lynda video course by Keith Casey

http://www.lynda.com/API-tutorials/Up-Running-Cloud-Service-APIs/151707-2.html 1h 25m Lynda video course by Joseph Lowery

https://www.youtube.com/watch?v=UuxKpmIrK4w API Testing and Debugging @ APIStrat SF

https://www.youtube.com/watch?v=heh4OeB9A-c How to Design a good API and Why It Matters (Google TechTalk Jan. 24, 2007 Joshua Bloch)

"APIs live forever. Bad APIs create an un-ending stream of support calls."

"inter-modular boundaries are APIs".

https://www.youtube.com/watch?v=BtAYZ3pHg4c Developer Friendly API Designs Nov 16, 2011 by Ferenc Milhaly

https://www.youtube.com/watch?v=QlQm786MClE Secrets of Awesome API Design

https://www.youtube.com/watch?v=mZ8_QgJ5mbs Beautiful REST & JSON APIs

https://www.youtube.com/watch?v=hdSrT4yjS1g REST+JSON API Design - Best Practices for Developers by Stormpath

https://www.youtube.com/watch?v=qCdpTji8nxo How to create great APIs (at Facebook Developer Day 2013)

http://www.lynda.com/Developer-Programming-Languages-tutorials/Building-Web-Services-Java-EE/149837-2.html