Skip to content

Commit 5300651

Browse files
Peaches491wing328
authored andcommitted
[cpp-restsdk] Generate mockable APIs (#595)
* Port GMock feature from NativeInstruments swagger-codegen fork: NativeInstruments/swagger-codegen#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
1 parent 59d38d7 commit 5300651

39 files changed

Lines changed: 139 additions & 47 deletions

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppRestSdkClientCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class CppRestSdkClientCodegen extends AbstractCppCodegen {
4949

5050
public static final String DECLSPEC = "declspec";
5151
public static final String DEFAULT_INCLUDE = "defaultInclude";
52+
public static final String GENERATE_GMOCKS_FOR_APIS = "generateGMocksForApis";
5253

5354
protected String packageVersion = "1.0.0";
5455
protected String declspec = "";
@@ -114,6 +115,9 @@ public CppRestSdkClientCodegen() {
114115
addOption(DEFAULT_INCLUDE,
115116
"The default include statement that should be placed in all headers for including things like the declspec (convention: #include \"Commons.h\" ",
116117
this.defaultInclude);
118+
addOption(GENERATE_GMOCKS_FOR_APIS,
119+
"Generate Google Mock classes for APIs.",
120+
null);
117121

118122
supportingFiles.add(new SupportingFile("modelbase-header.mustache", "", "ModelBase.h"));
119123
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "", "ModelBase.cpp"));
@@ -177,6 +181,11 @@ public void processOpts() {
177181
defaultInclude = additionalProperties.get(DEFAULT_INCLUDE).toString();
178182
}
179183

184+
if (convertPropertyToBoolean(GENERATE_GMOCKS_FOR_APIS)) {
185+
apiTemplateFiles.put("api-gmock.mustache", "GMock.h");
186+
additionalProperties.put("gmockApis", "true");
187+
}
188+
180189
additionalProperties.put("modelNamespaceDeclarations", modelPackage.split("\\."));
181190
additionalProperties.put("modelNamespace", modelPackage.replaceAll("\\.", "::"));
182191
additionalProperties.put("modelHeaderGuardPrefix", modelPackage.replaceAll("\\.", "_").toUpperCase());
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{>licenseInfo}}
2+
{{#operations}}
3+
#ifndef {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_
4+
#define {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_
5+
6+
#include <gmock/gmock.h>
7+
8+
#include "{{classname}}.h"
9+
10+
{{#apiNamespaceDeclarations}}
11+
namespace {{this}} {
12+
{{/apiNamespaceDeclarations}}
13+
14+
using namespace {{modelNamespace}};
15+
16+
17+
class {{declspec}} {{classname}}Mock : public I{{classname}}
18+
{
19+
public:
20+
using Base = I{{classname}};
21+
22+
{{classname}}Mock() = default;
23+
explicit {{classname}}Mock( std::shared_ptr<ApiClient> apiClient ) { };
24+
~{{classname}}Mock() override = default;
25+
26+
{{#operation}}
27+
MOCK_METHOD{{allParams.size}}( {{operationId}}, pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> (
28+
{{#allParams}}
29+
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
30+
{{/allParams}}
31+
) );
32+
{{/operation}}
33+
};
34+
35+
{{#apiNamespaceDeclarations}}
36+
}
37+
{{/apiNamespaceDeclarations}}
38+
39+
#endif /* {{apiHeaderGuardPrefix}}_{{classname}}GMock_H_ */
40+
41+
{{/operations}}

modules/openapi-generator/src/main/resources/cpp-rest-sdk-client/api-header.mustache

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,38 @@ namespace {{this}} {
2222

2323
using namespace {{modelNamespace}};
2424

25-
class {{declspec}} {{classname}}
25+
{{#gmockApis}}
26+
class {{declspec}} I{{classname}}
2627
{
2728
public:
28-
{{classname}}( std::shared_ptr<ApiClient> apiClient );
29-
virtual ~{{classname}}();
29+
I{{classname}}() = default;
30+
virtual ~I{{classname}}() = default;
31+
32+
{{#operation}}
33+
virtual pplx::task<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}> {{operationId}}(
34+
{{#allParams}}
35+
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
36+
{{/allParams}}
37+
) = 0;
38+
{{/operation}}
39+
};{{/gmockApis}}
40+
41+
class {{declspec}} {{classname}} {{#gmockApis}} : public I{{classname}} {{/gmockApis}}
42+
{
43+
public:
44+
{{#gmockApis}}
45+
using Base = I{{classname}};
46+
{{/gmockApis}}
47+
48+
explicit {{classname}}( std::shared_ptr<ApiClient> apiClient );
49+
50+
{{#gmockApis}}
51+
~{{classname}}() override;
52+
{{/gmockApis}}
53+
{{^gmockApis}}
54+
virtual ~{{classname}}() = default;
55+
{{/gmockApis}}
56+
3057
{{#operation}}
3158
/// <summary>
3259
/// {{summary}}
@@ -41,7 +68,7 @@ public:
4168
{{#allParams}}
4269
{{^required}}boost::optional<{{/required}}{{#isFile}}std::shared_ptr<{{/isFile}}{{{dataType}}}{{#isFile}}>{{/isFile}}{{^required}}>{{/required}} {{paramName}}{{#hasMore}},{{/hasMore}}
4370
{{/allParams}}
44-
);
71+
){{#gmockApis}} override{{/gmockApis}};
4572
{{/operation}}
4673

4774
protected:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.1.1-SNAPSHOT

samples/client/petstore/cpp-restsdk/ApiClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

samples/client/petstore/cpp-restsdk/ApiException.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by OpenAPI-Generator 3.0.0-SNAPSHOT.
7+
* NOTE: This class is auto generated by OpenAPI-Generator 3.1.1-SNAPSHOT.
88
* https://openapi-generator.tech
99
* Do not edit the class manually.
1010
*/

0 commit comments

Comments
 (0)