[cpp-restsdk] Generate mockable APIs (#595)

* Port GMock feature from NativeInstruments

swagger-codegen fork:
https://github.com/NativeInstruments/swagger-codegen/pull/9

* Update petstore for Mockable APIs

* Fix shared_ptr in templates for File params

* Add guards in templates for GMock APIs

* Regenerate samples without GMocks

* Add useful constructors for GMock APIs

* Add constructors to API header interface

* Update samples with explicit monadic constructors

* Add default implementations for destructors
This commit is contained in:
Daniel Miller 2018-07-24 00:31:21 -07:00 committed by William Cheng
parent 59d38d7dd2
commit 530065137d
39 changed files with 139 additions and 47 deletions

View File

@ -49,6 +49,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
public static final String DECLSPEC = "declspec";
public static final String DEFAULT_INCLUDE = "defaultInclude";
public static final String GENERATE_GMOCKS_FOR_APIS = "generateGMocksForApis";
protected String packageVersion = "1.0.0";
protected String declspec = "";
@ -114,6 +115,9 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
addOption(DEFAULT_INCLUDE,
"The default include statement that should be placed in all headers for including things like the declspec (convention: #include \"Commons.h\" ",
this.defaultInclude);
addOption(GENERATE_GMOCKS_FOR_APIS,
"Generate Google Mock classes for APIs.",
null);
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "", "ModelBase.h"));
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "", "ModelBase.cpp"));
@ -177,6 +181,11 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
defaultInclude = additionalProperties.get(DEFAULT_INCLUDE).toString();
}
if (convertPropertyToBoolean(GENERATE_GMOCKS_FOR_APIS)) {
apiTemplateFiles.put("api-gmock.mustache", "GMock.h");
additionalProperties.put("gmockApis", "true");
}
additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\."));
additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::"));
additionalProperties.put("modelHeaderGuardPrefix", modelPackage.replaceAll("\\.", "_").toUpperCase());

View File

@ -0,0 +1,41 @@
{{>licenseInfo}}
{{#operations}}
#ifndef {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_
#define {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_
#include <gmock/gmock.h>
#include "{{classname}}.h"
{{#apiNamespaceDeclarations}}
namespace {{this}} {
{{/apiNamespaceDeclarations}}
using namespace {{modelNamespace}};
class {{declspec}} {{classname}}Mock : public I{{classname}}
{
public:
using Base = I{{classname}};
{{classname}}Mock() = default;
explicit {{classname}}Mock( std::shared_ptr<ApiClient> apiClient ) { };
~{{classname}}Mock() override = default;
{{#operation}}
MOCK_METHOD{{allParams.size}}( {{operationId}}, pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> (
{{#allParams}}
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
{{/allParams}}
) );
{{/operation}}
};
{{#apiNamespaceDeclarations}}
}
{{/apiNamespaceDeclarations}}
#endif /* {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_ */
{{/operations}}

View File

@ -22,11 +22,38 @@ namespace {{this}} {
using namespace {{modelNamespace}};
class {{declspec}} {{classname}}
{{#gmockApis}}
class {{declspec}} I{{classname}}
{
public:
{{classname}}( std::shared_ptr<ApiClient> apiClient );
virtual ~{{classname}}();
I{{classname}}() = default;
virtual ~I{{classname}}() = default;
{{#operation}}
virtual pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}(
{{#allParams}}
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
{{/allParams}}
) = 0;
{{/operation}}
};{{/gmockApis}}
class {{declspec}} {{classname}} {{#gmockApis}} : public I{{classname}} {{/gmockApis}}
{
public:
{{#gmockApis}}
using Base = I{{classname}};
{{/gmockApis}}
explicit {{classname}}( std::shared_ptr<ApiClient> apiClient );
{{#gmockApis}}
~{{classname}}() override;
{{/gmockApis}}
{{^gmockApis}}
virtual ~{{classname}}() = default;
{{/gmockApis}}
{{#operation}}
/// <summary>
/// {{summary}}
@ -41,7 +68,7 @@ public:
{{#allParams}}
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
{{/allParams}}
);
){{#gmockApis}} override{{/gmockApis}};
{{/operation}}
protected:

View File

@ -1 +1 @@
3.0.0-SNAPSHOT
3.1.1-SNAPSHOT

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@ -12,7 +12,7 @@
/*
* MultipartFormData.h
*
* This class represents a container for building a application/x-multipart-formdata requests.
* This class represents a container for building application/x-multipart-formdata requests.
*/
#ifndef ORG_OPENAPITOOLS_CLIENT_MODEL_MultipartFormData_H_

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@ -35,11 +35,16 @@ namespace api {
using namespace org::openapitools::client::model;
class PetApi
class PetApi
{
public:
PetApi( std::shared_ptr<ApiClient> apiClient );
explicit PetApi( std::shared_ptr<ApiClient> apiClient );
virtual ~PetApi();
/// <summary>
/// Add a new pet to the store
/// </summary>

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@ -34,11 +34,16 @@ namespace api {
using namespace org::openapitools::client::model;
class StoreApi
class StoreApi
{
public:
StoreApi( std::shared_ptr<ApiClient> apiClient );
explicit StoreApi( std::shared_ptr<ApiClient> apiClient );
virtual ~StoreApi();
/// <summary>
/// Delete purchase order by ID
/// </summary>

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/
@ -34,11 +34,16 @@ namespace api {
using namespace org::openapitools::client::model;
class UserApi
class UserApi
{
public:
UserApi( std::shared_ptr<ApiClient> apiClient );
explicit UserApi( std::shared_ptr<ApiClient> apiClient );
virtual ~UserApi();
/// <summary>
/// Create user
/// </summary>

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/

View File

@ -4,7 +4,7 @@
*
* OpenAPI spec version: 1.0.0
*
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
* https://openapi-generator.tech
* Do not edit the class manually.
*/