-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(cts): add common client test (#646)
- Loading branch information
Showing
39 changed files
with
361 additions
and
632 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ indent_size = 2 | |
|
||
[*.{mustache,ts,yml}] | ||
charset = utf-8 | ||
|
||
[*.mustache] | ||
insert_final_newline = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
generators/src/main/java/com/algolia/codegen/cts/EscapeSlashLambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.algolia.codegen.cts; | ||
|
||
import com.samskivert.mustache.Mustache; | ||
import com.samskivert.mustache.Template; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
|
||
public class EscapeSlashLambda implements Mustache.Lambda { | ||
|
||
@Override | ||
public void execute(Template.Fragment fragment, Writer writer) throws IOException { | ||
String text = fragment.execute(); | ||
writer.write(text.replace("\\", "\\\\")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 66 additions & 4 deletions
70
generators/src/main/java/com/algolia/codegen/cts/tests/TestsGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,77 @@ | ||
package com.algolia.codegen.cts.tests; | ||
|
||
import com.algolia.codegen.Utils; | ||
import com.algolia.codegen.exceptions.CTSException; | ||
import io.swagger.v3.core.util.Json; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import org.openapitools.codegen.CodegenModel; | ||
import org.openapitools.codegen.CodegenOperation; | ||
import org.openapitools.codegen.SupportingFile; | ||
|
||
public interface TestsGenerator { | ||
public boolean available(); | ||
public abstract class TestsGenerator { | ||
|
||
public void addSupportingFiles(List<SupportingFile> supportingFiles, String outputFolder, String extension); | ||
protected final String language, client; | ||
|
||
public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation> operations, Map<String, Object> bundle) throws Exception; | ||
public TestsGenerator(String language, String client) { | ||
this.language = language; | ||
this.client = client; | ||
} | ||
|
||
public abstract boolean available(); | ||
|
||
public abstract void addSupportingFiles(List<SupportingFile> supportingFiles, String outputFolder, String extension); | ||
|
||
public abstract void run(Map<String, CodegenModel> models, Map<String, CodegenOperation> operations, Map<String, Object> bundle) | ||
throws Exception; | ||
|
||
protected <T> Map<String, T> loadCTS(String path, String clientName, Class<T> jsonType) throws Exception { | ||
if (!available()) { | ||
throw new CTSException("Templates not found for " + path, true); | ||
} | ||
|
||
File dir = new File("tests/CTS/" + path + "/" + clientName); | ||
File commonTestDir = new File("tests/CTS/" + path + "/common"); | ||
if (!dir.exists()) { | ||
throw new CTSException("CTS not found at " + dir.getAbsolutePath(), true); | ||
} | ||
if (!commonTestDir.exists()) { | ||
throw new CTSException("CTS not found at " + commonTestDir.getAbsolutePath(), true); | ||
} | ||
List<File> allTests = new ArrayList<>(); | ||
Collections.addAll(allTests, dir.listFiles()); | ||
Collections.addAll(allTests, commonTestDir.listFiles()); | ||
|
||
Map<String, T> cts = new TreeMap<>(); | ||
|
||
for (File f : allTests) { | ||
String json = new String(Files.readAllBytes(Paths.get(f.getAbsolutePath()))); | ||
json = injectVariables(json); | ||
cts.put(f.getName().replace(".json", ""), Json.mapper().readValue(json, jsonType)); | ||
} | ||
return cts; | ||
} | ||
|
||
private String languageCased() { | ||
switch (language) { | ||
case "java": | ||
return "Java"; | ||
case "javascript": | ||
return "JavaScript"; | ||
case "php": | ||
return "PHP"; | ||
default: | ||
return language; | ||
} | ||
} | ||
|
||
private String injectVariables(String json) { | ||
return json.replace("${{languageCased}}", languageCased()).replace("${{clientPascalCase}}", Utils.capitalize(Utils.camelize(client))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.