@Test
  public void verifyOK() throws Exception {
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal p = org.jasig.cas.authentication.TestUtils.getPrincipal(ID, map);
    final TicketGrantingTicketImpl impl =
        new TicketGrantingTicketImpl(
            TGT_ID,
            org.jasig.cas.authentication.TestUtils.getAuthentication(p),
            new NeverExpiresExpirationPolicy());

    ((OAuth20WrapperController) oauth20WrapperController)
        .getTicketRegistry()
        .addTicket(
            new ServiceTicketImpl(
                CODE,
                impl,
                org.jasig.cas.authentication.TestUtils.getService(),
                false,
                new ExpirationPolicy() {
                  private static final long serialVersionUID = -7321055962209199811L;

                  @Override
                  public boolean isExpired(final TicketState ticketState) {
                    return false;
                  }
                }));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);

    ((OAuth20WrapperController) oauth20WrapperController).getTicketRegistry().deleteTicket(CODE);

    assertEquals("text/plain", mockResponse.getContentType());
    assertEquals(200, mockResponse.getStatus());
    final String body = mockResponse.getContentAsString();

    assertTrue(
        body.startsWith(
            OAuthConstants.ACCESS_TOKEN + '=' + TGT_ID + '&' + OAuthConstants.EXPIRES + '='));
    // delta = 2 seconds
    final int delta = 2;
    final int timeLeft =
        Integer.parseInt(StringUtils.substringAfter(body, '&' + OAuthConstants.EXPIRES + '='));
    assertTrue(timeLeft >= TIMEOUT - 10 - delta);
  }
  @Test
  public void verifyOKWithState() throws Exception {
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.addParameter(OAuthConstants.TICKET, SERVICE_TICKET);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_CALLBACKURL, REDIRECT_URI);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_STATE, STATE);
    mockRequest.setSession(mockSession);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final ModelAndView modelAndView =
        oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(OAuthConstants.CONFIRM_VIEW, modelAndView.getViewName());
    final Map<String, Object> map = modelAndView.getModel();
    assertEquals(SERVICE_NAME, map.get("serviceName"));
    assertEquals(
        REDIRECT_URI
            + "?"
            + OAuthConstants.CODE
            + "="
            + SERVICE_TICKET
            + "&"
            + OAuthConstants.STATE
            + "="
            + STATE,
        map.get("callbackUrl"));
  }
  public void testProcess() throws Exception {

    Controller c = (Controller) applicationContext.getBean("peopleController");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    HttpServletResponse response = new MockHttpServletResponse();

    request.addParameter("importStudents", "Import Students");

    ModelAndView mav =
        c.handleRequest((HttpServletRequest) request, (HttpServletResponse) response);
    Map m = mav.getModel();
    PeopleBacking p = (PeopleBacking) m.get("peopleBacking");

    MockPageContext ctx = new MockPageContext();
    ctx.setAttribute("peopleBacking", mav.getModel().get("peopleBacking"));
    request.setAttribute("peopleBacking", mav.getModel().get("peopleBacking"));

    List<ProcessErrors> errors =
        (List<ProcessErrors>)
            ExpressionEvaluationUtils.evaluate(
                "test", "${peopleBacking.processStudentsErrors}", List.class, ctx);

    for (ProcessErrors e : errors) {
      ctx.setAttribute("processErrors", e);
      log.debug(
          ExpressionEvaluationUtils.evaluate(
              "test", "${processErrors.message}", String.class, ctx));
    }
  }
 @Test
 public void verifyNoClientSecret() throws Exception {
   final MockHttpServletRequest mockRequest =
       new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
   mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
   mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
   mockRequest.setParameter(OAuthConstants.CODE, CODE);
   final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
   oauth20WrapperController.handleRequest(mockRequest, mockResponse);
   assertEquals(400, mockResponse.getStatus());
   assertEquals("error=" + OAuthConstants.INVALID_REQUEST, mockResponse.getContentAsString());
 }
  @Test
  public void verifyExpiredServiceTicket() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    final Map<String, Object> map = new HashMap<>();
    map.put(NAME, VALUE);
    final List<String> list = Arrays.asList(VALUE, VALUE);
    map.put(NAME2, list);

    final Principal p = org.jasig.cas.authentication.TestUtils.getPrincipal(ID, map);
    final TicketGrantingTicketImpl impl =
        new TicketGrantingTicketImpl(
            TGT_ID,
            org.jasig.cas.authentication.TestUtils.getAuthentication(p),
            new NeverExpiresExpirationPolicy());

    ((OAuth20WrapperController) oauth20WrapperController)
        .getTicketRegistry()
        .addTicket(
            new ServiceTicketImpl(
                "ST1",
                impl,
                org.jasig.cas.authentication.TestUtils.getService(),
                false,
                new ExpirationPolicy() {
                  private static final long serialVersionUID = -7321055962209199811L;

                  @Override
                  public boolean isExpired(final TicketState ticketState) {
                    return true;
                  }
                }));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(400, mockResponse.getStatus());
    assertEquals("error=" + OAuthConstants.INVALID_GRANT, mockResponse.getContentAsString());
  }
  public void _testGetPeople() throws Exception {
    Controller c = (Controller) applicationContext.getBean("peopleController");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    HttpServletResponse response = new MockHttpServletResponse();

    request.addParameter("search", "l");
    ModelAndView mav =
        c.handleRequest((HttpServletRequest) request, (HttpServletResponse) response);
    Map m = mav.getModel();
    log.debug("m = " + m);
    PeopleBacking p = (PeopleBacking) m.get("peopleBacking");
    assertNotNull(p);
    assertNotNull(p.getList());
    assertEquals(mav.getViewName(), "people");
  }
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {
    String path = request.getServletPath();
    String internalPath = path.replaceAll(xmlMarker, "");

    Controller controller = (Controller) getApplicationContext().getBean(internalPath);
    if (controller != null) {
      ModelAndView modelAndView = controller.handleRequest(request, response);
      if (modelAndView.getModel() != null) {
        TransportableModel tm = new TransportableModel();
        tm.putAll(modelAndView.getModel());
        XStream x = new XStream();
        x.toXML(tm, response.getWriter());
      }
    }
    return null;
  }
  public void testInitialList() throws Exception {

    sf.getCurrentSession().createSQLQuery("delete from work_code").executeUpdate();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request = new MockHttpServletRequest("GET", "/workCode.html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    simpleDispatcherServlet.service(request, response);
    ModelAndView mv = parent.handleRequest(request, response);

    Map m = mv.getModel();
    log.debug("m = " + m);
    WorkCodeBacking workCodesBacking = (WorkCodeBacking) m.get("workCodesBacking");
    //		for (WorkCode w: workCodesBacking.getList()) {
    //			log.debug("w = " +  Constants.toReflexString(w));
    //		}
    assertTrue(workCodesBacking.getList().size() == 0);
  }
  @Test
  public void verifyNoServiceTicket() throws Exception {
    clearAllServices();
    final MockHttpServletRequest mockRequest =
        new MockHttpServletRequest("GET", CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.CODE, CODE);
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    ((OAuth20WrapperController) oauth20WrapperController)
        .getServicesManager()
        .save(getRegisteredService(REDIRECT_URI, CLIENT_SECRET));

    oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertEquals(400, mockResponse.getStatus());
    assertEquals("error=" + OAuthConstants.INVALID_GRANT, mockResponse.getContentAsString());
  }
  @Override
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    ModelAndView mav = null;

    try {

      MediaType mediaType =
          ContentNegotiationResolver.INSTANCE.getContentType(request, response, producesMediaTypes);
      mav = delegate.handleRequest(request, response);
      if (mediaType.equals(MediaType.APPLICATION_JSON)) {
        mav.setViewName(jsonView);
      }

    } catch (UnresolvedMediaTypeException e) {
      // No MediaType could be resolved for this request based on the produces setting. Let's return
      // 404.
      mav =
          new ModelAndView(
              new View() {
                @Override
                public String getContentType() {
                  return null;
                }

                @Override
                public void render(
                    Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
                  response.setStatus(HttpStatus.NOT_FOUND.value());
                }
              });
    }

    return mav;
  }
  public void testNew() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request = new MockHttpServletRequest("GET", "/workCodeForm.html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    simpleDispatcherServlet.service(request, response);
    ModelAndView mv = child.handleRequest(request, response);

    Map m = mv.getModel();
    log.debug("m = " + m);
    WorkCodeFormBacking backing = (WorkCodeFormBacking) m.get("workCodeFormBacking");
    log.debug(Constants.toReflexString(backing));

    MockPageContext ctx = new MockPageContext();
    Iterator i = m.keySet().iterator();
    while (i.hasNext()) {
      String key = (String) i.next();
      ctx.setAttribute(key, m.get(key));
    }

    RequestContext r = new RequestContext(request, m);

    BindStatus b = r.getBindStatus("workCodeFormBacking.workCode.description");
    log.debug("expression = " + b.getExpression());
    log.debug("value = " + b.getValue());

    String description =
        (String)
            ExpressionEvaluationUtils.evaluate(
                "test", "${workCodeFormBacking.workCode.description}", String.class, ctx);

    // request.addParameter(name, description);
    String parttime =
        (String)
            ExpressionEvaluationUtils.evaluate(
                "test", "${workCodeFormBacking.workCode.parttime}", String.class, ctx);
    String code =
        (String)
            ExpressionEvaluationUtils.evaluate(
                "test", "${workCodeFormBacking.workCode.code}", String.class, ctx);
    String id =
        (String)
            ExpressionEvaluationUtils.evaluate(
                "test", "${workCodeFormBacking.workCode.id}", String.class, ctx);
    String save =
        (String)
            ExpressionEvaluationUtils.evaluate(
                "test", "${workCodeFormBacking.save}", String.class, ctx);

    log.debug("description = " + description);
    log.debug("parttime = " + parttime);
    log.debug("code = " + code);
    log.debug("id = " + id);
    log.debug("save = " + save);

    HashMap newMap = new HashMap();
    newMap.put(
        r.getBindStatus("workCodeFormBacking.workCode.description").getExpression(), description);
    newMap.put(r.getBindStatus("workCodeFormBacking.workCode.parttime").getExpression(), parttime);
    newMap.put(r.getBindStatus("workCodeFormBacking.workCode.code").getExpression(), code);
    newMap.put(r.getBindStatus("workCodeFormBacking.workCode.id").getExpression(), id);
    newMap.put(r.getBindStatus("workCodeFormBacking.save").getExpression(), "save");

    request = new MockHttpServletRequest("POST", "/workCodeForm.html");
    i = newMap.keySet().iterator();
    while (i.hasNext()) {
      String key = (String) i.next();
      log.debug("add parameter " + key + "  " + newMap.get(key));
      request.addParameter(key, (String) newMap.get(key));
    }
    mv = child.handleRequest(request, response);
    m = mv.getModel();
    log.debug("m = " + m);
    BeanPropertyBindingResult br =
        (BeanPropertyBindingResult)
            m.get("org.springframework.validation.BindingResult.workCodeFormBacking");
    log.debug("field errors " + br.getFieldErrorCount());
    log.debug("errors " + br.getErrorCount());
    assertTrue(br.getFieldError("workCode.code") != null);
    assertTrue(br.getFieldError("workCode.description") != null);
    backing = (WorkCodeFormBacking) m.get("workCodeFormBacking");
    log.debug(response.getErrorMessage());
    log.debug(Constants.toReflexString(backing));
    log.debug("view = " + mv.getViewName());
    assertTrue("workCodeForm".equals(mv.getViewName()));
  }