Skip to content
Permalink
Browse files

add Artificial REST API generator for resource dependency mio experim…

…ents

further generated REST APIs will be organized under 'evomaster-resource-experiments' folder
  • Loading branch information...
ZhangMan1126 committed Aug 30, 2019
1 parent aa5f593 commit abadcea66db12ad08cbdfcdfd7ce6f02ebff8e00
Showing with 2,943 additions and 0 deletions.
  1. +1 −0 pom.xml
  2. +81 −0 resource-rest-experiments/api-generator/pom.xml
  3. +20 −0 ...est-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/FormatUtil.kt
  4. +73 −0 ...rest-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/GenConfig.kt
  5. +115 −0 ...t-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/GenerateREST.kt
  6. +42 −0 ...ator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/AppliedJavaType.kt
  7. +68 −0 ...-generator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaClass.kt
  8. +32 −0 ...ator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaDeclaration.kt
  9. +22 −0 ...tor/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaGetterMethod.kt
  10. +18 −0 ...generator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaMethod.kt
  11. +20 −0 ...tor/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaSetterMethod.kt
  12. +51 −0 ...nerator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaTemplate.kt
  13. +73 −0 ...ator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/JavaxAnnotation.kt
  14. +116 −0 ...tor/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/SpringAnnotation.kt
  15. +70 −0 ...erator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/SpringRestAPI.kt
  16. +42 −0 ...or/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/SwaggerAnnotation.kt
  17. +27 −0 ...or/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/app/ApiInfoMethod.kt
  18. +50 −0 ...enerator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/app/JavaApp.kt
  19. +31 −0 ...rc/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/app/JavaDockApiMethod.kt
  20. +28 −0 ...rator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/app/MainMethod.kt
  21. +20 −0 ...c/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/dto/JavaDeclarationDto.kt
  22. +36 −0 ...enerator/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/dto/JavaDto.kt
  23. +29 −0 .../kotlin/org/evomaster/resource/rest/generator/implementation/java/entity/JavaDeclarationEntity.kt
  24. +65 −0 ...src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/entity/JavaE2DMethod.kt
  25. +59 −0 ...or/src/main/kotlin/org/evomaster/resource/rest/generator/implementation/java/entity/JavaEntity.kt
  26. +45 −0 ...n/kotlin/org/evomaster/resource/rest/generator/implementation/java/entity/JavaEntityRepository.kt
  27. +15 −0 ...in/org/evomaster/resource/rest/generator/implementation/java/service/JavaDeclarationRepository.kt
  28. +66 −0 .../main/kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaResourceAPI.kt
  29. +42 −0 .../kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestDeleteMethod.kt
  30. +44 −0 ...kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestGetByIdMethod.kt
  31. +37 −0 .../org/evomaster/resource/rest/generator/implementation/java/service/JavaRestGetCollectionMethod.kt
  32. +82 −0 ...n/kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestPatchMethod.kt
  33. +64 −0 ...lin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestPatchValueMethod.kt
  34. +79 −0 ...in/kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestPostMethod.kt
  35. +84 −0 ...ain/kotlin/org/evomaster/resource/rest/generator/implementation/java/service/JavaRestPutMethod.kt
  36. +78 −0 ...s/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ClazzSpecification.kt
  37. +20 −0 ...eriments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/CommonTypes.kt
  38. +7 −0 ...ator/src/main/kotlin/org/evomaster/resource/rest/generator/model/EdgeMultiplicitySpecification.kt
  39. +6 −0 .../api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/MethodSpecification.kt
  40. +41 −0 ...pi-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/PropertySpecification.kt
  41. +9 −0 ...ts/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/RegisterMediaType.kt
  42. +12 −0 ...api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/RelationMultiplicity.kt
  43. +6 −0 ...-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ResEdge.kt
  44. +208 −0 .../api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ResGenSpecification.kt
  45. +39 −0 ...-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ResNode.kt
  46. +191 −0 ...iments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ResourceGraph.kt
  47. +10 −0 ...api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/ResourceRelationType.kt
  48. +14 −0 ...periments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/model/RestMethod.kt
  49. +116 −0 ...ents/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/pom/DependencyManager.kt
  50. +93 −0 ...st-experiments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/pom/POModel.kt
  51. +16 −0 ...ments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/AbstractTag.kt
  52. +11 −0 ...eriments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/Boundary.kt
  53. +123 −0 ...nts/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/ClassTemplate.kt
  54. +10 −0 ...riments/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/ClassType.kt
  55. +17 −0 ...pi-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/ConstantTypeScript.kt
  56. +19 −0 ...api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/DeclarationScript.kt
  57. +17 −0 ...nts/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/GeneralSymbol.kt
  58. +59 −0 ...ents/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/MethodScript.kt
  59. +39 −0 ...ents/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/RegisterType.kt
  60. +16 −0 ...ts/api-generator/src/main/kotlin/org/evomaster/resource/rest/generator/template/ScriptTemplate.kt
  61. +19 −0 resource-rest-experiments/pom.xml
@@ -24,6 +24,7 @@
<module>e2e-tests</module>
<module>experiments</module>
<module>report</module>
<module>resource-rest-experiments</module>
</modules>

<issueManagement>
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>evomaster-resource-rest-experiments</artifactId>
<groupId>org.evomaster</groupId>
<version>0.3.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>resource-rest-experiments-api-generator</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
</dependency>

<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>1.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.xml.parsers/jaxp-api -->
<dependency>
<groupId>javax.xml.parsers</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4.5</version>
</dependency>

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-io</artifactId>
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-ext</artifactId>
<version>1.3.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,20 @@
package org.evomaster.resource.rest.generator

/**
* created by manzh on 2019-08-14
*/
object FormatUtil {

fun upperFirst(text: String) = upper(text, 0)

fun lowerFirst(text: String) = lower(text, 0)


private fun upper(text : String, index : Int) = text.replaceRange(index, index+1, text[index].toUpperCase().toString())

private fun lower(text : String, index : Int) = text.replaceRange(index, index+1, text[index].toLowerCase().toString())

fun formatFolder(path : String) = System.getProperty("file.separator").run { if (path.endsWith(this)) path else "$path$this" }

}

@@ -0,0 +1,73 @@
package org.evomaster.resource.rest.generator

import org.evomaster.resource.rest.generator.model.RestMethod

/**
* created by manzh on 2019-08-14
*/
class GenConfig {

/**
* output folder
*/
var outputFolder = "/Users/mazh001/Documents/GitHub/automated-generated-api/"

/**
*
*/
var outputType = OutputType.MAVEN_MODULE

enum class OutputType{
SOURCE,
MAVEN_MODULE,
MAVEN_PROJECT,
}
var outputContent = OutputContent.CS

enum class OutputContent{
EM,
CS,
BOTH
}

var projectName = "auto-rest-example"
var csName = "cs"
var emName = "em"

var srcFolder = "src/main"

var csProjectPackage = "org.evomaster.resource.rest.artificial.cs"

var emProjectPackage = "org.evomaster.resource.rest.artificial.em.controller"

var language = Format.JAVA_SPRING_SWAGGER

enum class Format(val srcFolder : String, val resource : String?){
JAVA_SPRING_SWAGGER("java", "resources")
}

var restMethods = listOf(RestMethod.POST, RestMethod.GET_ID, RestMethod.GET_ALL, RestMethod.PUT, RestMethod.DELETE, RestMethod.PATCH_VALUE, RestMethod.PATCH)

var numOfNodes = 10

var numOfOneToOne = 0

var numOfOneToTwo = 0

var numOfOneToMany = 0

var numOfTwoToOne = 0

var numOfManyToOne = 0

var numOfTwoToTwo = 0

var numOfManyToMany = 0

fun getCsOutputFolder() = "${FormatUtil.formatFolder(getCsRootFolder())}$srcFolder/${language.srcFolder}"
fun getCsResourceFolder() = "${FormatUtil.formatFolder(getCsRootFolder())}$srcFolder/${language.resource}"
private fun getRootFolder() = "${FormatUtil.formatFolder(outputFolder)}${if (outputContent == OutputContent.BOTH) "$projectName" else ""}"
fun getCsRootFolder() = "${FormatUtil.formatFolder(getRootFolder())}${if (outputType != OutputType.SOURCE) csName else ""}"
fun getEmOutputFolder() = "${FormatUtil.formatFolder(getRootFolder())}$emName/$srcFolder/${language.srcFolder}"

}
@@ -0,0 +1,115 @@
package org.evomaster.resource.rest.generator

import org.evomaster.resource.rest.generator.model.ResourceGraph
import org.evomaster.resource.rest.generator.model.RestMethod
import org.evomaster.resource.rest.generator.implementation.java.AppliedJavaType
import org.evomaster.resource.rest.generator.implementation.java.app.JavaApp
import org.evomaster.resource.rest.generator.implementation.java.dto.JavaDto
import org.evomaster.resource.rest.generator.implementation.java.entity.JavaEntity
import org.evomaster.resource.rest.generator.implementation.java.entity.JavaEntityRepository
import org.evomaster.resource.rest.generator.implementation.java.service.JavaResourceAPI
import org.evomaster.resource.rest.generator.model.AppClazz
import org.evomaster.resource.rest.generator.model.EdgeMultiplicitySpecification
import org.evomaster.resource.rest.generator.model.ResGenSpecification
import org.evomaster.resource.rest.generator.pom.CSPOModel
import org.evomaster.resource.rest.generator.template.ClassTemplate
import org.evomaster.resource.rest.generator.template.RegisterType

/**
* created by manzh on 2019-08-16
*/
class GenerateREST(val config: GenConfig) {

private val resourceCluster = mutableMapOf<String, ResGenSpecification>()

fun run(){
init()
generateAndSaveCS()
}

private fun init(){
val graph = ResourceGraph(
numOfNodes = config.numOfNodes,
multiplicity = listOf(
EdgeMultiplicitySpecification(config.numOfOneToOne, 1,1),
EdgeMultiplicitySpecification(config.numOfOneToTwo, 1,2),
EdgeMultiplicitySpecification(config.numOfOneToMany, 1,3),
EdgeMultiplicitySpecification(config.numOfTwoToOne, 2,1),
EdgeMultiplicitySpecification(config.numOfManyToOne, 3,1),
EdgeMultiplicitySpecification(config.numOfTwoToTwo, 2,2),
EdgeMultiplicitySpecification(config.numOfManyToMany, 3,3)
)
)
graph.save(config.getCsResourceFolder())
createResources(graph)
}

private fun generateAndSaveCS(){
val type = registerType()
generateAndSaveCS(JavaApp(AppClazz(
rootPackage = config.csProjectPackage,
outputFolder = config.getCsOutputFolder()
)
), type)
resourceCluster.values.forEach { generateAndSaveCS(it, type) }

if (config.outputType != GenConfig.OutputType.SOURCE)
CSPOModel(config.csName, config.csName, output = config.getCsRootFolder()).save()
}

private fun registerType() : RegisterType {
val dtos = resourceCluster.values.map { it.nameDtoClass() }.toList()

val common = when(config.language){
GenConfig.Format.JAVA_SPRING_SWAGGER -> AppliedJavaType()
}

val generic = common.getGenericTypes(dtos)
return RegisterType(common, resourceCluster.values.flatMap { it.produceTypes() }.toSet().plus(generic.values))
}


private fun generateAndSaveCS(gen : ResGenSpecification, type: RegisterType){
when(config.language){
GenConfig.Format.JAVA_SPRING_SWAGGER -> {
generateAndSaveCS(JavaDto(gen.getDto()), type)
generateAndSaveCS(JavaEntity(gen.getEntity()), type)
gen.getRepository()?.apply { generateAndSaveCS(JavaEntityRepository(this), type) }
gen.getApiService()?.apply { generateAndSaveCS(JavaResourceAPI(this), type) }
}
}
}

private fun <T> generateAndSaveCS(imp : T, type : RegisterType) where T: ClassTemplate{
imp.generateAndSave(type)
}

private fun createResources(graph : ResourceGraph){
graph.nodes.values.forEach {node->
resourceCluster.putIfAbsent(node.name, ResGenSpecification(
resNode = node,
rootPackage = config.csProjectPackage,
outputFolder = config.getCsOutputFolder(),
restMethods = config.restMethods
))
}

resourceCluster.forEach { (t, u) ->
u.initDependence(resourceCluster)
}
}
}

fun main(args : Array<String>){
val config = GenConfig()

config.outputFolder = "e2e-tests/spring-examples/"
config.outputType = GenConfig.OutputType.SOURCE
config.csProjectPackage = "com.foo.rest.examples.spring.resource"
config.numOfNodes = 3
config.numOfOneToOne = 1
config.restMethods = listOf(RestMethod.POST, RestMethod.GET_ID)

GenerateREST(config).run()
}

@@ -0,0 +1,42 @@
package org.evomaster.resource.rest.generator.implementation.java

import org.evomaster.resource.rest.generator.model.CommonTypes
import org.evomaster.resource.rest.generator.template.ConstantTypeScript

/**
* created by manzh on 2019-08-20
*/
class AppliedJavaType : ConstantTypeScript {

private val map = CommonTypes.values().map {
when(it){
CommonTypes.BOOLEAN -> Pair(it, "bool")
CommonTypes.STRING -> Pair(it, "String")
CommonTypes.OBJ_BOOLEAN -> Pair(it, "Boolean")
CommonTypes.OBJ_DOUBLE -> Pair(it, "Double")
CommonTypes.OBJ_INT -> Pair(it, "Integer")
CommonTypes.OBJ_LONG -> Pair(it, "Long")
else-> Pair(it, it.name.toLowerCase())
}
}.toMap()

override fun getCommonType(type: CommonTypes): String = map.getValue(type)

override fun getAllCommonTypes(): Map<CommonTypes, String> = map

override fun getTypes(): Map<String, String> = mapOf(
"ResponseEntity" to "ResponseEntity",
"Docket" to "Docket",
"ApiInfo" to "ApiInfo",
"String[]" to "String[]"
)

override fun getGenericTypes(list: List<String>): Map<String, String> {
val map = mutableMapOf<String, String>()
list.forEach {
map.put("ResponseEntity<List<$it>>", "ResponseEntity<List<$it>>")
map.put("ResponseEntity<$it>","ResponseEntity<$it>")
}
return map
}
}

0 comments on commit abadcea

Please sign in to comment.
You can’t perform that action at this time.