Add spring cloud sample tests back

This commit is contained in:
cbornet 2018-05-05 00:18:06 +02:00
parent 1619486578
commit 8405b18563
11 changed files with 458 additions and 0 deletions

View File

@ -796,6 +796,18 @@
<module>samples/server/petstore/springboot-beanvalidation</module>
</modules>
</profile>
<profile>
<id>springboot-reactive</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/server/petstore/springboot-reactive</module>
</modules>
</profile>
<profile>
<id>springboot</id>
<activation>

View File

@ -774,6 +774,18 @@
<module>samples/server/petstore/springboot-beanvalidation</module>
</modules>
</profile>
<profile>
<id>springboot-reactive</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/server/petstore/springboot-reactive</module>
</modules>
</profile>
<profile>
<id>springboot</id>
<activation>

View File

@ -780,6 +780,18 @@
<module>samples/server/petstore/springboot-beanvalidation</module>
</modules>
</profile>
<profile>
<id>springboot-reactive</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/server/petstore/springboot-reactive</module>
</modules>
</profile>
<profile>
<id>springboot</id>
<activation>

View File

@ -796,6 +796,18 @@
<module>samples/server/petstore/springboot-beanvalidation</module>
</modules>
</profile>
<profile>
<id>springboot-reactive</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/server/petstore/springboot-reactive</module>
</modules>
</profile>
<profile>
<id>springboot</id>
<activation>

View File

@ -790,6 +790,18 @@
<module>samples/server/petstore/springboot-beanvalidation</module>
</modules>
</profile>
<profile>
<id>springboot-reactive</id>
<activation>
<property>
<name>env</name>
<value>java</value>
</property>
</activation>
<modules>
<module>samples/server/petstore/springboot-reactive</module>
</modules>
</profile>
<profile>
<id>springboot</id>
<activation>

View File

@ -0,0 +1,20 @@
package org.openapitools;
import feign.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableFeignClients
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).run(args);
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

View File

@ -0,0 +1,17 @@
package org.openapitools;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
public class TestUtils {
private static final AtomicLong atomicId = createAtomicId();
public static long nextId() {
return atomicId.getAndIncrement();
}
private static AtomicLong createAtomicId() {
int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000;
return new AtomicLong((long) baseId);
}
}

View File

@ -0,0 +1,190 @@
package org.openapitools.api;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openapitools.Application;
import org.openapitools.TestUtils;
import org.openapitools.model.Category;
import org.openapitools.model.Pet;
import org.openapitools.model.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class PetApiTest {
@Autowired
private PetApiClient client;
@Test
public void testCreateAndGetPet() {
Pet pet = createRandomPet();
client.addPet(pet).execute();
Pet fetched = client.getPetById(pet.getId()).execute().getBody();
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
@Test
public void testUpdatePet() throws Exception {
Pet pet = createRandomPet();
pet.setName("programmer");
client.updatePet(pet).execute();
Pet fetched = client.getPetById(pet.getId()).execute().getBody();
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
@Ignore
@Test
public void testFindPetsByStatus() throws Exception {
Pet pet = createRandomPet();
pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
client.updatePet(pet).execute();
List<Pet> pets = client.findPetsByStatus(Collections.singletonList("available")).execute().getBody();
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
@Ignore
@Test
public void testFindPetsByTags() throws Exception {
Pet pet = createRandomPet();
pet.setName("monster");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<Tag> tags = new ArrayList<>();
Tag tag1 = new Tag();
tag1.setName("friendly");
tags.add(tag1);
pet.setTags(tags);
client.updatePet(pet).execute();
List<Pet> pets = client.findPetsByTags(Collections.singletonList("friendly")).execute().getBody();
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
@Test
public void testUpdatePetWithForm() throws Exception {
Pet pet = createRandomPet();
pet.setName("frank");
client.addPet(pet).execute();
Pet fetched = client.getPetById(pet.getId()).execute().getBody();
client.updatePetWithForm(fetched.getId(), "furt", null).execute();
Pet updated = client.getPetById(fetched.getId()).execute().getBody();
assertEquals(updated.getName(), "furt");
}
@Test
public void testDeletePet() throws Exception {
Pet pet = createRandomPet();
client.addPet(pet).execute();
Pet fetched = client.getPetById(pet.getId()).execute().getBody();
client.deletePet(fetched.getId(), null).execute();
try {
client.getPetById(fetched.getId()).execute();
fail("expected an error");
} catch (HystrixRuntimeException e) {
assertTrue(e.getCause().getMessage().startsWith("status 404 "));
}
}
@Ignore("Multipart form is not supported by spring-cloud yet.")
@Test
public void testUploadFile() throws Exception {
Pet pet = createRandomPet();
client.addPet(pet).execute();
MockMultipartFile filePart = new MockMultipartFile("file", "bar".getBytes());
client.uploadFile(pet.getId(), "a test file", filePart).execute();
}
@Test
public void testEqualsAndHashCode() {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
pet2.setName("really-happy");
pet2.setPhotoUrls(Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2"));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
assertTrue(pet2.hashCode() == pet2.hashCode());
pet1.setName("really-happy");
pet1.setPhotoUrls(Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2"));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();
category.setName("really-happy");
pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<String> photos = Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2");
pet.setPhotoUrls(photos);
return pet;
}
}

View File

@ -0,0 +1,72 @@
package org.openapitools.api;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openapitools.Application;
import org.openapitools.TestUtils;
import org.openapitools.model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.time.OffsetDateTime;
import java.util.Map;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class StoreApiTest {
@Autowired
private StoreApiClient client;
@Test
public void testGetInventory() {
Map<String, Integer> inventory = client.getInventory().execute().getBody();
assertTrue(inventory.keySet().size() > 0);
}
@Test
public void testPlaceOrder() {
Order order = createOrder();
client.placeOrder(order).execute();
Order fetched = client.getOrderById(order.getId()).execute().getBody();
assertEquals(order.getId(), fetched.getId());
assertEquals(order.getPetId(), fetched.getPetId());
assertEquals(order.getQuantity(), fetched.getQuantity());
assertEquals(order.getShipDate().toInstant(), fetched.getShipDate().toInstant());
}
@Test
public void testDeleteOrder() {
Order order = createOrder();
client.placeOrder(order).execute();
Order fetched = client.getOrderById(order.getId()).execute().getBody();
assertEquals(fetched.getId(), order.getId());
client.deleteOrder(String.valueOf(order.getId())).execute();
try {
client.getOrderById(order.getId()).execute();
fail("expected an error");
} catch (HystrixRuntimeException e) {
assertTrue(e.getCause().getMessage().startsWith("status 404 "));
}
}
private Order createOrder() {
return new Order()
.id(TestUtils.nextId())
.petId(200L)
.quantity(13)
//Ensure 3 fractional digits because of a bug in the petstore server
.shipDate(OffsetDateTime.now().withNano(123000000))
.status(Order.StatusEnum.PLACED)
.complete(true);
}
}

View File

@ -0,0 +1,88 @@
package org.openapitools.api;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openapitools.Application;
import org.openapitools.TestUtils;
import org.openapitools.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class UserApiTest {
@Autowired
private UserApiClient client;
@Test
public void testCreateUser() {
User user = createUser();
client.createUser(user).execute();
User fetched = client.getUserByName(user.getUsername()).execute().getBody();
assertEquals(user.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithArray() {
User user1 = createUser();
user1.setUsername("user" + user1.getId());
User user2 = createUser();
user2.setUsername("user" + user2.getId());
client.createUsersWithArrayInput(Arrays.asList(user1, user2)).execute();
User fetched = client.getUserByName(user1.getUsername()).execute().getBody();
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithList() {
User user1 = createUser();
user1.setUsername("user" + user1.getId());
User user2 = createUser();
user2.setUsername("user" + user2.getId());
client.createUsersWithListInput(Arrays.asList(user1, user2)).execute();
User fetched = client.getUserByName(user1.getUsername()).execute().getBody();
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testLoginUser() {
User user = createUser();
client.createUser(user).execute();
String token = client.loginUser(user.getUsername(), user.getPassword()).execute().getBody();
assertTrue(token.startsWith("logged in user session:"));
}
@Test
public void logoutUser() {
client.logoutUser().execute();
}
private User createUser() {
User user = new User();
user.setId(TestUtils.nextId());
user.setUsername("fred");
user.setFirstName("Fred");
user.setLastName("Meyer");
user.setEmail("fred@fredmeyer.com");
user.setPassword("xxXXxx");
user.setPhone("408-867-5309");
user.setUserStatus(123);
return user;
}
}

View File

@ -0,0 +1,11 @@
spring:
application:
name: petstore-test
jackson:
serialization.WRITE_DATES_AS_TIMESTAMPS: false
hystrix.command.default.execution.timeout.enabled: false
logging.level.io.swagger.api: DEBUG
feign.hystrix.enabled: true