Skip to content

Commit cb0bccb

Browse files
committed
add utility for bb experiment
0 parents  commit cb0bccb

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bb-exp-util/target/

bb-exp-util/pom.xml

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.evomaster.util</groupId>
8+
<artifactId>bb-exp-util</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<java.version>1.8</java.version>
13+
<swagger.parser-v3.version>2.0.27</swagger.parser-v3.version>
14+
<kotlin.version>1.5.32</kotlin.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>io.swagger.parser.v3</groupId>
20+
<artifactId>swagger-parser</artifactId>
21+
<version>${swagger.parser-v3.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.jetbrains.kotlin</groupId>
25+
<artifactId>kotlin-stdlib-jdk8</artifactId>
26+
<version>${kotlin.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.jetbrains.kotlin</groupId>
30+
<artifactId>kotlin-test</artifactId>
31+
<version>${kotlin.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
37+
<build>
38+
<plugins>
39+
40+
<plugin>
41+
<artifactId>kotlin-maven-plugin</artifactId>
42+
<groupId>org.jetbrains.kotlin</groupId>
43+
<version>${kotlin.version}</version>
44+
<configuration>
45+
<jvmTarget>${java.version}</jvmTarget>
46+
</configuration>
47+
<executions>
48+
<execution>
49+
<id>compile</id>
50+
<goals>
51+
<goal>compile</goal>
52+
</goals>
53+
<configuration>
54+
<sourceDirs>
55+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
56+
<sourceDir>${project.basedir}/src/main/java</sourceDir>
57+
<sourceDir>${project.basedir}/target/generated-sources/antlr4</sourceDir>
58+
</sourceDirs>
59+
</configuration>
60+
</execution>
61+
<execution>
62+
<id>test-compile</id>
63+
<goals>
64+
<goal>test-compile</goal>
65+
</goals>
66+
<configuration>
67+
<sourceDirs>
68+
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
69+
<sourceDir>${project.basedir}/src/test/java</sourceDir>
70+
</sourceDirs>
71+
</configuration>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
76+
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-assembly-plugin</artifactId>
80+
<version>3.3.0</version>
81+
<executions>
82+
<execution>
83+
<id>make-assembly</id>
84+
<phase>package</phase>
85+
<goals>
86+
<goal>single</goal>
87+
</goals>
88+
<configuration>
89+
<archive>
90+
<manifest>
91+
<mainClass>org.evomaster.json2yaml.Main</mainClass>
92+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
93+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
94+
</manifest>
95+
</archive>
96+
<finalName>bb-exp-util</finalName>
97+
<appendAssemblyId>false</appendAssemblyId>
98+
<descriptorRefs>
99+
<descriptorRef>jar-with-dependencies</descriptorRef>
100+
</descriptorRefs>
101+
</configuration>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
106+
</plugins>
107+
</build>
108+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package org.evomaster.json2yaml
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.fasterxml.jackson.databind.node.ArrayNode
5+
import com.fasterxml.jackson.databind.node.ObjectNode
6+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
7+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
8+
import io.swagger.v3.core.util.Yaml
9+
import io.swagger.v3.parser.OpenAPIV3Parser
10+
import java.io.File
11+
import java.net.URL
12+
import java.nio.file.Files
13+
import java.nio.file.Paths
14+
import kotlin.io.path.name
15+
16+
17+
class Main {
18+
19+
20+
companion object {
21+
22+
private val TESTCONFIG_YAML_MAPPER = ObjectMapper(YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES))
23+
private val JSON_MAPPER = ObjectMapper()
24+
25+
private val YAML_MAPPER = Yaml.mapper()
26+
private val YAML_WRITER = Yaml.pretty()
27+
28+
@JvmStatic
29+
fun main(args: Array<String>) {
30+
31+
if (args.isEmpty())
32+
throw IllegalArgumentException("args must be specified")
33+
34+
when (args[0]) {
35+
"authForResTest" -> {
36+
if (args.size != 4)
37+
throw IllegalArgumentException("to configure auth for ResTest, the args should be 4, ie., `authForResTest <testConfig.yaml path> <key> <value>`")
38+
configureAuthForResTest(args[1], args[2], args[3])
39+
}
40+
"jsonToYaml" -> {
41+
if (args.size != 2)
42+
throw IllegalArgumentException("to convert json to yaml, the args should be 2, ie., `jsonToYaml <openapi path>`")
43+
jsonToYaml(args[1])
44+
}
45+
"updateURLAndPort" -> {
46+
if (args.size != 3)
47+
throw IllegalArgumentException("to update url and port, the args should be 3, ie., `updateURLAndPort <openapi path> <port>`")
48+
try {
49+
args[2].toInt()
50+
}catch (ex: NumberFormatException){
51+
throw IllegalArgumentException("port must be a int number")
52+
}
53+
updateURLAndPort(args[1], args[2])
54+
}
55+
else ->{
56+
throw IllegalArgumentException("only able to configure `authForResTest`, `jsonToYaml` and `updateURLAndPort`")
57+
}
58+
}
59+
}
60+
61+
fun updateURLAndPort(openapiFile: String, port: String, saveFile: String = openapiFile){
62+
val path = Paths.get(openapiFile)
63+
if (!Files.exists(path)) throw IllegalArgumentException("File cannot found")
64+
65+
val mapper = if (openapiFile.endsWith(".json")) JSON_MAPPER else YAML_MAPPER
66+
val openApi = mapper.readTree(path.toFile()) as ObjectNode
67+
val writer = if (openapiFile.endsWith(".json")) mapper.writer() else YAML_WRITER
68+
69+
if (openApi.has("swagger")){
70+
openApi.put("host", "localhost:$port")
71+
if (!openApi.has("schemes")){
72+
openApi.putArray("schemes").add("http")
73+
}
74+
}else if (openApi.has("openapi")){
75+
76+
if (openApi.has("servers") && openApi.get("servers").isArray && !openApi.get("servers").isEmpty){
77+
val all = (openApi.get("servers") as ArrayNode).map { o->
78+
val resourcePath = getResourcePath(o.get("url").asText())?:""
79+
mapper.createObjectNode().apply {
80+
put("url","http://localhost:$port$resourcePath")
81+
}
82+
}
83+
openApi.putArray("servers").addAll(all)
84+
85+
}else{
86+
val urlNode = mapper.createObjectNode()
87+
urlNode.put("url", "http://localhost:$port")
88+
openApi.putArray("servers").add(urlNode)
89+
}
90+
}
91+
writer.writeValue(Paths.get(saveFile).toFile(), openApi)
92+
}
93+
94+
private fun getResourcePath(path: String): String?{
95+
try {
96+
val url = URL(path)
97+
return url.file
98+
}catch (e: Exception){
99+
return null
100+
}
101+
102+
}
103+
104+
fun configureAuthForResTest(file: String, headerKey: String, headerValue : String, saveFile: String = file){
105+
val path = Paths.get(file)
106+
if (!Files.exists(path)) throw IllegalArgumentException("File cannot found")
107+
val root = TESTCONFIG_YAML_MAPPER.readTree(path.toFile()) as ObjectNode
108+
109+
val authNode = TESTCONFIG_YAML_MAPPER.createObjectNode()
110+
authNode.put(headerKey,headerValue)
111+
112+
113+
(root.get("auth") as ObjectNode).put("headerParams", authNode)
114+
115+
TESTCONFIG_YAML_MAPPER.writer().writeValue(Paths.get(saveFile).toFile(), root)
116+
}
117+
118+
fun jsonToYaml(jsonFile: String, saveDir: String? = null){
119+
val path = Paths.get(jsonFile)
120+
if (!Files.exists(path)) throw IllegalArgumentException("File cannot found")
121+
122+
try {
123+
val openApi = OpenAPIV3Parser().read(jsonFile)
124+
val yaml = Yaml.pretty().writeValueAsString(openApi)
125+
val dir = (saveDir?:path.parent.toAbsolutePath().toString()).run {
126+
if (!endsWith(File.separator))
127+
"${this}${File.separator}"
128+
else
129+
this
130+
}
131+
132+
val output = path.fileName.name.replace(".json", ".yaml")
133+
Files.write(Paths.get("$dir$output"), yaml.toByteArray())
134+
} catch (e: Exception) {
135+
e.printStackTrace()
136+
}
137+
}
138+
139+
}
140+
141+
}

0 commit comments

Comments
 (0)