mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-06 10:35:25 +00:00
[kotlin] add testFolder configuration for kotlin (#2975)
* add apiSuffix configuration * refactor the global option apiSuffix to kotlin specific feature * add testcase * add testFolder for kotlin gen * add basic api_test.mustache template * add api_test,mustache * add tests * improve usage of file separators * replace defined file separator into os specific separators * improve test templates and kotlin-springboot sample * add test dependecies into gradle template * add newline into template file
This commit is contained in:
parent
2e6b911022
commit
7109f5fb3b
@ -44,6 +44,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
protected String apiSuffix = "Api";
|
||||
|
||||
protected String sourceFolder = "src/main/kotlin";
|
||||
protected String testFolder = "src/test/kotlin";
|
||||
|
||||
protected String apiDocPath = "docs/";
|
||||
protected String modelDocPath = "docs/";
|
||||
@ -208,12 +209,17 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
|
||||
@Override
|
||||
public String apiDocFileFolder() {
|
||||
return (outputFolder + "/" + apiDocPath).replace('/', File.separatorChar);
|
||||
return (outputFolder + File.separator + apiDocPath).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar);
|
||||
return (outputFolder + File.separator + sourceFolder + File.separator + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiTestFileFolder() {
|
||||
return (outputFolder + File.separator + testFolder + File.separator + apiPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar) ;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -410,6 +416,10 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
|
||||
this.sourceFolder = sourceFolder;
|
||||
}
|
||||
|
||||
public void setTestFolder(String testFolder) {
|
||||
this.testFolder = testFolder;
|
||||
}
|
||||
|
||||
public Boolean getParcelizeModels() {
|
||||
return parcelizeModels;
|
||||
}
|
||||
|
@ -76,10 +76,11 @@ public class KotlinSpringServerCodegen extends AbstractKotlinCodegen
|
||||
public KotlinSpringServerCodegen() {
|
||||
super();
|
||||
|
||||
apiTestTemplateFiles.put("api_test.mustache", ".kt");
|
||||
|
||||
reservedWords.addAll(VARIABLE_RESERVED_WORDS);
|
||||
|
||||
outputFolder = "generated-code/kotlin-spring";
|
||||
apiTestTemplateFiles.clear(); // TODO: add test template
|
||||
embeddedTemplateDir = templateDir = "kotlin-spring";
|
||||
|
||||
artifactId = "openapi-spring";
|
||||
|
33
modules/openapi-generator/src/main/resources/kotlin-spring/api_test.mustache
vendored
Normal file
33
modules/openapi-generator/src/main/resources/kotlin-spring/api_test.mustache
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
package {{package}}
|
||||
|
||||
{{#imports}}import {{import}}
|
||||
{{/imports}}
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class {{classname}}Test {
|
||||
|
||||
{{#serviceInterface}}private val service: {{classname}}Service = {{classname}}ServiceImpl(){{/serviceInterface}}
|
||||
private val api: {{classname}}Controller = {{classname}}Controller({{#serviceInterface}}service{{/serviceInterface}})
|
||||
|
||||
{{#operations}}{{#operation}}
|
||||
/**
|
||||
* {{summary}}
|
||||
*
|
||||
* {{notes}}
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun {{operationId}}Test() {
|
||||
{{#allParams}}
|
||||
val {{paramName}}:{{{dataType}}}? = null
|
||||
{{/allParams}}
|
||||
val response: ResponseEntity<{{>returnTypes}}> = api.{{operationId}}({{#allParams}}{{paramName}}!!{{#hasMore}}, {{/hasMore}}{{/allParams}})
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
{{/operation}}{{/operations}}
|
||||
}
|
@ -42,6 +42,7 @@ dependencies {
|
||||
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
|
||||
compile("com.fasterxml.jackson.module:jackson-module-kotlin")
|
||||
|
||||
testCompile("org.jetbrains.kotlin:kotlin-test-junit5")
|
||||
testCompile("org.springframework.boot:spring-boot-starter-test") {
|
||||
exclude(module = "junit")
|
||||
}
|
||||
|
@ -109,5 +109,11 @@
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
{{/useBeanValidation}}
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-junit5</artifactId>
|
||||
<version>1.3.31</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -2,8 +2,11 @@ package org.openapitools.codegen.kotlin;
|
||||
|
||||
import org.openapitools.codegen.CodegenType;
|
||||
import org.openapitools.codegen.languages.AbstractKotlinCodegen;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.openapitools.codegen.CodegenConstants.ENUM_PROPERTY_NAMING_TYPE.*;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
@ -136,4 +139,21 @@ public class AbstractKotlinCodegenTest {
|
||||
assertEquals(codegen.toApiName(""), "DefaultApi");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apIFileFolder() {
|
||||
codegen.setOutputDir("/User/open/api/tools");
|
||||
codegen.setSourceFolder("src/folder");
|
||||
codegen.setApiPackage("org.openapitools.codegen.api");
|
||||
Assert.assertEquals(codegen.apiFileFolder(), "/User/open/api/tools/src/folder/org/openapitools/codegen/api".replace('/', File.separatorChar));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apiTestFileFolder() {
|
||||
codegen.setOutputDir("/User/open/api/tools");
|
||||
codegen.setTestFolder("test/folder");
|
||||
codegen.setApiPackage("org.openapitools.codegen.api");
|
||||
Assert.assertEquals(codegen.apiTestFileFolder(), "/User/open/api/tools/test/folder/org/openapitools/codegen/api".replace('/', File.separatorChar));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -40,6 +40,7 @@ dependencies {
|
||||
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
|
||||
compile("com.fasterxml.jackson.module:jackson-module-kotlin")
|
||||
|
||||
testCompile("org.jetbrains.kotlin:kotlin-test-junit5")
|
||||
testCompile("org.springframework.boot:spring-boot-starter-test") {
|
||||
exclude(module = "junit")
|
||||
}
|
||||
|
@ -105,5 +105,11 @@
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-junit5</artifactId>
|
||||
<version>1.3.31</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,148 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.ModelApiResponse
|
||||
import org.openapitools.model.Pet
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class PetApiTest {
|
||||
|
||||
private val service: PetApiService = PetApiServiceImpl()
|
||||
private val api: PetApiController = PetApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Add a new pet to the store
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun addPetTest() {
|
||||
val body:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.addPet(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deletePetTest() {
|
||||
val petId:Long? = null
|
||||
val apiKey:String? = null
|
||||
val response: ResponseEntity<Unit> = api.deletePet(petId!!, apiKey!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by status
|
||||
*
|
||||
* Multiple status values can be provided with comma separated strings
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByStatusTest() {
|
||||
val status:List<String>? = null
|
||||
val response: ResponseEntity<List<Pet>> = api.findPetsByStatus(status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds Pets by tags
|
||||
*
|
||||
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun findPetsByTagsTest() {
|
||||
val tags:List<String>? = null
|
||||
val response: ResponseEntity<List<Pet>> = api.findPetsByTags(tags!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find pet by ID
|
||||
*
|
||||
* Returns a single pet
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getPetByIdTest() {
|
||||
val petId:Long? = null
|
||||
val response: ResponseEntity<Pet> = api.getPetById(petId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetTest() {
|
||||
val body:Pet? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePet(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a pet in the store with form data
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updatePetWithFormTest() {
|
||||
val petId:Long? = null
|
||||
val name:String? = null
|
||||
val status:String? = null
|
||||
val response: ResponseEntity<Unit> = api.updatePetWithForm(petId!!, name!!, status!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* uploads an image
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun uploadFileTest() {
|
||||
val petId:Long? = null
|
||||
val additionalMetadata:String? = null
|
||||
val file:org.springframework.core.io.Resource? = null
|
||||
val response: ResponseEntity<ModelApiResponse> = api.uploadFile(petId!!, additionalMetadata!!, file!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.Order
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class StoreApiTest {
|
||||
|
||||
private val service: StoreApiService = StoreApiServiceImpl()
|
||||
private val api: StoreApiController = StoreApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Delete purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteOrderTest() {
|
||||
val orderId:String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteOrder(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns pet inventories by status
|
||||
*
|
||||
* Returns a map of status codes to quantities
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getInventoryTest() {
|
||||
val response: ResponseEntity<Map<String, Int>> = api.getInventory()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Find purchase order by ID
|
||||
*
|
||||
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getOrderByIdTest() {
|
||||
val orderId:Long? = null
|
||||
val response: ResponseEntity<Order> = api.getOrderById(orderId!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Place an order for a pet
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun placeOrderTest() {
|
||||
val body:Order? = null
|
||||
val response: ResponseEntity<Order> = api.placeOrder(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,143 @@
|
||||
package org.openapitools.api
|
||||
|
||||
import org.openapitools.model.User
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
import org.springframework.http.ResponseEntity
|
||||
|
||||
class UserApiTest {
|
||||
|
||||
private val service: UserApiService = UserApiServiceImpl()
|
||||
private val api: UserApiController = UserApiController(service)
|
||||
|
||||
|
||||
/**
|
||||
* Create user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUserTest() {
|
||||
val body:User? = null
|
||||
val response: ResponseEntity<Unit> = api.createUser(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithArrayInputTest() {
|
||||
val body:List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithArrayInput(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of users with given input array
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun createUsersWithListInputTest() {
|
||||
val body:List<User>? = null
|
||||
val response: ResponseEntity<Unit> = api.createUsersWithListInput(body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun deleteUserTest() {
|
||||
val username:String? = null
|
||||
val response: ResponseEntity<Unit> = api.deleteUser(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user by user name
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun getUserByNameTest() {
|
||||
val username:String? = null
|
||||
val response: ResponseEntity<User> = api.getUserByName(username!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs user into the system
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun loginUserTest() {
|
||||
val username:String? = null
|
||||
val password:String? = null
|
||||
val response: ResponseEntity<String> = api.loginUser(username!!, password!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs out current logged in user session
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun logoutUserTest() {
|
||||
val response: ResponseEntity<Unit> = api.logoutUser()
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
/**
|
||||
* Updated user
|
||||
*
|
||||
* This can only be done by the logged in user.
|
||||
*
|
||||
* @throws ApiException
|
||||
* if the Api call fails
|
||||
*/
|
||||
@Test
|
||||
fun updateUserTest() {
|
||||
val username:String? = null
|
||||
val body:User? = null
|
||||
val response: ResponseEntity<Unit> = api.updateUser(username!!, body!!)
|
||||
|
||||
// TODO: test validations
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user