mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 04:55:50 +00:00
optimize static resources
This commit is contained in:
parent
12351e40ec
commit
8d1efb240b
4 changed files with 87 additions and 90 deletions
|
@ -17,4 +17,10 @@ spring.messages.basename=messages/messages
|
|||
management.contextPath=/manage
|
||||
|
||||
# Logging
|
||||
logging.level.org.springframework=INFO
|
||||
logging.level.org.springframework=INFO
|
||||
|
||||
spring.resources.chain.enabled=true
|
||||
spring.resources.chain.gzipped=true
|
||||
spring.resources.cache-period=600
|
||||
spring.resources.chain.strategy.content.enabled=true
|
||||
spring.resources.chain.strategy.content.paths=/**/*.js, /**/*.css, /**/*.png, /**/*.html
|
|
@ -1,28 +0,0 @@
|
|||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration({"classpath:spring/business-config.xml", "classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})
|
||||
@WebAppConfiguration
|
||||
@ActiveProfiles("spring-data-jpa")
|
||||
|
||||
public abstract class AbstractWebResourceTests {
|
||||
|
||||
protected MockMvc mockMvc;
|
||||
|
||||
public void runMockSpringMVC(Object resource) {
|
||||
this.mockMvc = MockMvcBuilders.standaloneSetup(resource).build();
|
||||
}
|
||||
|
||||
public AbstractWebResourceTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,53 +1,64 @@
|
|||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.model.Owner;
|
||||
import org.springframework.samples.petclinic.model.Pet;
|
||||
import org.springframework.samples.petclinic.model.PetType;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
/**
|
||||
* Test class for the UserResource REST controller.
|
||||
*
|
||||
* @see UserResource
|
||||
*/
|
||||
public class PetResourceTests extends AbstractWebResourceTests {
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(PetResource.class)
|
||||
public class PetResourceTests {
|
||||
|
||||
@Autowired
|
||||
private PetResource petResource;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
runMockSpringMVC(petResource);
|
||||
}
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
ClinicService clinicService;
|
||||
|
||||
/**
|
||||
* Expected JSon result:
|
||||
* {
|
||||
"id":2,
|
||||
"name":"Basil",
|
||||
"birthDate":1344211200000,
|
||||
"type":{
|
||||
"id":6,
|
||||
"name":"hamster",
|
||||
"new":false
|
||||
},
|
||||
"visits":[],
|
||||
"new":false
|
||||
}
|
||||
*/
|
||||
@Test
|
||||
public void shouldGetAPetInJSonFormat() throws Exception {
|
||||
ResultActions actions = mockMvc.perform(get("/owner/2/pet/2.json").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
actions.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||
|
||||
Pet pet = setupPet();
|
||||
|
||||
given(clinicService.findPetById(2)).willReturn(pet);
|
||||
|
||||
|
||||
mvc.perform(get("/owner/2/pet/2.json").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType("application/json;charset=UTF-8"))
|
||||
.andExpect(jsonPath("$.id").value(2))
|
||||
.andExpect(jsonPath("$.name").value("Basil"))
|
||||
.andExpect(jsonPath("$.type.id").value(6));
|
||||
.andExpect(jsonPath("$.type.id").value(6));
|
||||
}
|
||||
|
||||
private Pet setupPet() {Owner owner = new Owner();
|
||||
owner.setFirstName("George");
|
||||
owner.setLastName("Bush");
|
||||
|
||||
Pet pet = new Pet();
|
||||
|
||||
pet.setName("Basil");
|
||||
pet.setId(2);
|
||||
|
||||
PetType petType = new PetType();
|
||||
petType.setId(6);
|
||||
pet.setType(petType);
|
||||
|
||||
owner.addPet(pet);
|
||||
return pet;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,37 +1,45 @@
|
|||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.samples.petclinic.model.Vet;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
/**
|
||||
* Test class for the UserResource REST controller.
|
||||
*
|
||||
* @see UserResource
|
||||
*/
|
||||
public class VetResourceTests extends AbstractWebResourceTests {
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(VetResource.class)
|
||||
public class VetResourceTests {
|
||||
|
||||
@Autowired
|
||||
private VetResource vetResource;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
runMockSpringMVC(vetResource);
|
||||
}
|
||||
private MockMvc mvc;
|
||||
|
||||
@MockBean
|
||||
ClinicService clinicService;
|
||||
|
||||
@Test
|
||||
public void shouldGetAListOfVetsInJSonFormat() throws Exception {
|
||||
ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
actions.andExpect(content().contentType("application/json"))
|
||||
.andExpect(jsonPath("$[0].id").value(1));
|
||||
//before when collection was nested inside parent object 'vetList', we had: $.vetList[0].id
|
||||
|
||||
Vet vet = new Vet();
|
||||
vet.setId(1);
|
||||
|
||||
given(clinicService.findVets()).willReturn(Arrays.asList(vet));
|
||||
|
||||
mvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].id").value(1));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue