Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kotlin] add testFolder configuration for kotlin #2975

Merged
merged 12 commits into from
May 26, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down Expand Up @@ -208,12 +209,17 @@ public AbstractKotlinCodegen() {

@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
Expand Down Expand Up @@ -410,6 +416,10 @@ public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}

public void setTestFolder(String testFolder) {
this.testFolder = testFolder;
}

public Boolean getParcelizeModels() {
return parcelizeModels;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please add an empty newline here ?

Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

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.*;

Expand Down Expand Up @@ -136,4 +139,21 @@ public void convertApiNameWithSuffix() {
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));
}


}
6 changes: 6 additions & 0 deletions samples/server/petstore/kotlin-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -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 &lt; 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 &lt;&#x3D; 5 or &gt; 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
}

}
Loading