Esempio n. 1
0
func TestOauth2Middleware(t *testing.T) {
	Convey("oauth2 middleware", t, func() {

		targetdir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("middleware generation test", func() {
			apiDef := new(raml.APIDefinition)
			err := raml.ParseFile("../fixtures/security/dropbox.raml", apiDef)
			So(err, ShouldBeNil)

			err = generateSecurity(apiDef.SecuritySchemes, targetdir)
			So(err, ShouldBeNil)

			// oauth 2 in dropbox
			s, err := testLoadFile(filepath.Join(targetdir, "oauth2_Dropbox.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("../fixtures/security/oauth2_Dropbox.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// oauth 2 facebook
			s, err = testLoadFile(filepath.Join(targetdir, "oauth2_Facebook.py"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/security/oauth2_Facebook.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		})

		Convey("routes generation", func() {
			apiDef := new(raml.APIDefinition)
			err := raml.ParseFile("../fixtures/security/dropbox.raml", apiDef)
			So(err, ShouldBeNil)

			_, err = generateServerResources(apiDef, targetdir)
			So(err, ShouldBeNil)

			// check route
			s, err := testLoadFile(filepath.Join(targetdir, "deliveries.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("../fixtures/security/deliveries.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		})

		Reset(func() {
			os.RemoveAll(targetdir)
		})
	})
}
Esempio n. 2
0
func TestGenerateClientFromRaml(t *testing.T) {
	Convey("generate client from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("./fixtures/client_resources/client.raml", apiDef)
		So(err, ShouldBeNil)

		targetdir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("Simple client from raml", func() {
			err = GenerateClient(apiDef, targetdir, "theclient", "go", "client")
			So(err, ShouldBeNil)

			s, err := testLoadFile(filepath.Join(targetdir, "client_structapitest.go"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/client_resources/client_structapitest.txt")
			So(err, ShouldBeNil)

			So(tmpl, ShouldEqual, s)
		})

		Reset(func() {
			os.RemoveAll(targetdir)
		})
	})
}
Esempio n. 3
0
func TestPythonResource(t *testing.T) {
	Convey("resource generator", t, func() {
		targetdir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("resource with request body", func() {
			apiDef := new(raml.APIDefinition)
			err := raml.ParseFile("../fixtures/server_resources/deliveries.raml", apiDef)
			So(err, ShouldBeNil)

			_, err = generateServerResources(apiDef, targetdir)
			So(err, ShouldBeNil)

			// check  api implementation
			s, err := testLoadFile(filepath.Join(targetdir, "deliveries.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("../fixtures/server_resources/deliveries.py")
			So(err, ShouldBeNil)
			So(s, ShouldEqual, tmpl)
		})

		Reset(func() {
			os.RemoveAll(targetdir)
		})
	})
}
Esempio n. 4
0
func TestGenerateClassFromBody(t *testing.T) {
	Convey("generate struct body from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("../fixtures/struct/struct.raml", apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("python class from request/response bodies", func() {
			rs := getAllResources(apiDef, true)
			err = generateClassesFromBodies(rs, targetDir)
			So(err, ShouldBeNil)

			// req body
			s, err := testLoadFile(filepath.Join(targetDir, "UsersPostReqBody.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("../fixtures/struct/UsersPostReqBody.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 5
0
func TestGenerateClientFromRaml(t *testing.T) {
	Convey("generate client from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("./fixtures/client_resources/client.raml", apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		err = GenerateClient(apiDef, targetDir, "theclient", "go", "client")
		So(err, ShouldBeNil)
		rootFixture := "./fixtures/client_resources"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"client_structapitest.go", "client_structapitest.txt"},
			{"users_service.go", "users_service.txt"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 6
0
//Execute generates a client from a RAML specification
func (command *ClientCommand) Execute() error {
	log.Debug("Generating a rest client for ", command.Language)
	apiDef := new(raml.APIDefinition)
	err := raml.ParseFile(command.RamlFile, apiDef)
	if err != nil {
		return err
	}
	return codegen.GenerateClient(apiDef, command.Dir, command.PackageName, command.Language, command.ImportPath)
}
Esempio n. 7
0
func TestGenerateStructBodyFromRaml(t *testing.T) {
	Convey("generate struct body from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("./fixtures/struct/struct.raml", apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("simple body", func() {
			err := generateBodyStructs(apiDef, targetDir, "main", langGo)
			So(err, ShouldBeNil)

			//load and compare UsersIdGetRespBody
			s, err := testLoadFile(filepath.Join(targetDir, "UsersIdGetRespBody.go"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/struct/UsersIdGetRespBody.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			//load and compare usersgetreqbody
			s, err = testLoadFile(filepath.Join(targetDir, "UsersPostReqBody.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("./fixtures/struct/UsersPostReqBody.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		})

		Convey("python class from request/response bodies", func() {
			err = generateBodyStructs(apiDef, targetDir, "", langPython)
			So(err, ShouldBeNil)

			// req body
			s, err := testLoadFile(filepath.Join(targetDir, "UsersPostReqBody.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/struct/UsersPostReqBody.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 8
0
func TestGeneratePythonClass(t *testing.T) {
	Convey("generate python class from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("./fixtures/struct/struct.raml", apiDef)
		So(err, ShouldBeNil)
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("python class from raml Types", func() {
			err = generatePythonClasses(apiDef.Types, targetDir)
			So(err, ShouldBeNil)

			// strings validator
			s, err := testLoadFile(filepath.Join(targetDir, "ValidationString.py"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/struct/ValidationString.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// with form field
			s, err = testLoadFile(filepath.Join(targetDir, "Cage.py"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("./fixtures/struct/Cage.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// FieldList of FormField
			s, err = testLoadFile(filepath.Join(targetDir, "animal.py"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("./fixtures/struct/animal.py")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// another test could be seen at body_test.go
		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})

	})
}
Esempio n. 9
0
//Execute generates a client from a RAML specification
func (command *CapnpCommand) Execute() error {
	var apiDef raml.APIDefinition

	command.Language = strings.ToLower(command.Language)
	if command.Language != "go" && command.Language != "plain" {
		return fmt.Errorf("canpnp generator only support plain & Go-compatible schema")
	}

	log.Debug("Generating capnp models")

	err := raml.ParseFile(command.RAMLFile, &apiDef)
	if err != nil {
		return err
	}
	return codegen.GenerateCapnp(&apiDef, command.Dir, command.Language, command.Package)
}
Esempio n. 10
0
func TestGenerateObjectFromRaml(t *testing.T) {
	Convey("generate object from raml", t, func() {
		var apiDef raml.APIDefinition
		err := raml.ParseFile("../fixtures/struct/struct.raml", &apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("Simple struct from raml", func() {
			err = generateObjects(apiDef.Types, targetDir)
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/object/"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"EnumCity.nim", "EnumCity.nim"},
				{"animal.nim", "animal.nim"},
				{"Cage.nim", "Cage.nim"},
				{"Cat.nim", "Cat.nim"},
				{"ArrayOfCats.nim", "ArrayOfCats.nim"},
				{"BidimensionalArrayOfCats.nim", "BidimensionalArrayOfCats.nim"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 11
0
func TestGeneratePythonClientFromRaml(t *testing.T) {
	Convey("generate python client", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("./fixtures/client/client.raml", apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("Simple client", func() {
			client := NewClient(apiDef)
			err = client.Generate(targetDir)
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/client"
			// cek with generated with fixtures
			checks := []struct {
				Result   string
				Expected string
			}{
				{"client.py", "client.py"},
				{"__init__.py", "__init__.py"},
				{"client_utils.py", "client_utils.py"},
				{"users_service.py", "users_service.py"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}
		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 12
0
func TestGenerateServer(t *testing.T) {
	Convey("generate server from raml", t, func() {
		var apiDef raml.APIDefinition
		err := raml.ParseFile("../fixtures/server_resources/deliveries.raml", &apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		ns := Server{
			Title:      apiDef.Title,
			APIDef:     &apiDef,
			APIDocsDir: "apidocs",
			Dir:        targetDir,
		}
		err = ns.Generate()
		So(err, ShouldBeNil)

		rootFixture := "./fixtures/server/delivery"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"main.nim", "main.nim"},
			{"deliveries_api.nim", "deliveries_api.nim"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 13
0
func TestGenerateClient(t *testing.T) {
	Convey("generate client from raml", t, func() {
		var apiDef raml.APIDefinition
		err := raml.ParseFile("../fixtures/client_resources/client.raml", &apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		client := Client{
			APIDef: &apiDef,
			Dir:    targetDir,
		}
		err = client.Generate()
		So(err, ShouldBeNil)

		rootFixture := "./fixtures/resource/client"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"client.nim", "client.nim"},
			{"Users_service.nim", "Users_service.nim"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 14
0
func TestGenerateResourceAPI(t *testing.T) {
	Convey("generate object from raml", t, func() {
		var apiDef raml.APIDefinition
		err := raml.ParseFile("../fixtures/server_resources/deliveries.raml", &apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("deliveries API", func() {
			err = generateResourceAPIs(getAllResources(&apiDef, true), targetDir)
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/resource/"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"deliveries_api.nim", "deliveries_api.nim"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 15
0
func TestGoLibrary(t *testing.T) {
	Convey("Library usage in server", t, func() {
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		err = GenerateServer("./fixtures/libraries/api.raml", targetDir, "main", "go", "apidocs", "examples.com/ramlcode", true)
		So(err, ShouldBeNil)

		rootFixture := "./fixtures/libraries/go_server"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"Place.go", "Place.txt"},
			{"dirs_api.go", "dirs_api.txt"},
			{"configs_api.go", "configs_api.txt"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
	Convey("Library usage in client", t, func() {
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		apiDef := new(raml.APIDefinition)
		err = raml.ParseFile("./fixtures/libraries/api.raml", apiDef)
		So(err, ShouldBeNil)

		err = GenerateClient(apiDef, targetDir, "theclient", langGo, "examples.com/client")
		So(err, ShouldBeNil)

		rootFixture := "./fixtures/libraries/go_client"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"Place.go", "Place.txt"},
			{"client_exampleapi.go", "client_exampleapi.txt"},
			{"client_utils.go", "client_utils.txt"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})

}
Esempio n. 16
0
func TestGenerateCapnpSchema(t *testing.T) {
	Convey("generate capnp schema from raml", t, func() {
		var apiDef raml.APIDefinition
		err := raml.ParseFile("./fixtures/struct.raml", &apiDef)
		So(err, ShouldBeNil)

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("Schema for Python & Nim", func() {
			err = GenerateCapnp(&apiDef, targetDir, "nim", "")
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/struct/vanilla"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"Animal.capnp", "Animal.capnp"},
				{"Cage.capnp", "Cage.capnp"},
				{"Admin.capnp", "Admin.capnp"},
				{"EnumAdminClearanceLevel.capnp", "EnumAdminClearanceLevel.capnp"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}

		})

		Convey("Schema for Go", func() {
			err = GenerateCapnp(&apiDef, targetDir, "go", "main")
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/struct/golang"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"Animal.capnp", "Animal.capnp"},
				{"Cage.capnp", "Cage.capnp"},
				{"Admin.capnp", "Admin.capnp"},
				{"EnumAdminClearanceLevel.capnp", "EnumAdminClearanceLevel.capnp"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}
Esempio n. 17
0
func TestGoLibrary(t *testing.T) {
	Convey("Library usage in server", t, func() {
		var apiDef raml.APIDefinition

		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		err = raml.ParseFile("../fixtures/libraries/api.raml", &apiDef)
		So(err, ShouldBeNil)

		server := NewServer(&apiDef, "main", "apidocs", "examples.com/ramlcode", true)
		err = server.Generate(targetDir)
		So(err, ShouldBeNil)

		rootFixture := "../fixtures/libraries/go_server"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"Place.go", "Place.txt"},
			{"dirs_api.go", "dirs_api.txt"},
			{"configs_api.go", "configs_api.txt"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})

	Convey("Library usage in client", t, func() {
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		apiDef := new(raml.APIDefinition)
		err = raml.ParseFile("../fixtures/libraries/api.raml", apiDef)
		So(err, ShouldBeNil)

		client, err := NewClient(apiDef, "theclient", "examples.com/theclient")
		So(err, ShouldBeNil)

		err = client.Generate(targetDir)
		So(err, ShouldBeNil)

		rootFixture := "../fixtures/libraries/go_client"
		checks := []struct {
			Result   string
			Expected string
		}{
			{"Place.go", "Place.txt"},
			{"client_exampleapi.go", "client_exampleapi.txt"},
			{"client_utils.go", "client_utils.txt"},
			{"dirs_service.go", "dirs_service.txt"},
			{"configs_service.go", "configs_service.txt"},
		}

		for _, check := range checks {
			s, err := testLoadFile(filepath.Join(targetDir, check.Result))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)
		}

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})

	Convey("raml-examples", t, func() {
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("server", func() {
			var apiDef raml.APIDefinition

			err = raml.ParseFile("../fixtures/raml-examples/libraries/api.raml", &apiDef)
			So(err, ShouldBeNil)

			server := NewServer(&apiDef, "main", "apidocs", "examples.com/libro", true)
			err = server.Generate(targetDir)
			So(err, ShouldBeNil)

			rootFixture := "../fixtures/libraries/raml-examples/go_server"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"person_api.go", "person_api.txt"},
				{"types_lib/Person.go", "types_lib/Person.txt"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}
		})

		Convey("client", func() {
			var apiDef raml.APIDefinition

			err = raml.ParseFile("../fixtures/raml-examples/libraries/api.raml", &apiDef)
			So(err, ShouldBeNil)

			client, err := NewClient(&apiDef, "client", "examples.com/libro")
			So(err, ShouldBeNil)

			err = client.Generate(targetDir)
			So(err, ShouldBeNil)

			rootFixture := "../fixtures/libraries/raml-examples/go_client"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"person_service.go", "person_service.txt"},
				{"types_lib/Person.go", "types_lib/Person.txt"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}
		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})

}
func TestResource(t *testing.T) {
	Convey("resource generator", t, func() {
		targetdir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		apiDef := new(raml.APIDefinition)

		Convey("simple resource", func() {
			err := raml.ParseFile("./fixtures/server_resources/deliveries.raml", apiDef)
			So(err, ShouldBeNil)

			_, err = generateServerResources(apiDef, targetdir, "main", "go")
			So(err, ShouldBeNil)

			// check interface file
			s, err := testLoadFile(filepath.Join(targetdir, "deliveries_if.go"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/server_resources/deliveries_if.txt")
			So(err, ShouldBeNil)
			So(s, ShouldEqual, tmpl)

			// check api implemetation file
			s, err = testLoadFile(filepath.Join(targetdir, "deliveries_api.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("./fixtures/server_resources/deliveries_api.txt")
			So(err, ShouldBeNil)
			So(s, ShouldEqual, tmpl)
		})

		Convey("resource with request body", func() {
			err := raml.ParseFile("./fixtures/server_resources/usergroups.raml", apiDef)
			So(err, ShouldBeNil)

			_, err = generateServerResources(apiDef, targetdir, "main", "go")
			So(err, ShouldBeNil)

			// check users api implementation
			s, err := testLoadFile(filepath.Join(targetdir, "users_api.go"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/server_resources/users_api.txt")
			So(err, ShouldBeNil)
			So(s, ShouldEqual, tmpl)

			// check user interface
			s, err = testLoadFile(filepath.Join(targetdir, "users_if.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("./fixtures/server_resources/users_if.txt")
			So(err, ShouldBeNil)
			So(s, ShouldEqual, tmpl)

		})

		Reset(func() {
			os.RemoveAll(targetdir)
		})
	})
}
Esempio n. 19
0
func TestGenerateStructFromRaml(t *testing.T) {
	Convey("generate struct from raml", t, func() {
		apiDef := new(raml.APIDefinition)
		err := raml.ParseFile("../fixtures/struct/struct.raml", apiDef)
		So(err, ShouldBeNil)
		targetdir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("Simple struct from raml", func() {
			err = generateStructs(apiDef.Types, targetdir, "main")
			So(err, ShouldBeNil)

			//first test
			s, err := testLoadFile(filepath.Join(targetdir, "EnumCity.go"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("../fixtures/struct/enumcity.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			//second test
			s, err = testLoadFile(filepath.Join(targetdir, "animal.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/animal.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			//third test, single inheritance
			s, err = testLoadFile(filepath.Join(targetdir, "SingleInheritance.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/singleinheritance.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			//fourth test, multiple inheritance
			s, err = testLoadFile(filepath.Join(targetdir, "MultipleInheritance.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/multipleinheritance.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			//fifth test, array of object
			s, err = testLoadFile(filepath.Join(targetdir, "ArrayOfCats.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/arrayofcats.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// bidimensional array
			s, err = testLoadFile(filepath.Join(targetdir, "BidimensionalArrayOfCats.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/bidimensionalarrayofcats.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// using map type & testing case sensitive type name
			s, err = testLoadFile(filepath.Join(targetdir, "petshop.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/petshop.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// Union
			s, err = testLoadFile(filepath.Join(targetdir, "Pet.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/Pet.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// Array of union
			s, err = testLoadFile(filepath.Join(targetdir, "ArrayOfPets.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/ArrayOfPets.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// Specialization
			s, err = testLoadFile(filepath.Join(targetdir, "Specialization.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/Specialization.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// Enum type
			s, err = testLoadFile(filepath.Join(targetdir, "EnumString.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/enumstring.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

			// With validation
			s, err = testLoadFile(filepath.Join(targetdir, "ValidationString.go"))
			So(err, ShouldBeNil)

			tmpl, err = testLoadFile("../fixtures/struct/ValidationString.txt")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

		})

		Reset(func() {
			os.RemoveAll(targetdir)
		})
	})
}
Esempio n. 20
0
func TestGenerateObjectMethodBody(t *testing.T) {
	Convey("generate object from method body", t, func() {
		targetDir, err := ioutil.TempDir("", "")
		So(err, ShouldBeNil)

		Convey("generate request body", func() {
			var body raml.Bodies
			properties := map[string]interface{}{
				"age": map[interface{}]interface{}{
					"type": "integer",
				},
				"ID": map[interface{}]interface{}{
					"type": "string",
				},
				"item": map[interface{}]interface{}{},
				"grades": map[interface{}]interface{}{
					"type": "integer[]",
				},
			}
			body.ApplicationJSON = &raml.BodiesProperty{
				Properties: properties,
			}

			_, err := generateObjectFromBody("usersPost", &body, true, targetDir)
			So(err, ShouldBeNil)

			s, err := testLoadFile(filepath.Join(targetDir, "usersPostReqBody.nim"))
			So(err, ShouldBeNil)

			tmpl, err := testLoadFile("./fixtures/object/usersPostReqBody.nim")
			So(err, ShouldBeNil)

			So(s, ShouldEqual, tmpl)

		})

		Convey("Simple object from raml", func() {
			var apiDef raml.APIDefinition
			err := raml.ParseFile("../fixtures/struct/struct.raml", &apiDef)
			So(err, ShouldBeNil)

			_, err = generateObjectsFromBodies(getAllResources(&apiDef, true), targetDir)
			So(err, ShouldBeNil)

			rootFixture := "./fixtures/object/"
			checks := []struct {
				Result   string
				Expected string
			}{
				{"usersPostReqBody.nim", "usersPostReqBody.nim"},
				{"usersByIdGetRespBody.nim", "usersByIdGetRespBody.nim"},
			}

			for _, check := range checks {
				s, err := testLoadFile(filepath.Join(targetDir, check.Result))
				So(err, ShouldBeNil)

				tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected))
				So(err, ShouldBeNil)

				So(s, ShouldEqual, tmpl)
			}

		})

		Reset(func() {
			os.RemoveAll(targetDir)
		})
	})
}