Creating a register and log in endpoints with Java and Javalin

180 views Asked by At

I'm trying to create endpoints to register and log in with java and javalin. and it has some test cases that I have to pass. So far I've passed 6 out of 7, but I can't get to pass the last test case of the login. Here is what I have for both endpoints:

public class SocialMediaController {

    private Map<String, Account> accounts = new HashMap<>();
   
 private int accountIdCounter = 0; // 
Counter for generating account_id

    public Javalin startAPI() {
        Javalin app = Javalin.create();
        app.post("/register", this::registerHandler);
        app.post("/login", this::loginHandler);

        return app;
    }

   
    private void registerHandler(Context context) {
        Account newAccount = context.bodyAsClass(Account.class);

        if (newAccount.getUsername().isBlank() || newAccount.getPassword().length() < 4) {
            context.status(400).json("");
            return;
        }

       
        if (accounts.containsKey(newAccount.getUsername())) {
            context.status(400).json("");
            return;
        }

       
        int accountId = generateAccountId();
        newAccount.setAccount_id(accountId);

       
        accounts.put(newAccount.getUsername(), newAccount);

    
        context.status(200).json(newAccount);
    }

    private synchronized int generateAccountId() {
        accountIdCounter++; // Increment the counter
        return accountIdCounter;
    }




   private void loginHandler(Context context) {
        Account loginAccount = context.bodyAsClass(Account.class);

        Account matchedAccount = accounts.values().stream()
                .filter(account -> account.getUsername().equals(loginAccount.getUsername())
                        && account.getPassword().equals(loginAccount.getPassword()))
                .findFirst()
                .orElse(null);

        if (matchedAccount != null) {
            context.status(200).json(matchedAccount);
        } else {
            context.status(401).json(""); 
        }
    }
}

And this is the last test case that I can't get to pass:

@Test
    public void loginSuccessful() throws IOException, InterruptedException {
        HttpRequest postRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8080/login"))
                .POST(HttpRequest.BodyPublishers.ofString("{" +
                        "\"username\": \"testuser1\", " +
                        "\"password\": \"password\" }"))
                .header("Content-Type", "application/json")
                .build();
        HttpResponse response = webClient.send(postRequest, HttpResponse.BodyHandlers.ofString());
        int status = response.statusCode();

        Assert.assertEquals(200, status);
        ObjectMapper om = new ObjectMapper();
        Account expectedResult = new Account(1, "testuser1", "password");
        Account actualResult = om.readValue(response.body().toString(), Account.class);
        Assert.assertEquals(expectedResult, actualResult);        

    }

Does anyone have any idea on what I'm doing wrong? I don't know much about Java

I tried modifying the handlers and modifying the private int accountIdCounter = 0; to 1 instead of 0 but I was unsuccesful at all my attempts to make it through

0

There are 0 answers