mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-17 13:05:49 +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
|
management.contextPath=/manage
|
||||||
|
|
||||||
# Logging
|
# 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;
|
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.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
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.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import org.junit.Before;
|
@RunWith(SpringRunner.class)
|
||||||
import org.junit.Test;
|
@WebMvcTest(PetResource.class)
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
public class PetResourceTests {
|
||||||
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 {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PetResource petResource;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@Before
|
@MockBean
|
||||||
public void setup() {
|
ClinicService clinicService;
|
||||||
runMockSpringMVC(petResource);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Expected JSon result:
|
|
||||||
* {
|
|
||||||
"id":2,
|
|
||||||
"name":"Basil",
|
|
||||||
"birthDate":1344211200000,
|
|
||||||
"type":{
|
|
||||||
"id":6,
|
|
||||||
"name":"hamster",
|
|
||||||
"new":false
|
|
||||||
},
|
|
||||||
"visits":[],
|
|
||||||
"new":false
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetAPetInJSonFormat() throws Exception {
|
public void shouldGetAPetInJSonFormat() throws Exception {
|
||||||
ResultActions actions = mockMvc.perform(get("/owner/2/pet/2.json").accept(MediaType.APPLICATION_JSON))
|
|
||||||
.andExpect(status().isOk());
|
Pet pet = setupPet();
|
||||||
actions.andExpect(content().contentType("application/json;charset=UTF-8"))
|
|
||||||
|
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("$.id").value(2))
|
||||||
.andExpect(jsonPath("$.name").value("Basil"))
|
.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;
|
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.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.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
import org.junit.Before;
|
@RunWith(SpringRunner.class)
|
||||||
import org.junit.Test;
|
@WebMvcTest(VetResource.class)
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
public class VetResourceTests {
|
||||||
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 {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private VetResource vetResource;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@Before
|
@MockBean
|
||||||
public void setup() {
|
ClinicService clinicService;
|
||||||
runMockSpringMVC(vetResource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetAListOfVetsInJSonFormat() throws Exception {
|
public void shouldGetAListOfVetsInJSonFormat() throws Exception {
|
||||||
ResultActions actions = mockMvc.perform(get("/vets.json").accept(MediaType.APPLICATION_JSON))
|
|
||||||
.andExpect(status().isOk());
|
Vet vet = new Vet();
|
||||||
actions.andExpect(content().contentType("application/json"))
|
vet.setId(1);
|
||||||
.andExpect(jsonPath("$[0].id").value(1));
|
|
||||||
//before when collection was nested inside parent object 'vetList', we had: $.vetList[0].id
|
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