Skip to content

Commit

Permalink
Added more backend infrastructure and made small change to maven gith…
Browse files Browse the repository at this point in the history
…ub workflow configuration (#5)

* Added more backend infrastructure and some unit tests

* Added remaining Account unit tests

* Added temp change to maven yml

* Fixed issue with unit test
  • Loading branch information
Hexman768 authored Oct 20, 2024
1 parent 5cacc9b commit 6d3b2df
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ name: Java CI with Maven
on:
pull_request:
branches: [ "main" ]
push:
branches: [ "main" ]

jobs:
build:
Expand All @@ -26,7 +28,7 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
run: mvn clean install -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ public class AccountController
{
private final AccountService accountService;

/**
* Creates a new account object and saves it to the database.
*
* @param account the account instance to be added to the database.
* @return The response from the server.
*/
@PostMapping("/account")
public ResponseEntity<?> save(@RequestBody Account account)
{
return new ResponseEntity<>(accountService.create(account), HttpStatus.CREATED);
}

/**
* @return A response object containing all accounts from the database.
*/
@GetMapping("/accounts")
public ResponseEntity<?> findAll()
{
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/avengers/example/controller/LoanController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.avengers.example.controller;

import com.avengers.example.domain.Loan;
import com.avengers.example.service.LoanService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
public class LoanController
{
private final LoanService loanService;

/**
* Creates a new loan object and saves it to the database.
*
* @param loan the loan instance to be added to the database.
* @return The response from the server.
*/
@PostMapping("/loan")
public ResponseEntity<?> save(@RequestBody Loan loan)
{
return new ResponseEntity<>(loanService.create(loan), HttpStatus.CREATED);
}

/**
* @return A response object containing all loans from the database.
*/
@GetMapping("/loans")
public ResponseEntity<?> findAll()
{
return new ResponseEntity<>(loanService.findAll(), HttpStatus.OK);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/avengers/example/domain/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class Account
private String email;
private String phoneNumber;

/**
* No argument constructor for account object.
*/
public Account()
{
}
Expand All @@ -32,16 +35,29 @@ public Account(int userType, String username, String password, String email, Str
this.phoneNumber = phoneNumber;
}

/**
* Sets the type of user account.
*
* @param userType (int) type of user account.
*/
public void setUserType(int userType)
{
this.userType = userType;
}

/**
* @return (int) the type of user account
*/
public int getUserType()
{
return userType;
}

/**
* Sets the username of the account.
*
* @param username (String) username of the account.
*/
public void setUsername(String username)
{
if (null == username || username == "" || username == " ")
Expand All @@ -51,11 +67,19 @@ public void setUsername(String username)
this.username = username;
}

/**
* @return (String) username of the account.
*/
public String getUsername()
{
return username;
}

/**
* Sets the password of the account.
*
* @param password (String) account password.
*/
public void setPassword(String password)
{
if (null == password || password == "" || password == " ")
Expand All @@ -65,11 +89,19 @@ public void setPassword(String password)
this.password = password;
}

/**
* @return (String) account password.
*/
public String getPassword()
{
return password;
}

/**
* Sets the email on the account.
*
* @param email (String) account email.
*/
public void setEmail(String email)
{
if (null == email || email == "" || email == " ")
Expand All @@ -79,11 +111,19 @@ public void setEmail(String email)
this.email = email;
}

/**
* @return (String) account email.
*/
public String getEmail()
{
return email;
}

/**
* Sets the phone number of the account.
*
* @param phoneNumber (String) account phone number.
*/
public void setPhoneNumber(String phoneNumber)
{
if (null == phoneNumber || phoneNumber == "" || phoneNumber == " ")
Expand All @@ -93,6 +133,9 @@ public void setPhoneNumber(String phoneNumber)
this.phoneNumber = phoneNumber;
}

/**
* @return (String) account phone number.
*/
public String getPhoneNumber()
{
return phoneNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.avengers.example.repository;

import com.avengers.example.domain.Loan;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LoanRepository extends JpaRepository<Loan, Long>
{
}
27 changes: 27 additions & 0 deletions src/main/java/com/avengers/example/service/LoanService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.avengers.example.service;

import com.avengers.example.domain.Loan;
import com.avengers.example.repository.LoanRepository;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class LoanService
{
private final LoanRepository loanRepository;

@Transactional
public Loan create(Loan loan)
{
return loanRepository.save(loan);
}

public List<Loan> findAll()
{
return loanRepository.findAll();
}
}
9 changes: 4 additions & 5 deletions src/test/java/com/avengers/example/ApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
@SpringBootTest
class ApplicationTests
{

@Test
void contextLoads() {
}

@Test
void contextLoads()
{
}
}
97 changes: 97 additions & 0 deletions src/test/java/com/avengers/example/domain/AccountTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.avengers.example.domain;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
public class AccountTests
{
private Account testAccount;
private long acctId = 1L;
private int userType = 1;
private String username = "user";
private String password = "pass";
private String email = "[email protected]";
private String phonenumber = "(111)111-1111";

@BeforeEach
public void setup()
{
testAccount = new Account(userType, username, password, email, phonenumber);
}

@Test
public void testNoArgumentConstructor()
{
Account account = new Account();
assert account != null;
}

@Test
public void testAllArgumentConstructor()
{
assert testAccount != null;
}

@Test
public void testSetUserType()
{
testAccount.setUserType(2);
assert testAccount.getUserType() == 2;
}

@Test
public void testSetUserName()
{
testAccount.setUsername("test");
assert testAccount.getUsername() == "test";
}

@Test
public void testSetUserName_NullUserName()
{
assertThrows(IllegalArgumentException.class, () -> testAccount.setUsername(null));
}

@Test
public void testSetPassword()
{
testAccount.setPassword("test");
assert testAccount.getPassword() == "test";
}

@Test
public void testSetPassword_NullPassword()
{
assertThrows(IllegalArgumentException.class, () -> testAccount.setPassword(null));
}

@Test
public void testSetPhoneNumber()
{
testAccount.setPhoneNumber("test");
assert testAccount.getPhoneNumber() == "test";
}

@Test
public void testSetPhoneNumber_NullPhoneNumber()
{
assertThrows(IllegalArgumentException.class, () -> testAccount.setPhoneNumber(null));
}

@Test
public void testSetEmail()
{
testAccount.setEmail("test");
assert testAccount.getEmail() == "test";
}

@Test
public void testSetEmail_NullEmail()
{
assertThrows(IllegalArgumentException.class, () -> testAccount.setEmail(null));
}
}

0 comments on commit 6d3b2df

Please sign in to comment.