From f9ad16e7aa7a96ec558696a2e5ec95bfdd06b8d0 Mon Sep 17 00:00:00 2001 From: geekerzp Date: Wed, 22 Jul 2015 17:45:31 +0800 Subject: [PATCH] Update objc client. * Add test cases for deserialization. * Pretty print model infomation. --- .../main/resources/objc/model-body.mustache | 6 +- .../petstore/objc/SwaggerClient/SWGCategory.m | 6 +- .../petstore/objc/SwaggerClient/SWGOrder.m | 6 +- .../petstore/objc/SwaggerClient/SWGPet.m | 6 +- .../petstore/objc/SwaggerClient/SWGTag.m | 6 +- .../petstore/objc/SwaggerClient/SWGUser.m | 6 +- .../SwaggerClient.xcodeproj/project.pbxproj | 8 ++ .../SwaggerClient/SWGViewController.m | 30 ++++- .../Tests/DeserializationTest.m | 119 ++++++++++++++++++ .../objc/SwaggerClientTests/Tests/PetTest.m | 37 ++++++ .../Tests/SWGApiClientTest.m | 73 ----------- 11 files changed, 222 insertions(+), 81 deletions(-) create mode 100644 samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m create mode 100644 samples/client/petstore/objc/SwaggerClientTests/Tests/PetTest.m diff --git a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache index b728e6bb06c..6e93823b1f5 100644 --- a/modules/swagger-codegen/src/main/resources/objc/model-body.mustache +++ b/modules/swagger-codegen/src/main/resources/objc/model-body.mustache @@ -3,7 +3,7 @@ #import "{{classname}}.h" @implementation {{classname}} - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ {{#vars}}@"{{baseName}}": @"{{name}}"{{#hasMore}}, {{/hasMore}}{{/vars}} }]; @@ -21,6 +21,10 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + {{/model}} @end {{/models}} diff --git a/samples/client/petstore/objc/SwaggerClient/SWGCategory.m b/samples/client/petstore/objc/SwaggerClient/SWGCategory.m index c41f76a5247..ebe7326d1bb 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGCategory.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGCategory.m @@ -1,7 +1,7 @@ #import "SWGCategory.h" @implementation SWGCategory - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"name": @"name" }]; @@ -19,4 +19,8 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGOrder.m b/samples/client/petstore/objc/SwaggerClient/SWGOrder.m index 5d3179446ed..d511eee2e08 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGOrder.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGOrder.m @@ -1,7 +1,7 @@ #import "SWGOrder.h" @implementation SWGOrder - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"petId": @"petId", @"quantity": @"quantity", @"shipDate": @"shipDate", @"status": @"status", @"complete": @"complete" }]; @@ -19,4 +19,8 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGPet.m b/samples/client/petstore/objc/SwaggerClient/SWGPet.m index d5559aa6f72..33c20c2dd20 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGPet.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGPet.m @@ -1,7 +1,7 @@ #import "SWGPet.h" @implementation SWGPet - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"category": @"category", @"name": @"name", @"photoUrls": @"photoUrls", @"tags": @"tags", @"status": @"status" }]; @@ -19,4 +19,8 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGTag.m b/samples/client/petstore/objc/SwaggerClient/SWGTag.m index 5d48a099070..5a2f395e7ad 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGTag.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGTag.m @@ -1,7 +1,7 @@ #import "SWGTag.h" @implementation SWGTag - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"name": @"name" }]; @@ -19,4 +19,8 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClient/SWGUser.m b/samples/client/petstore/objc/SwaggerClient/SWGUser.m index 060b65ffa12..1ee47811995 100644 --- a/samples/client/petstore/objc/SwaggerClient/SWGUser.m +++ b/samples/client/petstore/objc/SwaggerClient/SWGUser.m @@ -1,7 +1,7 @@ #import "SWGUser.h" @implementation SWGUser - + + (JSONKeyMapper *)keyMapper { return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id": @"_id", @"username": @"username", @"firstName": @"firstName", @"lastName": @"lastName", @"email": @"email", @"password": @"password", @"phone": @"phone", @"userStatus": @"userStatus" }]; @@ -19,4 +19,8 @@ } } +- (NSString *)description { + return [[self toDictionary] description]; +} + @end diff --git a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcodeproj/project.pbxproj b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcodeproj/project.pbxproj index 0ba21c560ef..a5fe06f63b7 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcodeproj/project.pbxproj +++ b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient.xcodeproj/project.pbxproj @@ -23,6 +23,8 @@ 6003F5BC195388D20070C39A /* Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6003F5BB195388D20070C39A /* Tests.m */; }; 873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; }; 94BE6BE84795B5034A811E61 /* libPods-SwaggerClient_Example.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8D46325ECAD48245C07F6733 /* libPods-SwaggerClient_Example.a */; }; + CF0ADB481B5F95D6008A2729 /* PetTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF0ADB471B5F95D6008A2729 /* PetTest.m */; }; + CF8F71391B5F73AC00162980 /* DeserializationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF8F71381B5F73AC00162980 /* DeserializationTest.m */; }; CFDFB4121B3CFFA8009739C5 /* UserApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40D1B3CFEC3009739C5 /* UserApiTest.m */; }; CFDFB4131B3CFFDD009739C5 /* PetApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */; }; CFDFB4141B3CFFF6009739C5 /* StoreApiTest.m in Sources */ = {isa = PBXBuildFile; fileRef = CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */; }; @@ -65,6 +67,8 @@ 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = ""; }; 8D46325ECAD48245C07F6733 /* libPods-SwaggerClient_Example.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-SwaggerClient_Example.a"; sourceTree = BUILT_PRODUCTS_DIR; }; BFB4BE760737508B3CFC23B2 /* Pods-SwaggerClient_Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwaggerClient_Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-SwaggerClient_Example/Pods-SwaggerClient_Example.release.xcconfig"; sourceTree = ""; }; + CF0ADB471B5F95D6008A2729 /* PetTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PetTest.m; sourceTree = ""; }; + CF8F71381B5F73AC00162980 /* DeserializationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DeserializationTest.m; sourceTree = ""; }; CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PetApiTest.m; sourceTree = ""; }; CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StoreApiTest.m; sourceTree = ""; }; CFDFB40C1B3CFEC3009739C5 /* SWGApiClientTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SWGApiClientTest.m; sourceTree = ""; }; @@ -166,6 +170,8 @@ CFDFB40A1B3CFEC3009739C5 /* PetApiTest.m */, CFDFB40B1B3CFEC3009739C5 /* StoreApiTest.m */, CFDFB40C1B3CFEC3009739C5 /* SWGApiClientTest.m */, + CF0ADB471B5F95D6008A2729 /* PetTest.m */, + CF8F71381B5F73AC00162980 /* DeserializationTest.m */, CFDFB40D1B3CFEC3009739C5 /* UserApiTest.m */, 6003F5BB195388D20070C39A /* Tests.m */, 6003F5B6195388D20070C39A /* Supporting Files */, @@ -374,10 +380,12 @@ buildActionMask = 2147483647; files = ( CFDFB4141B3CFFF6009739C5 /* StoreApiTest.m in Sources */, + CF0ADB481B5F95D6008A2729 /* PetTest.m in Sources */, CFDFB4131B3CFFDD009739C5 /* PetApiTest.m in Sources */, 6003F5BC195388D20070C39A /* Tests.m in Sources */, CFDFB4151B3D000B009739C5 /* SWGApiClientTest.m in Sources */, CFDFB4121B3CFFA8009739C5 /* UserApiTest.m in Sources */, + CF8F71391B5F73AC00162980 /* DeserializationTest.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m index a56f31408aa..d538fe49fdc 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m +++ b/samples/client/petstore/objc/SwaggerClientTests/SwaggerClient/SWGViewController.m @@ -7,6 +7,7 @@ // #import "SWGViewController.h" +#import @interface SWGViewController () @@ -17,13 +18,38 @@ - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view, typically from a nib. + SWGPet *pet = [self createPet]; + NSLog(@"%@", pet); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; - // Dispose of any resources that can be recreated. +} + +- (SWGPet*) createPet { + SWGPet * pet = [[SWGPet alloc] init]; + pet._id = [[NSNumber alloc] initWithLong:[[NSDate date] timeIntervalSince1970]]; + pet.name = @"monkey"; + + SWGCategory * category = [[SWGCategory alloc] init]; + category._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)]; + category.name = @"super-happy"; + pet.category = category; + + SWGTag *tag1 = [[SWGTag alloc] init]; + tag1._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)]; + tag1.name = @"test tag 1"; + SWGTag *tag2 = [[SWGTag alloc] init]; + tag2._id = [[NSNumber alloc] initWithInteger:arc4random_uniform(100000)]; + tag2.name = @"test tag 2"; + pet.tags = (NSArray *)[[NSArray alloc] initWithObjects:tag1, tag2, nil]; + + pet.status = @"available"; + + NSArray * photos = [[NSArray alloc] initWithObjects:@"http://foo.bar.com/3", @"http://foo.bar.com/4", nil]; + pet.photoUrls = photos; + return pet; } @end diff --git a/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m b/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m new file mode 100644 index 00000000000..2650dde31d0 --- /dev/null +++ b/samples/client/petstore/objc/SwaggerClientTests/Tests/DeserializationTest.m @@ -0,0 +1,119 @@ +#import +#import +#import +#import + +@interface DeserializationTest : XCTestCase { + +@private SWGApiClient *apiClient; + +} + +@end + +@implementation DeserializationTest + +- (void)setUp { + [super setUp]; + apiClient = [[SWGApiClient alloc] init]; +} + +- (void)testDeserializeDate { + NSString *dateStr = @"2012-09-27"; + + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"UTC"]; + [formatter setTimeZone:timezone]; + [formatter setDateFormat:@"yyyy-MM-dd"]; + NSDate *date = [formatter dateFromString:dateStr]; + + NSDate *deserializedDate = [apiClient deserialize:dateStr class:@"NSDate*"]; + + XCTAssertEqualWithAccuracy([date timeIntervalSinceReferenceDate], [deserializedDate timeIntervalSinceReferenceDate], 0.001); +} + +- (void)testDeserializeDateTime { + NSString *dateTimeStr = @"1997-07-16T19:20:30+00:00"; + + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; + NSDate *dateTime = [formatter dateFromString:dateTimeStr]; + + NSDate *deserializedDateTime = [apiClient deserialize:dateTimeStr class:@"NSDate*"]; + + XCTAssertEqualWithAccuracy([dateTime timeIntervalSinceReferenceDate], [deserializedDateTime timeIntervalSinceReferenceDate], 0.001); +} + +- (void)testDeserializeObject { + XCTAssertTrue([[apiClient deserialize:@"" class:@"NSObject*"] isKindOfClass:[NSObject class]]); +} + +- (void)testDeserializeString { + NSString *data = @"test string"; + NSString *result = [apiClient deserialize:data class:@"NSString*"]; + + XCTAssertTrue([result isEqualToString:data]); +} + +- (void)testDeserializeListOfModels { + NSArray *data = + @[ + @{ + @"id": @119, + @"category": @{ + @"id": @0, + @"name": @"string" + }, + @"name": @"doggie", + @"photoUrls": @[ + @"string" + ], + @"tags": @[ + @{ + @"id": @0, + @"name": @"string" + } + ], + @"status": @"available" + + }]; + + NSArray *result = [apiClient deserialize:data class:@"NSArray*"]; + + XCTAssertTrue([result isKindOfClass:[NSArray class]]); + XCTAssertTrue([[result firstObject] isKindOfClass:[SWGPet class]]); + XCTAssertEqualObjects([[result firstObject] _id], @119); +} + +- (void)testDeserializeMapOfModels { + NSDictionary *data = + @{ + @"pet": @{ + @"id": @119, + @"category": @{ + @"id": @0, + @"name": @"string" + }, + @"name": @"doggie", + @"photoUrls": @[ + @"string" + ], + @"tags": @[ + @{ + @"id": @0, + @"name": @"string" + } + ], + @"status": @"available" + + } + }; + + NSDictionary *result = [apiClient deserialize:data class:@"NSDictionary* /* NSString, SWGPet */"]; + + XCTAssertTrue([result isKindOfClass:[NSDictionary class]]); + XCTAssertTrue([result[@"pet"] isKindOfClass:[SWGPet class]]); + XCTAssertEqualObjects([result[@"pet"] _id], @119); +} + +@end diff --git a/samples/client/petstore/objc/SwaggerClientTests/Tests/PetTest.m b/samples/client/petstore/objc/SwaggerClientTests/Tests/PetTest.m new file mode 100644 index 00000000000..9dbde13310b --- /dev/null +++ b/samples/client/petstore/objc/SwaggerClientTests/Tests/PetTest.m @@ -0,0 +1,37 @@ +#import +#import +#import + +@interface PetTest : XCTestCase { + +@private SWGPet *pet; + +} + +@end + +@implementation PetTest + +- (void)setUp { + [super setUp]; + + NSDictionary *petDict = @{ @"id": @1, @"name": @"test pet", + @"status": @"sold", + @"photoUrls": @[@"string"], + @"category": @{ @"id": @1, @"name": @"test category" }, + @"tags": @[ @{ @"id": @1, @"name": @"test tag" } ]}; + pet = [[SWGPet alloc] initWithDictionary:petDict error:nil]; +} + +- (void)testDescription { + NSDictionary *petDict = @{ @"id": @1, @"name": @"test pet", + @"status": @"sold", + @"photoUrls": @[@"string"], + @"category": @{ @"id": @1, @"name": @"test category" }, + @"tags": @[ @{ @"id": @1, @"name": @"test tag" } ]}; + NSString *petStr = [petDict description]; + + XCTAssertTrue([[pet description] isEqualToString:petStr]); +} + +@end diff --git a/samples/client/petstore/objc/SwaggerClientTests/Tests/SWGApiClientTest.m b/samples/client/petstore/objc/SwaggerClientTests/Tests/SWGApiClientTest.m index 00c6bf70da6..6365632ea2d 100644 --- a/samples/client/petstore/objc/SwaggerClientTests/Tests/SWGApiClientTest.m +++ b/samples/client/petstore/objc/SwaggerClientTests/Tests/SWGApiClientTest.m @@ -98,77 +98,4 @@ XCTAssertEqualObjects(basicAuthCredentials, [config getBasicAuthToken]); } -- (void)testDeserialize { - id data; - id result; - - // list of models - data = - @[ - @{ - @"id": @119, - @"category": @{ - @"id": @0, - @"name": @"string" - }, - @"name": @"doggie", - @"photoUrls": @[ - @"string" - ], - @"tags": @[ - @{ - @"id": @0, - @"name": @"string" - } - ], - @"status": @"available" - - }]; - result = [self.apiClient deserialize:data class:@"NSArray*"]; - - XCTAssertTrue([result isKindOfClass:[NSArray class]]); - XCTAssertTrue([[result firstObject] isKindOfClass:[SWGPet class]]); - XCTAssertEqualObjects([[result firstObject] _id], @119); - - // map of models - data = - @{ - @"pet": @{ - @"id": @119, - @"category": @{ - @"id": @0, - @"name": @"string" - }, - @"name": @"doggie", - @"photoUrls": @[ - @"string" - ], - @"tags": @[ - @{ - @"id": @0, - @"name": @"string" - } - ], - @"status": @"available" - - } - }; - result = [self.apiClient deserialize:data class:@"NSDictionary* /* NSString, SWGPet */"]; - - XCTAssertTrue([result isKindOfClass:[NSDictionary class]]); - XCTAssertTrue([result[@"pet"] isKindOfClass:[SWGPet class]]); - XCTAssertEqualObjects([result[@"pet"] _id], @119); - - // pure object - result = [self.apiClient deserialize:@"" class:@"NSObject*"]; - - XCTAssertTrue([result isKindOfClass:[NSObject class]]); - - // NSString - data = @"test string"; - result = [self.apiClient deserialize:data class:@"NSString*"]; - - XCTAssertTrue([result isKindOfClass:[NSString class]]); -} - @end