1// Copyright 2015 go-swagger maintainers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package spec
16
17import (
18	"encoding/json"
19	"testing"
20
21	"github.com/go-openapi/swag"
22	"github.com/stretchr/testify/assert"
23)
24
25var items = Items{
26	Refable: Refable{Ref: MustCreateRef("Dog")},
27	CommonValidations: CommonValidations{
28		Maximum:          float64Ptr(100),
29		ExclusiveMaximum: true,
30		ExclusiveMinimum: true,
31		Minimum:          float64Ptr(5),
32		MaxLength:        int64Ptr(100),
33		MinLength:        int64Ptr(5),
34		Pattern:          "\\w{1,5}\\w+",
35		MaxItems:         int64Ptr(100),
36		MinItems:         int64Ptr(5),
37		UniqueItems:      true,
38		MultipleOf:       float64Ptr(5),
39		Enum:             []interface{}{"hello", "world"},
40	},
41	SimpleSchema: SimpleSchema{
42		Type:   "string",
43		Format: "date",
44		Items: &Items{
45			Refable: Refable{Ref: MustCreateRef("Cat")},
46		},
47		CollectionFormat: "csv",
48		Default:          "8",
49	},
50}
51
52const itemsJSON = `{
53	"items": {
54		"$ref": "Cat"
55	},
56  "$ref": "Dog",
57  "maximum": 100,
58  "minimum": 5,
59  "exclusiveMaximum": true,
60  "exclusiveMinimum": true,
61  "maxLength": 100,
62  "minLength": 5,
63  "pattern": "\\w{1,5}\\w+",
64  "maxItems": 100,
65  "minItems": 5,
66  "uniqueItems": true,
67  "multipleOf": 5,
68  "enum": ["hello", "world"],
69  "type": "string",
70  "format": "date",
71	"collectionFormat": "csv",
72	"default": "8"
73}`
74
75func TestIntegrationItems(t *testing.T) {
76	var actual Items
77	if assert.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual)) {
78		assert.EqualValues(t, actual, items)
79	}
80
81	assertParsesJSON(t, itemsJSON, items)
82}
83
84func TestTypeNameItems(t *testing.T) {
85	var nilItems Items
86	assert.Equal(t, "", nilItems.TypeName())
87
88	assert.Equal(t, "date", items.TypeName())
89	assert.Equal(t, "", items.ItemsTypeName())
90
91	nested := Items{
92		SimpleSchema: SimpleSchema{
93			Type: "array",
94			Items: &Items{
95				SimpleSchema: SimpleSchema{
96					Type:   "integer",
97					Format: "int32",
98				},
99			},
100			CollectionFormat: "csv",
101		},
102	}
103
104	assert.Equal(t, "array", nested.TypeName())
105	assert.Equal(t, "int32", nested.ItemsTypeName())
106
107	simple := SimpleSchema{
108		Type:  "string",
109		Items: nil,
110	}
111
112	assert.Equal(t, "string", simple.TypeName())
113	assert.Equal(t, "", simple.ItemsTypeName())
114
115	simple.Items = NewItems()
116	simple.Type = "array"
117	simple.Items.Type = "string"
118
119	assert.Equal(t, "array", simple.TypeName())
120	assert.Equal(t, "string", simple.ItemsTypeName())
121}
122
123func TestItemsBuilder(t *testing.T) {
124	simple := SimpleSchema{
125		Type: "array",
126		Items: NewItems().
127			Typed("string", "uuid").
128			WithDefault([]string{"default-value"}).
129			WithEnum([]string{"abc", "efg"}, []string{"hij"}).
130			WithMaxItems(4).
131			WithMinItems(1).
132			UniqueValues(),
133	}
134
135	assert.Equal(t, SimpleSchema{
136		Type: "array",
137		Items: &Items{
138			SimpleSchema: SimpleSchema{
139				Type:    "string",
140				Format:  "uuid",
141				Default: []string{"default-value"},
142			},
143			CommonValidations: CommonValidations{
144				Enum:        []interface{}{[]string{"abc", "efg"}, []string{"hij"}},
145				MinItems:    swag.Int64(1),
146				MaxItems:    swag.Int64(4),
147				UniqueItems: true,
148			},
149		},
150	}, simple)
151}
152
153func TestJSONLookupItems(t *testing.T) {
154	res, err := items.JSONLookup("$ref")
155	if !assert.NoError(t, err) {
156		t.FailNow()
157		return
158	}
159	if assert.IsType(t, &Ref{}, res) {
160		ref := res.(*Ref)
161		assert.EqualValues(t, MustCreateRef("Dog"), *ref)
162	}
163
164	var max *float64
165	res, err = items.JSONLookup("maximum")
166	if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, max, res) {
167		t.FailNow()
168		return
169	}
170	max = res.(*float64)
171	assert.Equal(t, float64(100), *max)
172
173	var f string
174	res, err = items.JSONLookup("collectionFormat")
175	if !assert.NoError(t, err) || !assert.NotNil(t, res) || !assert.IsType(t, f, res) {
176		t.FailNow()
177		return
178	}
179	f = res.(string)
180	assert.Equal(t, "csv", f)
181
182	res, err = items.JSONLookup("unknown")
183	if !assert.Error(t, err) || !assert.Nil(t, res) {
184		t.FailNow()
185		return
186	}
187}
188