-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support rdl file without version on parsec 2.1.7 (#75)
- Loading branch information
1 parent
e3bd3cb
commit d80bf56
Showing
10 changed files
with
319 additions
and
10 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ target/ | |
tests/generated/ | ||
*.iml | ||
*.rdl | ||
!testdata/*.rdl |
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
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
108 changes: 108 additions & 0 deletions
108
cmd/rdl-gen-parsec-java-server/genServerTemplate_test.go
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,108 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/ardielle/ardielle-go/rdl" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateServerWithoutVersion(t *testing.T) { | ||
testOutputDir := getTempDir(t, ".", "testOutput-") | ||
path := testOutputDir + "/com/yahoo/shopping/parsec_generated/" | ||
srcPath := "./src/main/java/com/yahoo/shopping/" | ||
|
||
//generate output result | ||
schema, err := rdl.ParseRDLFile("../../testdata/sampleWithoutVersion.rdl", false, false, false) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
GenerateJavaServer("withoutVersion", schema, testOutputDir, true, true, true, true, string(schema.Namespace), false) | ||
|
||
//asserts | ||
resourcesContent := checkAndGetFileContent(t, path, "SampleResources.java") | ||
assert.Contains(t, string(resourcesContent), "class SampleResources") | ||
assert.Contains(t, string(resourcesContent), "@Path(\"/sample\")") | ||
assert.Contains(t, string(resourcesContent), "User user") | ||
|
||
handlerContent := checkAndGetFileContent(t, path, "SampleHandler.java") | ||
assert.Contains(t, string(handlerContent), "interface SampleHandler") | ||
assert.Contains(t, string(handlerContent), "public User postUsers(ResourceContext context, User user)") | ||
assert.Contains(t, string(handlerContent), "public User getUsersById(ResourceContext context, Integer id)") | ||
|
||
serverContent := checkAndGetFileContent(t, path, "SampleServer.java") | ||
assert.Contains(t, string(serverContent), "class SampleServer") | ||
assert.Contains(t, string(serverContent), "bind(handler).to(SampleHandler.class)") | ||
|
||
hImplContent := checkAndGetFileContent(t, srcPath, "SampleHandlerImpl.java") | ||
assert.Contains(t, string(hImplContent), "class SampleHandlerImpl") | ||
assert.Contains(t, string(hImplContent), "implements SampleHandler") | ||
assert.Contains(t, string(hImplContent), "public User postUsers(ResourceContext context, User user)") | ||
assert.Contains(t, string(hImplContent), "public User getUsersById(ResourceContext context, Integer id)") | ||
|
||
// clean up folder | ||
defer os.RemoveAll(testOutputDir) | ||
defer os.RemoveAll("./src") | ||
} | ||
|
||
func TestGenerateServerWithVersion(t *testing.T) { | ||
testOutputDir := getTempDir(t, ".", "testOutput-") | ||
path := testOutputDir + "/com/yahoo/shopping/parsec_generated/" | ||
srcPath := "./src/main/java/com/yahoo/shopping/" | ||
|
||
//generate output result | ||
schema, err := rdl.ParseRDLFile("../../testdata/sampleWithVersion.rdl", false, false, false) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
|
||
GenerateJavaServer("withVersion", schema, testOutputDir, true, true, true, true, string(schema.Namespace), false) | ||
|
||
//asserts | ||
resourcesContent := checkAndGetFileContent(t, path, "SampleV2Resources.java") | ||
assert.Contains(t, string(resourcesContent), "class SampleV2Resources") | ||
assert.Contains(t, string(resourcesContent), "@Path(\"/sample/v2\")") | ||
assert.Contains(t, string(resourcesContent), "UserV2 user") | ||
|
||
handlerContent := checkAndGetFileContent(t, path, "SampleV2Handler.java") | ||
assert.Contains(t, string(handlerContent), "interface SampleV2Handler") | ||
assert.Contains(t, string(handlerContent), "public UserV2 postUsers(ResourceContext context, UserV2 user)") | ||
assert.Contains(t, string(handlerContent), "public UserV2 getUsersById(ResourceContext context, Integer id)") | ||
|
||
serverContent := checkAndGetFileContent(t, path, "SampleV2Server.java") | ||
assert.Contains(t, string(serverContent), "class SampleV2Server") | ||
assert.Contains(t, string(serverContent), "bind(handler).to(SampleV2Handler.class)") | ||
|
||
hImplContent := checkAndGetFileContent(t, srcPath, "SampleV2HandlerImpl.java") | ||
assert.Contains(t, string(hImplContent), "class SampleV2HandlerImpl") | ||
assert.Contains(t, string(hImplContent), "implements SampleV2Handler") | ||
assert.Contains(t, string(hImplContent), "public UserV2 postUsers(ResourceContext context, UserV2 user)") | ||
assert.Contains(t, string(hImplContent), "public UserV2 getUsersById(ResourceContext context, Integer id)") | ||
|
||
// clean up folder | ||
defer os.RemoveAll(testOutputDir) | ||
defer os.RemoveAll("./src") | ||
} | ||
|
||
func checkAndGetFileContent(t *testing.T, path string, fileName string) []byte { | ||
//1. check correspanding client file exists | ||
if _, err := os.Stat(path + fileName); err != nil { | ||
t.Fatalf("file not exists: %s", path+fileName) | ||
} | ||
//2. check file content | ||
content, err := os.ReadFile(path + fileName) | ||
if err != nil { | ||
t.Fatalf("can not read file: %s", path+fileName) | ||
} | ||
return content | ||
} | ||
|
||
func getTempDir(t *testing.T, dir string, prefix string) string { | ||
dir, err := ioutil.TempDir(dir, prefix) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
return dir | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace com.yahoo.shopping; | ||
name sample; | ||
version 2; | ||
|
||
// define datatype | ||
type User struct { | ||
int32 id; | ||
string name; | ||
} | ||
|
||
// define endpoints | ||
// create a new user | ||
resource User POST "/users" { | ||
User user; | ||
expected CREATED; | ||
} | ||
|
||
// get a user by id | ||
resource User GET "/users/{id}" { | ||
int32 id; | ||
expected OK; | ||
} |
Oops, something went wrong.