Skip to content

Latest commit

 

History

History
119 lines (108 loc) · 4.39 KB

README.md

File metadata and controls

119 lines (108 loc) · 4.39 KB

Anotaciones

🛠️ Requisitos

📝 Actividad

Actualizar método para agregar usuarios

Esta actividad continua a la descrita en la clase anterior: README

  1. Primero vamos a agregar la anotación de Override a nuestro método de concurrencia run()

    @Override
    public void run(){
        try {

    Esto evitará que llegaramos a tener algún problema respecto a que no llegara a correr el método.

  2. En nuestro método de run() vamos a mover todo lo que esté dentro de nuestro try a algún método que creemos. Y le agregaremos la anotación de @Deprecated

     @Deprecated(since = "Anotaciones update")
     private void createUsers() {
         try {
             String user = "user";
             String pass = "password";
             JSONArray jsonArray = new JSONArray(textThread);
             JSONObject user1 = new JSONObject(jsonArray.get(0).toString());
             JSONObject user2 = new JSONObject(jsonArray.get(1).toString());
             JSONObject user3 = new JSONObject(jsonArray.get(2).toString());
    
             ResponseDTO response = createUser(user1.getString(user), user1.getString(pass));
             responseTextThread = new JSONObject(response).toString();
             LOGGER.info("Usuario 1: " + responseTextThread);
             Thread.sleep(1000);
    
             response = createUser(user2.getString(user), user2.getString(pass));
             responseTextThread = new JSONObject(response).toString();
             LOGGER.info("Usuario 2: " + responseTextThread);
             Thread.sleep(1000);
    
             response = createUser(user3.getString(user), user3.getString(pass));
             responseTextThread = new JSONObject(response).toString();
             LOGGER.info("Usuario 3: " + responseTextThread);
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
         }
     }

    El since dentro de @Deprecated se usa para anotar desde cuando se volvió obsoleto este método.

  3. Crearemos un nuevo método llamado crearUsuarios() donde pondremos la nueva lógica para poder agregar usuarios. Este nuevo método aceptará cualquier número de usuarios que pongamos en el request.

     @Override
     public void run(){
         try {
             crearUsuarios();
         } catch (Exception e) {
             LOGGER.severe(e.getMessage());
             throw new ExcepcionGenerica(e.getMessage());
         }
     }
  4. Y el nuevo método de crearUsuarios quedaría de la siguiente forma.

    private void crearUsuarios() {
        try {
            String user = "user";
            String pass = "password";
            JSONArray jsonArray = new JSONArray(textThread);
            JSONObject userJson;
    
            ResponseDTO response = null;
    
            LOGGER.info("jsonArray.length(): " + jsonArray.length());
            for(int i = 0; i < jsonArray.length(); i++) {
                userJson = new JSONObject(jsonArray.get(i).toString());
                response = createUser(userJson.getString(user), userJson.getString(pass));
                responseTextThread = new JSONObject(response).toString();
                LOGGER.info("Usuario " + (i+1) + ": " + responseTextThread);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

💻 Requests

Usando postman vamos a usar el siguiente endpoint.

http://localhost:8080/api/createUsers

Y en el apartardo de Body vamos a agregar el siguiente raw en JSON.

[{
  "user": "user1@wizeline.com",
  "password": "pass1"
},
  {
    "user": "user2@wizeline.com",
    "password": "pass2"
  }]

✅ 200 Response

{
  "code": "OK000",
  "errors": {},
  "status": "success"
}

📚 Recursos