Commit 0558881f authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

bring the shelf back. wip.

parent 2b85c85b
package info.textgrid.rep.basket;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/service/shelf")
public class BasketAjaxController {
@RequestMapping("/add")
public long addToBasket(
HttpSession session,
@RequestParam("uri") String textgridUri
) throws IOException {
List<String> basket;
if(session.getAttribute("basket") == null) {
basket = new ArrayList<String>();
} else {
basket = (List<String>) session.getAttribute("basket");
}
basket.add(textgridUri);
session.setAttribute("basket", basket);
return basket.size();
}
@RequestMapping("/remove")
public long removeFromBasket(
HttpSession session,
@RequestParam("uri") String textgridUri
) throws IOException {
List<String> basket = (List<String>) session.getAttribute("basket");
basket.remove(textgridUri);
session.setAttribute("basket", basket);
return basket.size();
}
@RequestMapping("/clear")
public long clearBasket(
HttpSession session
) throws IOException {
session.setAttribute("basket", new ArrayList<String>());
return basketCount(session);
}
@GetMapping("/count") //TODO: merge with basketAjaxController?
public long basketCount(HttpSession session) {
long count;
if (session.getAttribute("basket")== null) {
count = 0;
} else {
count = ((List<String>) session.getAttribute("basket")).size();
}
return count;
}
}
package info.textgrid.rep.search;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest
@AutoConfigureMockMvc
public class BasketControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void showBasket() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/shelf")
.accept(MediaType.TEXT_HTML))
.andExpect(view().name("basket"))
.andExpect(forwardedUrl("/WEB-INF/jsp/basket.jsp"))
.andExpect(status().isOk());
}
@Test
public void addToBasket() throws Exception {
final MockHttpSession mockHttpSession = new MockHttpSession();
// add alice
mvc.perform(MockMvcRequestBuilders.post("/service/shelf/add")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(mockHttpSession)
.param("uri", "textgrid:kv2q.0"))
// .accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andExpect(content().string("1"));
// add another
mvc.perform(MockMvcRequestBuilders.post("/service/shelf/add")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(mockHttpSession)
.param("uri", "textgrid:wfw9.0"))
// .accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andExpect(content().string("2"));
// view html
mvc.perform(MockMvcRequestBuilders.get("/shelf")
.session(mockHttpSession)
.accept(MediaType.TEXT_HTML))
.andExpect(view().name("basket"))
.andExpect(forwardedUrl("/WEB-INF/jsp/basket.jsp"))
.andExpect(status().isOk())
.andExpect(model().attribute("results", hasSize(2)))
.andExpect(model().attribute("basketItemString", is("textgrid:kv2q.0,textgrid:wfw9.0")));
//.andExpect(model().attribute("results", containsString("Alice im Wunderland")));
// remove one
mvc.perform(MockMvcRequestBuilders.post("/service/shelf/remove")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.session(mockHttpSession)
.param("uri", "textgrid:wfw9.0"))
// .accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andExpect(content().string("1"));
// clear shelf
mvc.perform(MockMvcRequestBuilders.post("/service/shelf/clear")
.session(mockHttpSession))
// .accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andExpect(content().string("0"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment