Skip to content
Snippets Groups Projects
Commit 8d1efb24 authored by Dapeng's avatar Dapeng
Browse files

optimize static resources

parent 12351e40
No related branches found
No related tags found
No related merge requests found
......@@ -17,4 +17,10 @@ spring.messages.basename=messages/messages
management.contextPath=/manage
# Logging
logging.level.org.springframework=INFO
\ No newline at end of file
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
\ No newline at end of file
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();
}
}
\ No newline at end of file
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;
}
}
package org.springframework.samples.petclinic.web;
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.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.test.web.servlet.ResultActions;
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.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* 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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment