CatnapViewResolver implements ViewResolver { @Override public View resolveViewName(String viewName, Locale locale) throws Exception { if (views.isEmpty()) { logger.warn("No views configured for view resolver [{}]", getClass().getName()); } View view = null; if (view == null) { view = resolveViewWithHrefSuffix(viewName, locale); } if (view == null) { view = resolveViewWithAcceptHeader(viewName, locale); } return view; } CatnapViewResolver();  CatnapViewResolver(List<CatnapWrappingView> views); @Override View resolveViewName(String viewName, Locale locale); void setViews(List<CatnapWrappingView> views);  }
@Test public void resolveViewByHrefSuffix() throws Exception { CatnapWrappingView view = (CatnapWrappingView) viewResolver.resolveViewName("/view.json", Locale.US); assertTrue(view.getWrappedView() instanceof JsonView); }
@Test public void resolveViewByAcceptHeader() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept", "application/json"); RequestAttributes requestAttributes = new ServletRequestAttributes(request); requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST); RequestContextHolder.setRequestAttributes(requestAttributes); CatnapWrappingView view = (CatnapWrappingView) viewResolver.resolveViewName("/view", Locale.US); assertTrue(view.getWrappedView() instanceof JsonView); }
@Test public void resolveViewByInvalidHrefSuffixReturnsNull() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST); RequestContextHolder.setRequestAttributes(requestAttributes); assertNull(viewResolver.resolveViewName("/view.xml", Locale.US)); }
@Test public void resolveViewByInvalidAcceptHeaderReturnsNull() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addHeader("Accept", "application/xml"); RequestAttributes requestAttributes = new ServletRequestAttributes(request); requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST); RequestContextHolder.setRequestAttributes(requestAttributes); assertNull(viewResolver.resolveViewName("/view", Locale.US)); }
@Test public void noConfiguredViewsReturnsNull() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); requestAttributes.setAttribute(RequestAttributes.REFERENCE_REQUEST, request, RequestAttributes.SCOPE_REQUEST); RequestContextHolder.setRequestAttributes(requestAttributes); assertNull(new CatnapViewResolver().resolveViewName("/view.json", Locale.US)); }