Skip to content

Commit

Permalink
Move controllers and service into associated test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaggi committed Nov 25, 2024
1 parent 489d84f commit e9c2d78
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 328 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.hamcrest.Matchers.not;
import static org.hamcrest.collection.IsIterableContainingInRelativeOrder.containsInRelativeOrder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(TestController.class)
@WebMvcTest(HtmxHandlerInterceptorTest.TestController.class)
@ContextConfiguration(classes = HtmxHandlerInterceptorTest.TestController.class)
@WithMockUser
class HtmxHandlerInterceptorTest {

Expand Down Expand Up @@ -231,4 +237,176 @@ public void testHxReselect() throws Exception {
.andExpect(header().string("HX-Reselect", "#target"));
}

@Controller
@RequestMapping("/")
static class TestController {

@GetMapping("/without-trigger")
@ResponseBody
public String methodWithoutAnnotations() {
return "";
}

@GetMapping("/with-trigger")
@HxTrigger("eventTriggered")
@ResponseBody
public String methodWithHxTrigger() {
return "";
}

@GetMapping("/with-trigger-multiple-events")
@HxTrigger({ "event1", "event2" })
@ResponseBody
public String methodWithHxTriggerAndMultipleEvents() {
return "";
}

@GetMapping("/with-trigger-after-settle")
@HxTriggerAfterSettle("eventTriggered")
@ResponseBody
public String methodWithHxTriggerAfterSettle() {
return "";
}

@GetMapping("/with-trigger-after-settle-multiple-events")
@HxTriggerAfterSettle({ "event1", "event2" })
@ResponseBody
public String methodWithHxTriggerAfterSettleAndMultipleEvents() {
return "";
}

@GetMapping("/with-trigger-after-swap")
@HxTriggerAfterSwap("eventTriggered")
@ResponseBody
public String methodWithHxTriggerAfterSwap() {
return "";
}

@GetMapping("/with-trigger-after-swap-multiple-events")
@HxTriggerAfterSwap({ "event1", "event2" })
@ResponseBody
public String methodWithHxTriggerAfterSwapAndMultipleEvents() {
return "";
}

@GetMapping("/with-triggers")
@HxTrigger({ "event1", "event2" })
@HxTriggerAfterSettle({ "event1", "event2" })
@HxTriggerAfterSwap({ "event1", "event2" })
@ResponseBody
public String methodWithHxTriggers() {
return "";
}

@GetMapping("/updates-sidebar")
@HxUpdatesSidebar
@ResponseBody
public String updatesSidebar() {
return "";
}

@GetMapping("/hx-trigger-alias-for")
@HxTriggerWithAliasFor(event = "updateTrigger")
@ResponseBody
public String hxTriggerWithAliasForOverride() {
return "";
}

@GetMapping("/hx-refresh")
@HxRefresh
@ResponseBody
public String hxRefresh() {
return "";
}

@GetMapping("/hx-vary")
@ResponseBody
public String hxVary() {
return "";
}

@GetMapping("/hx-location-without-context-data")
@HxLocation("/path")
@ResponseBody
public String hxLocationWithoutContextData() {
return "";
}

@GetMapping("/hx-location-with-context-data")
@HxLocation(path = "/path", source = "source", event = "event", handler = "handler", target = "target", swap = "swap", select = "select")
@ResponseBody
public String hxLocationWithContextData() {
return "";
}

@GetMapping("/hx-push-url-path")
@HxPushUrl("/path")
@ResponseBody
public String hxPushUrlPath() {
return "";
}

@GetMapping("/hx-push-url")
@HxPushUrl
@ResponseBody
public String hxPushUrl() {
return "";
}

@GetMapping("/hx-push-url-false")
@HxPushUrl(HtmxValue.FALSE)
@ResponseBody
public String hxPushUrlFalse() {
return "";
}

@GetMapping("/hx-redirect")
@HxRedirect("/path")
@ResponseBody
public String hxRedirect() {
return "";
}

@GetMapping("/hx-replace-url-path")
@HxReplaceUrl("/path")
@ResponseBody
public String hxReplaceUrlPath() {
return "";
}
@GetMapping("/hx-replace-url")
@HxReplaceUrl
@ResponseBody
public String hxReplaceUrl() {
return "";
}
@GetMapping("/hx-replace-url-false")
@HxReplaceUrl(HtmxValue.FALSE)
@ResponseBody
public String hxReplaceUrlFalse() {
return "";
}

@GetMapping("/hx-reswap")
@HxReswap(value = HxSwapType.INNER_HTML, swap = 300)
@ResponseBody
public String hxReswap() {
return "";
}

@GetMapping("/hx-retarget")
@HxRetarget("#target")
@ResponseBody
public String hxRetarget() {
return "";
}

@GetMapping("/hx-reselect")
@HxReselect("#target")
@ResponseBody
public String hxReselect() {
return "";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;


@WebMvcTest(HtmxHandlerMethodArgumentResolverTestController.class)
@WebMvcTest(HtmxHandlerMethodArgumentResolverTest.TestController.class)
@ContextConfiguration(classes = HtmxHandlerMethodArgumentResolverTest.TestController.class)
@WithMockUser
class HtmxHandlerMethodArgumentResolverTest {
@Autowired
Expand Down Expand Up @@ -165,21 +172,70 @@ void testHxRequestAnnotation() throws Exception {
mockMvc.perform(get("/method-arg-resolver/users")
.header("HX-Request", "true"))
.andExpect(view().name("users :: list"));
;
}

@Test
void testHxRequestAnnotationInheritance() throws Exception {
mockMvc.perform(get("/method-arg-resolver/users/inherited")
.header("HX-Request", "true"))
.andExpect(view().name("users :: list"));
;
}

@Test
void testHxRequestSameUrlNoAnnotation() throws Exception {
mockMvc.perform(get("/method-arg-resolver/users"))
.andExpect(view().name("users"));
;
}

@Controller
@RequestMapping("/method-arg-resolver")
static class TestController {

@Autowired
private TestService service;

@GetMapping
@ResponseBody
public String htmxRequestDetails(HtmxRequest details) {
service.doSomething(details);

return "";
}

@GetMapping("/users")
@HxRequest
public String htmxRequest(HtmxRequest details) {
service.doSomething(details);

return "users :: list";
}

@GetMapping("/users")
public String normalRequest(HtmxRequest details) {
service.doSomething(details);

return "users";
}

@HxGetMapping("/users/inherited")
public String htmxRequestInheritance(HtmxRequest details) {
service.doSomething(details);

return "users :: list";
}

@GetMapping("/users/inherited")
public String normalRequestInheritance(HtmxRequest details) {
service.doSomething(details);

return "users";
}
}

@Service
public class TestService {
void doSomething(HtmxRequest details) {
}
}

}

This file was deleted.

Loading

0 comments on commit e9c2d78

Please sign in to comment.