Skip to content

Commit

Permalink
tools/importer-rest-api-specs: updating the swagger tags tests to u…
Browse files Browse the repository at this point in the history
…se the new assertion funcs
  • Loading branch information
tombuildsstuff committed Feb 19, 2024
1 parent 4152219 commit 344c1c2
Showing 1 changed file with 165 additions and 95 deletions.
260 changes: 165 additions & 95 deletions tools/importer-rest-api-specs/components/parser/swagger_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,183 @@

package parser

import "testing"
import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/pandora/tools/importer-rest-api-specs/models"
)

func TestParsingOperationsUsingTheSameSwaggerTagInDifferentCasings(t *testing.T) {
result, err := ParseSwaggerFileForTesting(t, "operations_single_tag_different_casing.json")
actual, err := ParseSwaggerFileForTesting(t, "operations_single_tag_different_casing.json")
if err != nil {
t.Fatalf("parsing: %+v", err)
}
if result == nil {
t.Fatal("result was nil")
}
if len(result.Resources) != 1 {
t.Fatalf("expected 1 resource but got %d", len(result.Resources))
}

resource, ok := result.Resources["Hello"]
if !ok {
t.Fatal("the Resource 'Hello' was not found")
}
expected := models.AzureApiDefinition{
ServiceName: "Example",
ApiVersion: "2020-01-01",
Resources: map[string]models.AzureApiResource{
"Hello": {
Models: map[string]models.ModelDetails{
"Example": {
Fields: map[string]models.FieldDetails{
"Name": {
JsonName: "name",
ObjectDefinition: &models.ObjectDefinition{
Type: models.ObjectDefinitionString,
},
Required: false,
},
},
},
},
Operations: map[string]models.OperationDetails{

// sanity checking
if len(resource.Constants) != 0 {
t.Fatalf("expected 0 constants but got %d", len(resource.Constants))
}
if len(resource.Models) != 1 {
t.Fatalf("expected 1 model but got %d", len(resource.Models))
}
if len(resource.Operations) != 4 {
t.Fatalf("expected 4 Operations but got %d", len(resource.Operations))
}
if len(resource.ResourceIds) != 0 {
t.Fatalf("expected 0 Resource IDs but got %d", len(resource.ResourceIds))
}
expectedOperations := []string{
"First",
"PutBar",
"PutFoo",
"Second",
}
for _, expected := range expectedOperations {
if _, ok := resource.Operations[expected]; !ok {
t.Fatalf("expected there to be an operation named %q but didn't get one", expected)
}
}
"First": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PUT",
OperationId: "Hello_First",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
// https://github.com/hashicorp/pandora/issues/3807
UriSuffix: pointer.To("/someotheruri"),
},
"PutBar": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PUT",
OperationId: "Hello_PutBar",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
UriSuffix: pointer.To("/bar"),
},
"PutFoo": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PUT",
OperationId: "hello_PutFoo",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
UriSuffix: pointer.To("/foo"),
},
"Second": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PATCH",
OperationId: "hello_Second",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
// https://github.com/hashicorp/pandora/issues/3807
UriSuffix: pointer.To("/someotheruri"),
},
},
},
},
}
validateParsedSwaggerResultMatches(t, expected, actual)
}

func TestParsingOperationsOnResources(t *testing.T) {
result, err := ParseSwaggerFileForTesting(t, "operations_on_resources.json")
actual, err := ParseSwaggerFileForTesting(t, "operations_on_resources.json")
if err != nil {
t.Fatalf("parsing: %+v", err)
}
if result == nil {
t.Fatal("result was nil")
}
if len(result.Resources) != 2 {
t.Fatalf("expected 2 resource but got %d", len(result.Resources))
}

resource, ok := result.Resources["Hello"]
if !ok {
t.Fatal("the Resource 'Hello' was not found")
}

// sanity checking
if len(resource.Constants) != 0 {
t.Fatalf("expected 0 constants but got %d", len(resource.Constants))
}
if len(resource.Models) != 1 {
t.Fatalf("expected 1 model but got %d", len(resource.Models))
}
if len(resource.Operations) != 3 {
t.Fatalf("expected 3 Operations but got %d", len(resource.Operations))
}
if len(resource.ResourceIds) != 0 {
t.Fatalf("expected 0 Resource IDs but got %d", len(resource.ResourceIds))
}
expectedOperations := []string{
"First",
"PutBar",
"Second",
}
for _, expected := range expectedOperations {
if _, ok := resource.Operations[expected]; !ok {
t.Fatalf("expected there to be an operation named %q but didn't get one", expected)
}
}

resourceOperation, ok := result.Resources["HelloOperations"]
if !ok {
t.Fatal("the Resource 'HelloOperations' was not found")
}

// sanity checking
if len(resourceOperation.Constants) != 0 {
t.Fatalf("expected 0 constants but got %d", len(resource.Constants))
}
if len(resourceOperation.Models) != 1 {
t.Fatalf("expected 1 model but got %d", len(resource.Models))
}
if len(resourceOperation.Operations) != 1 {
t.Fatalf("expected 1 Operations but got %d", len(resource.Operations))
}
if len(resourceOperation.ResourceIds) != 0 {
t.Fatalf("expected 0 Resource IDs but got %d", len(resource.ResourceIds))
}

if _, ok := resourceOperation.Operations["HelloRestart"]; !ok {
t.Fatalf("expected there to be an operation named `Restart` but didn't get one")
}

expected := models.AzureApiDefinition{
ServiceName: "Example",
ApiVersion: "2020-01-01",
Resources: map[string]models.AzureApiResource{
"Hello": {
Models: map[string]models.ModelDetails{
"Example": {
Fields: map[string]models.FieldDetails{
"Name": {
JsonName: "name",
ObjectDefinition: &models.ObjectDefinition{
Type: models.ObjectDefinitionString,
},
Required: false,
},
},
},
},
Operations: map[string]models.OperationDetails{
"First": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PUT",
OperationId: "Hello_First",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
// https://github.com/hashicorp/pandora/issues/3807
UriSuffix: pointer.To("/someotheruri"),
},
"PutBar": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PUT",
OperationId: "Hello_PutBar",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
UriSuffix: pointer.To("/bar"),
},
"Second": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "PATCH",
OperationId: "hello_Second",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
// https://github.com/hashicorp/pandora/issues/3807
UriSuffix: pointer.To("/someotheruri"),
},
},
},
"HelloOperations": {
Models: map[string]models.ModelDetails{
"Example": {
Fields: map[string]models.FieldDetails{
"Name": {
JsonName: "name",
ObjectDefinition: &models.ObjectDefinition{
Type: models.ObjectDefinitionString,
},
Required: false,
},
},
},
},
Operations: map[string]models.OperationDetails{
"HelloRestart": {
ContentType: "application/json",
ExpectedStatusCodes: []int{200},
Method: "POST",
OperationId: "Hello_Restart",
RequestObject: &models.ObjectDefinition{
ReferenceName: pointer.To("Example"),
Type: models.ObjectDefinitionReference,
},
UriSuffix: pointer.To("/foo"),
},
},
},
},
}
validateParsedSwaggerResultMatches(t, expected, actual)
}

0 comments on commit 344c1c2

Please sign in to comment.