Exemplo n.º 1
0
  /** Test the list Users method */
  public void testListUsers() {
    UsersController controller = new UsersController(userService);
    ModelMap model = null;
    ModelAndView result = controller.listUsers(model);
    assertEquals("admin/users/users", result.getViewName());

    controller = new UsersController(userService);
    model = new ModelMap();
    // Expectations
    context.checking(
        new Expectations() {
          {
            one(userService).getUsers();
            will(returnValue(new ArrayList<User>()));
          }
        });
    // Test
    result = controller.listUsers(model);
    // Verify
    assertNotNull(model.get("user"));
    List<User> users = (List<User>) model.get("users");
    assertTrue(users.isEmpty());
    assertEquals("admin/users/users", result.getViewName());

    model = new ModelMap();
    // Expectations
    context.checking(
        new Expectations() {
          {
            one(userService).getUsers();
            will(returnValue(new ArrayList<User>()));
          }
        });
    model.addAttribute("user", new User(null, null, null, false));
    result = controller.listUsers(model);
    assertNotNull(model.get("user"));
    users = (List<User>) model.get("users");
    assert (users.isEmpty());
    assertEquals("admin/users/users", result.getViewName());
  }
 @PUT
 @Path("/edit")
 @Consumes(MediaType.APPLICATION_JSON)
 public void editNote(String inputData) {
   JSONObject inputJSON = new JSONObject(inputData);
   if ((inputJSON.has("sessionID"))
       && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
     if (inputJSON.has("noteID")) {
       int id = Integer.parseInt(inputJSON.getString("noteID"));
       editNote(inputData, id);
     }
     // w przeciwnym wypadku nic nie robi
   }
 }
 @PUT
 @Path("/edit/{id}")
 @Consumes(MediaType.APPLICATION_JSON)
 public void editNote(String inputData, @PathParam("id") int id) {
   JSONObject inputJSON = new JSONObject(inputData);
   if ((inputJSON.has("sessionID"))
       && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
     Note newNote = resolveNoteData(inputJSON);
     if (inputJSON.has("noteID")) inputJSON.remove("noteID"); // na wszelki wypadek - ID podano
     hibernate.controllers.NotesController notesController =
         new hibernate.controllers.NotesController();
     if (newNote != null) notesController.updateNote(newNote, id);
   }
 }
 @POST
 @Path("/create")
 @Consumes(MediaType.APPLICATION_JSON)
 public void createNote(String inputData) {
   JSONObject inputJSON = new JSONObject(inputData);
   if ((inputJSON.has("sessionID"))
       && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
     if (inputJSON.has("noteID"))
       inputJSON.remove("noteID"); // na wszelki wypadek - chcemy generowaæ ID automatycznie
     Note newNote = resolveNoteData(inputJSON);
     hibernate.controllers.NotesController notesController =
         new hibernate.controllers.NotesController();
     if (newNote != null) notesController.addNote(newNote);
   }
 }
 @POST
 @Path("/view/{id}")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public String viewNote(String inputData, @PathParam("id") int id) {
   JSONObject inputJSON = new JSONObject(inputData);
   if ((inputJSON.has("sessionID"))
       && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
     hibernate.controllers.NotesController notesController =
         new hibernate.controllers.NotesController();
     Note note = notesController.readNote(id);
     if (note != null) {
       JSONObject jsonNote = getJSONNote(note);
       return jsonNote.toString();
     } else {
       return "No subject of ID == " + id + "!";
     }
   } else return "Invalid session!";
 }
  @DELETE
  @Path("/{id}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces("text/plain")
  public String deleteNote(String inputData, @PathParam("id") int id) {
    JSONObject inputJSON = new JSONObject(inputData);
    if ((inputJSON.has("sessionID"))
        && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
      try {
        hibernate.controllers.NotesController notesController =
            new hibernate.controllers.NotesController();
        notesController.deleteNote(id);

        return "OK";
      } catch (Exception ex) {
        return ex.getMessage();
      }
    } else return "Invalid session!";
  }
 @POST
 @Path("/view")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public String viewAllNotes(String inputData) {
   JSONObject inputJSON = new JSONObject(inputData);
   if ((inputJSON.has("sessionID"))
       && UsersController.checkLogin(inputJSON.getString("sessionID"))) {
     hibernate.controllers.NotesController notesController =
         new hibernate.controllers.NotesController();
     List<Note> notes = notesController.readAllNotes();
     JSONArray results = new JSONArray();
     JSONObject jsonNote;
     for (Note note : notes) {
       jsonNote = getJSONNote(note);
       results.put(jsonNote);
     }
     return results.toString();
   } else return "Invalid session!";
 }