Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -47,13 +25,59 @@ public class PetApiTests
{
private PetApi instance;

private long petId = 11088;

/// <summary>
/// Create a Pet object
/// </summary>
private Pet createPet()
{
// create pet
Pet p = new Pet(Name: "Csharp test", PhotoUrls: new List<string> { "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<String> photoUrls = new List<String>(new String[] {"sample photoUrls"});
// create Tag object
Tag tag = new Tag();
tag.Id = petId;
tag.Name = "csharp sample tag name1";
List<Tag> tags = new List<Tag>(new Tag[] {tag});
p.Tags = tags;
p.Category = category;
p.PhotoUrls = photoUrls;

return p;
}

/// <summary>
/// Convert string to byte array
/// </summary>
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;
}

/// <summary>
/// Setup before each unit test
/// </summary>
[SetUp]
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);
}

/// <summary>
Expand All @@ -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");
}

/// <summary>
Expand All @@ -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<PetApi>(instance);
}


Expand All @@ -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);
}

/// <summary>
Expand All @@ -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
}

/// <summary>
Expand All @@ -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<string> status = null;
//var response = instance.FindPetsByStatus(status);
//Assert.IsInstanceOf<List<Pet>> (response, "response is List<Pet>");
PetApi petApi = new PetApi ();
List<String> tagsList = new List<String>(new String[] {"available"});

List<Pet> listPet = petApi.FindPetsByTags (tagsList);
foreach (Pet pet in listPet) // Loop through List with foreach.
{
Assert.IsInstanceOf<Pet>(pet);
Assert.AreEqual ("csharp sample tag name1", pet.Tags[0]);
}
}

/// <summary>
Expand All @@ -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<string> tags = null;
//var response = instance.FindPetsByTags(tags);
//Assert.IsInstanceOf<List<Pet>> (response, "response is List<Pet>");
List<string> tags = new List<String>(new String[] {"pet"});
var response = instance.FindPetsByTags(tags);
Assert.IsInstanceOf<List<Pet>>(response);
}

/// <summary>
Expand All @@ -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<Pet> (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<Pet>(response);

Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);

Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);

Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);

Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}

/// <summary>
/// Test GetPetByIdAsync
/// </summary>
[Test ()]
public void TestGetPetByIdAsync ()
{
PetApi petApi = new PetApi ();
var task = petApi.GetPetByIdAsync (petId);
Pet response = task.Result;
Assert.IsInstanceOf<Pet>(response);

Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);

Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);

Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);

Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}

/// <summary>
/// Test GetPetByIdAsyncWithHttpInfo
/// </summary>
[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<Pet>(response);

Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);

Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);

Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);

Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}

/// <summary>
/// Test UpdatePet
/// </summary>
[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);
}

/// <summary>
Expand All @@ -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<Pet>(response);
Assert.IsInstanceOf<Category>(response.Category);
Assert.IsInstanceOf<List<Tag>>(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);
}

/// <summary>
Expand All @@ -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<ApiResponse> (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);

}

/// <summary>
/// Test status code
/// </summary>
[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");
}

/// <summary>
/// Test default header (should be deprecated
/// </summary>
[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");

}

}

Expand Down