CatnapWrappingView implements View, WrappingView<CatnapView> { public Object extractResult(Map<String, ?> model) { if (model != null && !model.isEmpty()) { if (model.size() == 1) { return model.values().iterator().next(); } else { if (model.containsKey(CatnapResponseBodyHandlerInterceptor.MODEL_NAME)) { return model.get(CatnapResponseBodyHandlerInterceptor.MODEL_NAME); } else { return model.get(getModelName()); } } } return null; } CatnapWrappingView(final CatnapView view);  CatnapWrappingView(final CatnapView view, final String modelName); @Override void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response); void setContentTypeHeader(HttpServletResponse response); com.github.gregwhitaker.catnap.core.context.HttpStatus httpStatus(HttpServletRequest request); Object extractResult(Map<String, ?> model); @Override String getContentType(); @Override String getCharacterEncoding(); @Override CatnapView getWrappedView(); @Override String getModelName(); static final String DEFAULT_MODELNAME; }
@Test public void extractModelFromSingleResultModel() { Map<String, Object> modelMap = new HashMap<String, Object>(1); modelMap.put("model", new BeanModel()); CatnapWrappingView view = new CatnapWrappingView(new JsonView()); assertTrue(view.extractResult(modelMap) instanceof BeanModel); }
@Test public void extractModelWithoutSpecifiedModelName() { Map<String, Object> modelMap = new HashMap<>(2); modelMap.put("params", new Object()); modelMap.put(CatnapWrappingView.DEFAULT_MODELNAME, new BeanModel()); CatnapWrappingView view = new CatnapWrappingView(new JsonView()); assertTrue(view.extractResult(modelMap) instanceof BeanModel); }
@Test public void extractModelWithModelName() { Map<String, Object> modelMap = new HashMap<>(2); modelMap.put("params", new Object()); modelMap.put("customModelName", new BeanModel()); CatnapWrappingView view = new CatnapWrappingView(new JsonView(), "customModelName"); assertTrue(view.extractResult(modelMap) instanceof BeanModel); }
@Test public void extractModelWithHandlerInterceptorDefaultName() { Map<String, Object> modelMap = new HashMap<>(2); modelMap.put("params", new Object()); modelMap.put(CatnapResponseBodyHandlerInterceptor.MODEL_NAME, new BeanModel()); CatnapWrappingView view = new CatnapWrappingView(new JsonView()); assertTrue(view.extractResult(modelMap) instanceof BeanModel); }
@Test public void extractEmptyModelReturnsNull() { assertNull(new CatnapWrappingView(new JsonView()).extractResult(new HashMap<>())); }
@Test public void extractNullModelReturnsNull() { assertNull(new CatnapWrappingView(new JsonView()).extractResult(null)); }