From e9f49abfee90b94ce40a868c12a2a0b803b8f203 Mon Sep 17 00:00:00 2001 From: wing328 Date: Thu, 12 Oct 2017 19:31:36 +0800 Subject: [PATCH] add back petstore test (#6663) --- .../src/IO.Swagger.Test/Api/PetApiTests.cs | 266 ++++++++++++++---- 1 file changed, 204 insertions(+), 62 deletions(-) diff --git a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Api/PetApiTests.cs b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Api/PetApiTests.cs index a512676b08c..37f6cb91a35 100644 --- a/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Api/PetApiTests.cs +++ b/samples/client/petstore/csharp/SwaggerClient/src/IO.Swagger.Test/Api/PetApiTests.cs @@ -1,25 +1,3 @@ -/* - * Swagger Petstore - * - * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ - * - * OpenAPI spec version: 1.0.0 - * Contact: apiteam@swagger.io - * Generated by: https://github.com/swagger-api/swagger-codegen.git - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - using System; using System.IO; using System.Collections.Generic; @@ -47,6 +25,45 @@ public class PetApiTests { private PetApi instance; + private long petId = 11088; + + /// + /// Create a Pet object + /// + private Pet createPet() + { + // create pet + Pet p = new Pet(Name: "Csharp test", PhotoUrls: new List { "http://petstore.com/csharp_test" }); + p.Id = petId; + //p.Name = "Csharp test"; + p.Status = Pet.StatusEnum.Available; + // create Category object + Category category = new Category(); + category.Id = 56; + category.Name = "sample category name2"; + List photoUrls = new List(new String[] {"sample photoUrls"}); + // create Tag object + Tag tag = new Tag(); + tag.Id = petId; + tag.Name = "csharp sample tag name1"; + List tags = new List(new Tag[] {tag}); + p.Tags = tags; + p.Category = category; + p.PhotoUrls = photoUrls; + + return p; + } + + /// + /// Convert string to byte array + /// + private byte[] GetBytes(string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } + /// /// Setup before each unit test /// @@ -54,6 +71,13 @@ public class PetApiTests public void Init() { instance = new PetApi(); + + // create pet + Pet p = createPet(); + + // add pet before testing + PetApi petApi = new PetApi("http://petstore.swagger.io/v2/"); + petApi.AddPet (p); } /// @@ -62,7 +86,9 @@ public void Init() [TearDown] public void Cleanup() { - + // remove the pet after testing + PetApi petApi = new PetApi (); + petApi.DeletePet(petId, "test key"); } /// @@ -71,8 +97,7 @@ public void Cleanup() [Test] public void InstanceTest() { - // TODO uncomment below to test 'IsInstanceOfType' PetApi - //Assert.IsInstanceOfType(typeof(PetApi), instance, "instance is a PetApi"); + Assert.IsInstanceOf(instance); } @@ -82,10 +107,9 @@ public void InstanceTest() [Test] public void AddPetTest() { - // TODO uncomment below to test the method and replace null with proper value - //Pet body = null; - //instance.AddPet(body); - + // create pet + Pet p = createPet(); + instance.AddPet(p); } /// @@ -94,11 +118,7 @@ public void AddPetTest() [Test] public void DeletePetTest() { - // TODO uncomment below to test the method and replace null with proper value - //long? petId = null; - //string apiKey = null; - //instance.DeletePet(petId, apiKey); - + // no need to test as it'c covered by Cleanup() already } /// @@ -107,10 +127,15 @@ public void DeletePetTest() [Test] public void FindPetsByStatusTest() { - // TODO uncomment below to test the method and replace null with proper value - //List status = null; - //var response = instance.FindPetsByStatus(status); - //Assert.IsInstanceOf> (response, "response is List"); + PetApi petApi = new PetApi (); + List tagsList = new List(new String[] {"available"}); + + List listPet = petApi.FindPetsByTags (tagsList); + foreach (Pet pet in listPet) // Loop through List with foreach. + { + Assert.IsInstanceOf(pet); + Assert.AreEqual ("csharp sample tag name1", pet.Tags[0]); + } } /// @@ -119,10 +144,9 @@ public void FindPetsByStatusTest() [Test] public void FindPetsByTagsTest() { - // TODO uncomment below to test the method and replace null with proper value - //List tags = null; - //var response = instance.FindPetsByTags(tags); - //Assert.IsInstanceOf> (response, "response is List"); + List tags = new List(new String[] {"pet"}); + var response = instance.FindPetsByTags(tags); + Assert.IsInstanceOf>(response); } /// @@ -131,22 +155,96 @@ public void FindPetsByTagsTest() [Test] public void GetPetByIdTest() { - // TODO uncomment below to test the method and replace null with proper value - //long? petId = null; - //var response = instance.GetPetById(petId); - //Assert.IsInstanceOf (response, "response is Pet"); + // set timeout to 10 seconds + Configuration c1 = new Configuration(); + c1.Timeout = 10000; + c1.UserAgent = "TEST_USER_AGENT"; + + PetApi petApi = new PetApi (c1); + Pet response = petApi.GetPetById (petId); + Assert.IsInstanceOf(response); + + Assert.AreEqual ("Csharp test", response.Name); + Assert.AreEqual (Pet.StatusEnum.Available, response.Status); + + Assert.IsInstanceOf>(response.Tags); + Assert.AreEqual (petId, response.Tags [0].Id); + Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name); + + Assert.IsInstanceOf>(response.PhotoUrls); + Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); + + Assert.IsInstanceOf(response.Category); + Assert.AreEqual (56, response.Category.Id); + Assert.AreEqual ("sample category name2", response.Category.Name); } + + /// + /// Test GetPetByIdAsync + /// + [Test ()] + public void TestGetPetByIdAsync () + { + PetApi petApi = new PetApi (); + var task = petApi.GetPetByIdAsync (petId); + Pet response = task.Result; + Assert.IsInstanceOf(response); + + Assert.AreEqual ("Csharp test", response.Name); + Assert.AreEqual (Pet.StatusEnum.Available, response.Status); + + Assert.IsInstanceOf>(response.Tags); + Assert.AreEqual (petId, response.Tags [0].Id); + Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name); + + Assert.IsInstanceOf>(response.PhotoUrls); + Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); + + Assert.IsInstanceOf(response.Category); + Assert.AreEqual (56, response.Category.Id); + Assert.AreEqual ("sample category name2", response.Category.Name); + } + /// + /// Test GetPetByIdAsyncWithHttpInfo + /// + [Test ()] + public void TestGetPetByIdAsyncWithHttpInfo () + { + PetApi petApi = new PetApi (); + var task = petApi.GetPetByIdAsyncWithHttpInfo (petId); + + Assert.AreEqual (200, task.Result.StatusCode); + Assert.IsTrue (task.Result.Headers.ContainsKey("Content-Type")); + Assert.AreEqual (task.Result.Headers["Content-Type"], "application/json"); + + Pet response = task.Result.Data; + Assert.IsInstanceOf(response); + + Assert.AreEqual ("Csharp test", response.Name); + Assert.AreEqual (Pet.StatusEnum.Available, response.Status); + + Assert.IsInstanceOf>(response.Tags); + Assert.AreEqual (petId, response.Tags [0].Id); + Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name); + + Assert.IsInstanceOf>(response.PhotoUrls); + Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]); + + Assert.IsInstanceOf(response.Category); + Assert.AreEqual (56, response.Category.Id); + Assert.AreEqual ("sample category name2", response.Category.Name); + } + /// /// Test UpdatePet /// [Test] public void UpdatePetTest() { - // TODO uncomment below to test the method and replace null with proper value - //Pet body = null; - //instance.UpdatePet(body); - + // create pet + Pet p = createPet(); + instance.UpdatePet(p); } /// @@ -155,12 +253,24 @@ public void UpdatePetTest() [Test] public void UpdatePetWithFormTest() { - // TODO uncomment below to test the method and replace null with proper value - //long? petId = null; - //string name = null; - //string status = null; - //instance.UpdatePetWithForm(petId, name, status); - + PetApi petApi = new PetApi(); + petApi.UpdatePetWithForm (petId, "new form name", "pending"); + + Pet response = petApi.GetPetById (petId); + Assert.IsInstanceOf(response); + Assert.IsInstanceOf(response.Category); + Assert.IsInstanceOf>(response.Tags); + + Assert.AreEqual ("new form name", response.Name); + Assert.AreEqual (Pet.StatusEnum.Pending, response.Status); + + Assert.AreEqual (petId, response.Tags [0].Id); + Assert.AreEqual (56, response.Category.Id); + + // test optional parameter + petApi.UpdatePetWithForm (petId, "new form name2"); + Pet response2 = petApi.GetPetById (petId); + Assert.AreEqual ("new form name2", response2.Name); } /// @@ -169,13 +279,45 @@ public void UpdatePetWithFormTest() [Test] public void UploadFileTest() { - // TODO uncomment below to test the method and replace null with proper value - //long? petId = null; - //string additionalMetadata = null; - //System.IO.Stream file = null; - //var response = instance.UploadFile(petId, additionalMetadata, file); - //Assert.IsInstanceOf (response, "response is ApiResponse"); + Assembly _assembly = Assembly.GetExecutingAssembly(); + Stream _imageStream = _assembly.GetManifestResourceStream("IO.Swagger.Test.swagger-logo.png"); + PetApi petApi = new PetApi (); + // test file upload with form parameters + petApi.UploadFile(petId, "new form name", _imageStream); + + // test file upload without any form parameters + // using optional parameter syntax introduced at .net 4.0 + petApi.UploadFile(petId: petId, file: _imageStream); + } + + /// + /// Test status code + /// + [Test ()] + public void TestStatusCodeAndHeader () + { + PetApi petApi = new PetApi (); + var response = petApi.GetPetByIdWithHttpInfo (petId); + Assert.AreEqual (response.StatusCode, 200); + Assert.IsTrue (response.Headers.ContainsKey("Content-Type")); + Assert.AreEqual (response.Headers["Content-Type"], "application/json"); + } + + /// + /// Test default header (should be deprecated + /// + [Test ()] + public void TestDefaultHeader () + { + PetApi petApi = new PetApi (); + // commented out the warning test below as it's confirmed the warning is working as expected + // there should be a warning for using AddDefaultHeader (deprecated) below + //petApi.AddDefaultHeader ("header_key", "header_value"); + // the following should be used instead as suggested in the doc + petApi.Configuration.AddDefaultHeader ("header_key2", "header_value2"); + + } }