1
+ #import " PetApiTest.h"
2
+ #import " SWGFile.h"
3
+
4
+ @implementation PetApiTest
5
+
6
+ - (void )setUp {
7
+ [super setUp ];
8
+ api = [[SWGPetApi alloc ]init];
9
+ // [[SWGApiClient sharedClientFromPool]setLoggingEnabled:true];
10
+ [SWGPetApi setBasePath: @" http://localhost:8002/api" ];
11
+ }
12
+
13
+ - (void )tearDown {
14
+ [super tearDown ];
15
+ }
16
+
17
+ - (void )testGetPetById {
18
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testGetPetById" ];
19
+ [api getPetByIdWithCompletionBlock: @1 completionHandler: ^(SWGPet *output, NSError *error) {
20
+ if (error){
21
+ XCTFail (@" got error %@ " , error);
22
+ }
23
+ if (output){
24
+ XCTAssertNotNil ([output _id ], @" token was nil" );
25
+ }
26
+ [expectation fulfill ];
27
+ }];
28
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
29
+ }
30
+
31
+ - (void ) testAddPet {
32
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testAddPet" ];
33
+
34
+ SWGPet * petToAdd = [[SWGPet alloc ] init ];
35
+ [petToAdd set_id: @1000 ];
36
+ NSMutableArray * tags = [[NSMutableArray alloc ] init ];
37
+ for (int i = 0 ; i < 5 ; i++){
38
+ SWGTag * tag = [[SWGTag alloc ] init ];
39
+ [tag set_id: [NSNumber numberWithInt: i]];
40
+ [tag setName: [NSString stringWithFormat: @" tag-%d " , i]];
41
+ [tags addObject: tag];
42
+ }
43
+ [petToAdd setTags: tags];
44
+ [petToAdd setStatus: @" lost" ];
45
+
46
+ SWGCategory * category = [[SWGCategory alloc ] init ];
47
+ [category setName: @" sold" ];
48
+ [petToAdd setCategory: category];
49
+ [petToAdd setName: @" dragon" ];
50
+
51
+ NSMutableArray * photos = [[NSMutableArray alloc ] init ];
52
+ for (int i = 0 ; i < 10 ; i++){
53
+ NSString * url = [NSString stringWithFormat: @" http://foo.com/photo/%d " , i];
54
+ [photos addObject: url];
55
+ }
56
+ [petToAdd setPhotoUrls: photos];
57
+
58
+ [api addPetWithCompletionBlock: petToAdd completionHandler: ^(NSError *error) {
59
+ if (error){
60
+ XCTFail (@" got error %@ " , error);
61
+ }
62
+ [expectation fulfill ];
63
+ }];
64
+
65
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
66
+ }
67
+
68
+ - (void ) testUpdatePet {
69
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testUpdatePet" ];
70
+ SWGPet * petToAdd = [[SWGPet alloc ] init ];
71
+ [petToAdd set_id: [NSNumber numberWithInt: 1000 ]];
72
+ NSMutableArray * tags = [[NSMutableArray alloc ] init ];
73
+ for (int i = 0 ; i < 5 ; i++){
74
+ SWGTag * tag = [[SWGTag alloc ] init ];
75
+ [tag set_id: [NSNumber numberWithInt: i]];
76
+ [tag setName: [NSString stringWithFormat: @" tag-%d " , i]];
77
+ [tags addObject: tag];
78
+ }
79
+ [petToAdd setTags: tags];
80
+ [petToAdd setStatus: @" lost" ];
81
+
82
+ SWGCategory * category = [[SWGCategory alloc ] init ];
83
+ [category setName: @" sold" ];
84
+ [petToAdd setCategory: category];
85
+ [petToAdd setName: @" dragon" ];
86
+
87
+ NSMutableArray * photos = [[NSMutableArray alloc ] init ];
88
+ for (int i = 0 ; i < 10 ; i++){
89
+ NSString * url = [NSString stringWithFormat: @" http://foo.com/photo/%d " , i];
90
+ [photos addObject: url];
91
+ }
92
+ [petToAdd setPhotoUrls: photos];
93
+ dispatch_semaphore_t sema = dispatch_semaphore_create (0 );
94
+
95
+ static bool hasResponse = false ;
96
+ [api addPetWithCompletionBlock: petToAdd completionHandler: ^(NSError *error) {
97
+ if (error) {
98
+ XCTFail (@" got error %@ " , error);
99
+ }
100
+ else {
101
+ [api getPetByIdWithCompletionBlock: [NSString stringWithFormat: @" %@ " ,[petToAdd _id ]] completionHandler: ^(SWGPet *output, NSError *error) {
102
+ if (error) {
103
+ XCTFail (@" got error %@ " , error);
104
+ }
105
+ if (output == nil ){
106
+ NSLog (@" failed to fetch pet" );
107
+ }
108
+ else {
109
+ SWGPet* pet = [[SWGPet alloc ] initWithValues: [output asDictionary ]];
110
+ NSLog (@" got the pet" );
111
+
112
+ [pet setName: @" programmer" ];
113
+ [pet setStatus: @" confused" ];
114
+
115
+ [api updatePetWithCompletionBlock: pet
116
+ completionHandler: ^(NSError *error) {
117
+ if (error) {
118
+ XCTFail (@" got error %@ " , error);
119
+ }
120
+ [api getPetByIdWithCompletionBlock: @1000 completionHandler: ^(SWGPet *output, NSError *error) {
121
+ if (error) {
122
+ XCTFail (@" got error %@ " , error);
123
+ }
124
+ if (output == nil ){
125
+ NSLog (@" failed to fetch pet" );
126
+ }
127
+ else {
128
+ SWGPet* pet = [[SWGPet alloc ] initWithValues: [output asDictionary ]];
129
+ XCTAssertNotNil ([pet _id ], @" pet was nil" );
130
+ XCTAssertEqualObjects ([pet name ], @" programmer" , @" pet name was not updated" );
131
+ XCTAssertEqualObjects ([pet status ], @" confused" , @" pet status was not updated" );
132
+ }
133
+ [expectation fulfill ];
134
+
135
+ }];
136
+ }];
137
+ }
138
+ }];
139
+ }
140
+ }];
141
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
142
+ }
143
+
144
+ - (void )testGetPetByStatus {
145
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testGetPetByStatus" ];
146
+ static NSMutableArray * pets = nil ;
147
+ static NSError * gError = nil ;
148
+ [api findPetsByStatusWithCompletionBlock: @" available" completionHandler: ^(NSArray *output, NSError *error) {
149
+ if (error) {
150
+ gError = error;
151
+ }
152
+ if (output == nil ){
153
+ NSLog (@" failed to fetch pets" );
154
+ }
155
+ else {
156
+ pets = [[NSMutableArray alloc ]init];
157
+ for (SWGPet* pet in output) {
158
+ [pets addObject: [[SWGPet alloc ] initWithValues: [pet asDictionary ]]];
159
+ }
160
+ }
161
+ }];
162
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
163
+ }
164
+
165
+ - (void )testGetPetByTags {
166
+ XCTestExpectation *expectation = [self expectationWithDescription: @" testGetPetByTags" ];
167
+ [api findPetsByTagsWithCompletionBlock: @" tag1,tag2" completionHandler: ^(NSArray *output, NSError *error) {
168
+ if (error){
169
+ XCTFail (@" got error %@ " , error);
170
+ }
171
+ if (output){
172
+ for (SWGPet * pet in output) {
173
+ bool hasTag = false ;
174
+ for (SWGTag * tag in [pet tags ]){
175
+ if ([[tag name ] isEqualToString: @" tag1" ] || [[tag name ] isEqualToString: @" tag2" ])
176
+ hasTag = true ;
177
+ }
178
+ if (!hasTag)
179
+ XCTFail (@" failed to find tag in pet" );
180
+ }
181
+ }
182
+ [expectation fulfill ];
183
+ }];
184
+ [self waitForExpectationsWithTimeout: 2.0 handler: nil ];
185
+ }
186
+ @end
0 commit comments