1 | /** | |
2 | * Copyright 2018 SmartBear Software | |
3 | * <p> | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * <p> | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * <p> | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package io.swagger.petstore.data; | |
18 | ||
19 | import io.swagger.petstore.model.Category; | |
20 | import io.swagger.petstore.model.Pet; | |
21 | import io.swagger.petstore.model.Pet0; | |
22 | import io.swagger.petstore.model.Pet1; | |
23 | import io.swagger.petstore.model.Tag; | |
24 | ||
25 | import java.util.ArrayList; | |
26 | import java.util.Arrays; | |
27 | import java.util.List; | |
28 | ||
29 | public class PetData { | |
30 | private static List<Pet> pets = new ArrayList<>(); | |
31 | private static List<Category> categories = new ArrayList<>(); | |
32 | ||
33 | static { | |
34 | categories.add(createCategory(1, "Dogs")); | |
35 | categories.add(createCategory(2, "Cats")); | |
36 | categories.add(createCategory(3, "Rabbits")); | |
37 | categories.add(createCategory(4, "Lions")); | |
38 | ||
39 | pets.add(createPet(1, categories.get(1), "Cat 1", new String[]{ | |
40 | "url1", "url2"}, new String[]{"tag1", "tag2"}, "available")); | |
41 | pets.add(createPet(2, categories.get(1), "Cat 2", new String[]{ | |
42 | "url1", "url2"}, new String[]{"tag2", "tag3"}, "available")); | |
43 | pets.add(createPet(3, categories.get(1), "Cat 3", new String[]{ | |
44 | "url1", "url2"}, new String[]{"tag3", "tag4"}, "pending")); | |
45 | ||
46 | pets.add(createPet(4, categories.get(0), "Dog 1", new String[]{ | |
47 | "url1", "url2"}, new String[]{"tag1", "tag2"}, "available")); | |
48 | pets.add(createPet(5, categories.get(0), "Dog 2", new String[]{ | |
49 | "url1", "url2"}, new String[]{"tag2", "tag3"}, "sold")); | |
50 | pets.add(createPet(6, categories.get(0), "Dog 3", new String[]{ | |
51 | "url1", "url2"}, new String[]{"tag3", "tag4"}, "pending")); | |
52 | ||
53 | pets.add(createPet(7, categories.get(3), "Lion 1", new String[]{ | |
54 | "url1", "url2"}, new String[]{"tag1", "tag2"}, "available")); | |
55 | pets.add(createPet(8, categories.get(3), "Lion 2", new String[]{ | |
56 | "url1", "url2"}, new String[]{"tag2", "tag3"}, "available")); | |
57 | pets.add(createPet(9, categories.get(3), "Lion 3", new String[]{ | |
58 | "url1", "url2"}, new String[]{"tag3", "tag4"}, "available")); | |
59 | ||
60 | pets.add(createPet(10, categories.get(2), "Rabbit 1", new String[]{ | |
61 | "url1", "url2"}, new String[]{"tag3", "tag4"}, "available")); | |
62 | } | |
63 | | |
64 | //Added methods for test | |
65 | | |
66 | public List<Pet> getPets() { | |
67 | return pets; | |
68 | } | |
69 | | |
70 | public List<Category> getCategories() { | |
71 | return categories; | |
72 | } | |
73 | | |
74 | public void deleteAll() { | |
75 | pets = new ArrayList<>(); | |
76 | categories = new ArrayList<>(); | |
77 | lastPetId=0; | |
78 | lastCategoryId=0; | |
79 | } | |
80 | ||
81 | public void addCategory(final Category category) { | |
82 | categories.add(category); | |
83 | } | |
84 | ||
85 | public void addPet0(final Pet0 pet) { | |
86 | if (pets.size() > 0) { | |
87 | for (int i = pets.size() - 1; i >= 0; i--) { | |
88 | if (pets.get(i).getId() == pet.getId()) { | |
89 | pets.remove(i); | |
90 | } | |
91 | } | |
92 | } | |
93 | pets.add(createPet(pet.getId(), pet.getCategory(), pet.getName(), new String[]{}, new String[]{}, pet.getStatus())); | |
94 | } | |
95 | ||
96 | public void addPet1(final Pet1 pet) { | |
97 | if (pets.size() > 0) { | |
98 | for (int i = pets.size() - 1; i >= 0; i--) { | |
99 | if (pets.get(i).getId() == pet.getId()) { | |
100 | pets.remove(i); | |
101 | } | |
102 | } | |
103 | } | |
104 | pets.add(createPet(pet.getId(), pet.getCategory(), pet.getName(), new String[]{}, new String[]{}, pet.getStatus())); | |
105 | } | |
106 | ||
107 | //added methods to test id generation in the backend | |
108 | | |
109 | private static long lastPetId=0; | |
110 | private static long lastCategoryId=0; | |
111 | | |
112 | public void addPetBackId(final Pet pet) { | |
113 | lastPetId +=1000; | |
114 | pet.setId(lastPetId); | |
115 | pets.add(pet); | |
116 | } | |
117 | ||
118 | public void addCategoryBackId(final Category category) { | |
119 | lastCategoryId += 10; | |
120 | category.setId(lastCategoryId); | |
121 | categories.add(category); | |
122 | } | |
123 | ||
124 | //End added methods for test | |
125 | ||
126 | public Pet getPetById(final long petId) { | |
127 | for (final Pet pet : pets) { | |
128 | if (pet.getId() == petId) { | |
129 | return pet; | |
130 | } | |
131 | } | |
132 | return null; | |
133 | } | |
134 | ||
135 | public List<Pet> findPetByStatus(final String status) { | |
136 | final String[] statues = status.split(","); | |
137 | final List<Pet> result = new ArrayList<>(); | |
138 | for (final Pet pet : pets) { | |
139 | for (final String s : statues) { | |
140 |
1
1. findPetByStatus : negated conditional → KILLED |
if (s.equals(pet.getStatus())) { |
141 | result.add(pet); | |
142 | } | |
143 | } | |
144 | } | |
145 |
1
1. findPetByStatus : replaced return value with Collections.emptyList for io/swagger/petstore/data/PetData::findPetByStatus → KILLED |
return result; |
146 | } | |
147 | ||
148 | public List<Pet> findPetByCategoryAndStatus(final String category, final String status) { | |
149 | final String[] statues = status.split(","); | |
150 | final List<Pet> result = new ArrayList<>(); | |
151 | for (final Pet pet : pets) { | |
152 | for (final String s : statues) { | |
153 |
2
1. findPetByCategoryAndStatus : negated conditional → KILLED 2. findPetByCategoryAndStatus : negated conditional → KILLED |
if (s.equals(pet.getStatus()) && category.equals(pet.getCategory().getName())) { |
154 | result.add(pet); | |
155 | } | |
156 | } | |
157 | } | |
158 |
1
1. findPetByCategoryAndStatus : replaced return value with Collections.emptyList for io/swagger/petstore/data/PetData::findPetByCategoryAndStatus → KILLED |
return result; |
159 | } | |
160 | ||
161 | public List<Pet> findPetByTags(final List<String> tags) { | |
162 | final List<Pet> result = new ArrayList<>(); | |
163 | for (final Pet pet : pets) { | |
164 | if (null != pet.getTags()) { | |
165 | for (final Tag tag : pet.getTags()) { | |
166 | for (final String tagListString : tags) { | |
167 | if (tagListString.equals(tag.getName())) { | |
168 | result.add(pet); | |
169 | } | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | return result; | |
175 | } | |
176 | ||
177 | public void addPet(final Pet pet) { | |
178 | if (pets.size() > 0) { | |
179 | for (int i = pets.size() - 1; i >= 0; i--) { | |
180 | if (pets.get(i).getId() == pet.getId()) { | |
181 | pets.remove(i); | |
182 | } | |
183 | } | |
184 | } | |
185 | pets.add(pet); | |
186 | } | |
187 | ||
188 | public void deletePetById(final Long petId) { | |
189 |
2
1. lambda$deletePetById$0 : negated conditional → NO_COVERAGE 2. lambda$deletePetById$0 : replaced boolean return with true for io/swagger/petstore/data/PetData::lambda$deletePetById$0 → NO_COVERAGE |
pets.removeIf(pet -> pet.getId() == petId); |
190 | } | |
191 | ||
192 | public static Pet createPet(final Long id, final Category cat, final String name, | |
193 | final List<String> urls, final List<Tag> tags, final String status) { | |
194 | final Pet pet = new Pet(); | |
195 | pet.setId(id); | |
196 | pet.setCategory(cat); | |
197 | pet.setName(name); | |
198 | pet.setPhotoUrls(urls); | |
199 | pet.setTags(tags); | |
200 | pet.setStatus(status); | |
201 | return pet; | |
202 | } | |
203 | ||
204 | private static Pet createPet(final long id, final Category cat, final String name, final String[] urls, | |
205 | final String[] tags, final String status) { | |
206 | final Pet pet = new Pet(); | |
207 | pet.setId(id); | |
208 | pet.setCategory(cat); | |
209 | pet.setName(name); | |
210 | if (null != urls) { | |
211 | final List<String> urlObjs = new ArrayList<>(Arrays.asList(urls)); | |
212 | pet.setPhotoUrls(urlObjs); | |
213 | } | |
214 | final List<Tag> tagObjs = new ArrayList<>(); | |
215 | int i = 0; | |
216 | if (null != tags) { | |
217 | for (final String tagString : tags) { | |
218 | i = i + 1; | |
219 | final Tag tag = new Tag(); | |
220 | tag.setId(i); | |
221 | tag.setName(tagString); | |
222 | tagObjs.add(tag); | |
223 | } | |
224 | } | |
225 | pet.setTags(tagObjs); | |
226 | pet.setStatus(status); | |
227 | return pet; | |
228 | } | |
229 | ||
230 | private static Category createCategory(final long id, final String name) { | |
231 | final Category category = new Category(); | |
232 | category.setId(id); | |
233 | category.setName(name); | |
234 | return category; | |
235 | } | |
236 | } | |
Mutations | ||
140 |
1.1 |
|
145 |
1.1 |
|
153 |
1.1 2.2 |
|
158 |
1.1 |
|
189 |
1.1 2.2 |