forked from QA-Instructor/lbg-grad-gh-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 92c649f
Showing
11 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.classpath | ||
.project | ||
.settings/ | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# lbg-grad-gh-actions | ||
Starting point for setting up a maven build with Github Actions | ||
|
||
This repository builds upon [this original repository](https://github.com/MrWalshyType2/QAA-Module3-UnitTest-Exercise-Solutions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.qaa.module3</groupId> | ||
<artifactId>unit_testing_exercises</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>Unit testing exercises</name> | ||
<description>Java based exercises for unit testing with JUnit</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<maven.compiler.release>11</maven.compiler.release> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core --> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>4.6.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter --> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<version>4.6.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.9.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html --> | ||
<dependency> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>3.0.0-M7</version> | ||
<type>maven-plugin</type> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/qaa/module3/unit_testing_exercises/exercise1/Calculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise1; | ||
|
||
public class Calculator { | ||
|
||
public double add(double num1, double num2) { | ||
return num1 + num2; | ||
} | ||
|
||
public double subtract(double num1, double num2) { | ||
return num1 - num2; | ||
} | ||
|
||
public double multiply(double num1, double num2) { | ||
return num1 * num2; | ||
} | ||
|
||
public double divide(double num1, double num2) { | ||
if (num2 == 0) throw new IllegalArgumentException("Division by zero: divisor must not be 0"); | ||
return num1 / num2; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/qaa/module3/unit_testing_exercises/exercise2/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise2; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class UserService { | ||
|
||
private Map<String, String> users; | ||
|
||
public UserService() { | ||
this.users = new HashMap<>(); | ||
} | ||
|
||
public String register(String username, String password) { | ||
// username must not be null or empty | ||
if (username == null) throw new IllegalArgumentException("Username must not be null"); | ||
String trimmedUsername = username.trim(); | ||
if (trimmedUsername.isEmpty()) throw new IllegalArgumentException("Username must not be whitespace only"); | ||
|
||
// password must not be null or empty | ||
if (password == null) throw new IllegalArgumentException("Password must not be null"); | ||
String trimmedPassword = password.trim(); | ||
if (trimmedPassword.isEmpty()) throw new IllegalArgumentException("Password must not be whitespace only"); | ||
|
||
// username must be at least 4 characters | ||
if (trimmedUsername.length() < 4) throw new IllegalArgumentException("Username must contain at least 4 characters"); | ||
|
||
// username must be unique | ||
if (users.get(trimmedUsername) != null) throw new IllegalArgumentException("Username already exists"); | ||
|
||
// password must be at least 6 characters | ||
if (trimmedPassword.length() < 6) throw new IllegalArgumentException("Password must contain at least 6 characters"); | ||
|
||
// password must contain at least 1 uppercase character | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[A-Z]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 uppercase character"); | ||
|
||
// password must contain at least 1 lowercase character | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[a-z]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 lowercase character"); | ||
|
||
// password must contain at least 1 number | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[1-9]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 number character"); | ||
|
||
// add user to map | ||
users.put(trimmedUsername, trimmedPassword); | ||
|
||
return trimmedUsername; | ||
} | ||
|
||
public String login(String username, String password) { | ||
// username and password must not be null | ||
if (username == null || password == null) throw new IllegalArgumentException("Username and password must not be null"); | ||
String trimmedUsername = username.trim(); | ||
String trimmedPassword = password.trim(); | ||
|
||
// username and password must not be empty | ||
if (trimmedUsername.isEmpty() || trimmedPassword.isEmpty()) throw new IllegalArgumentException("Username and password must not be empty"); | ||
|
||
String savedPassword = users.get(trimmedUsername); | ||
if (savedPassword == null) throw new RuntimeException("Invalid username supplied"); | ||
|
||
if (!trimmedPassword.equals(savedPassword)) throw new IllegalArgumentException("Invalid password supplied"); | ||
|
||
return username; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/qaa/module3/unit_testing_exercises/exercise3/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise3; | ||
|
||
import java.util.Objects; | ||
|
||
public class User { | ||
|
||
private int id; | ||
private String username; | ||
private String password; | ||
|
||
public User() { | ||
super(); | ||
} | ||
|
||
public User(int id, String username, String password) { | ||
super(); | ||
this.id = id; | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, password, username); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
User other = (User) obj; | ||
return id == other.id && Objects.equals(password, other.password) && Objects.equals(username, other.username); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User [id=" + id + ", username=" + username + ", password=" + password + "]"; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/qaa/module3/unit_testing_exercises/exercise3/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise3; | ||
|
||
public class UserController { | ||
|
||
private UserRepository repository; | ||
|
||
public UserController(UserRepository userRepository) { | ||
this.repository = userRepository; | ||
} | ||
|
||
public User register(User user) { | ||
// User must not be null | ||
if (user == null) throw new IllegalArgumentException("User must not be null"); | ||
|
||
String username = user.getUsername(); | ||
String password = user.getPassword(); | ||
|
||
// username must not be null or empty | ||
if (username == null) throw new IllegalArgumentException("Username must not be null"); | ||
String trimmedUsername = username.trim(); | ||
if (trimmedUsername.isEmpty()) throw new IllegalArgumentException("Username must not be whitespace only"); | ||
|
||
// password must not be null or empty | ||
if (password == null) throw new IllegalArgumentException("Password must not be null"); | ||
String trimmedPassword = password.trim(); | ||
if (trimmedPassword.isEmpty()) throw new IllegalArgumentException("Password must not be whitespace only"); | ||
|
||
// username must be at least 4 characters | ||
if (trimmedUsername.length() < 4) throw new IllegalArgumentException("Username must contain at least 4 characters"); | ||
|
||
// username must be unique | ||
if (repository.exists(trimmedUsername)) throw new IllegalArgumentException("Username already exists"); | ||
|
||
// password must be at least 6 characters | ||
if (trimmedPassword.length() < 6) throw new IllegalArgumentException("Password must contain at least 6 characters"); | ||
|
||
// password must contain at least 1 uppercase character | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[A-Z]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 uppercase character"); | ||
|
||
// password must contain at least 1 lowercase character | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[a-z]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 lowercase character"); | ||
|
||
// password must contain at least 1 number | ||
if (!trimmedPassword.matches("[A-Z|a-z|1-9]*[1-9]+[A-Z|a-z|1-9]*")) throw new IllegalArgumentException("Password must contain at least 1 number character"); | ||
|
||
// add user to db | ||
return repository.register(user); | ||
} | ||
|
||
public User login(User user) { | ||
// User must not be null | ||
if (user == null) throw new IllegalArgumentException("User must not be null"); | ||
|
||
String username = user.getUsername(); | ||
String password = user.getPassword(); | ||
|
||
// username and password must not be null | ||
if (username == null || password == null) throw new IllegalArgumentException("Username and password must not be null"); | ||
|
||
// username and password must not be empty | ||
if (username.isEmpty() || password.isEmpty()) throw new IllegalArgumentException("Username and password must not be empty"); | ||
|
||
return repository.login(user); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/qaa/module3/unit_testing_exercises/exercise3/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise3; | ||
|
||
public interface UserRepository { | ||
|
||
public boolean exists(String trimmedUsername); | ||
|
||
public User register(User user); | ||
|
||
public User login(User user); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/com/qaa/module3/unit_testing_exercises/exercise1/CalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.qaa.module3.unit_testing_exercises.exercise1; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CalculatorTest { | ||
|
||
private Calculator calculator; | ||
|
||
/* | ||
Junit is not invoking the setUp and tearDown methods, so as a workaround | ||
they are currently invoked manually at the start/end of each test. | ||
*/ | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
calculator = new Calculator(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
calculator = null; | ||
} | ||
|
||
@Test | ||
public void testAddSmallNumbersTest() { | ||
setUp(); | ||
// Arrange | ||
double num1 = 10, num2 = 20; | ||
double expected = 30; | ||
|
||
// Act | ||
double actual = calculator.add(num1, num2); | ||
|
||
// Assert | ||
Assertions.assertEquals(expected, actual); | ||
tearDown(); | ||
} | ||
|
||
@Test | ||
public void testSubtractSmallNumbersTest() { | ||
setUp(); | ||
// Arrange | ||
double num1 = 10, num2 = 20; | ||
double expected = -10; | ||
|
||
// Act | ||
double actual = calculator.subtract(num1, num2); | ||
|
||
// Assert | ||
Assertions.assertEquals(expected, actual); | ||
tearDown(); | ||
} | ||
|
||
@Test | ||
public void testMultiplySmallNumbersTest() { | ||
setUp(); | ||
// Arrange | ||
double num1 = 10, num2 = 20; | ||
double expected = 200; | ||
|
||
// Act | ||
double actual = calculator.multiply(num1, num2); | ||
|
||
// Assert | ||
Assertions.assertEquals(expected, actual); | ||
tearDown(); | ||
} | ||
|
||
@Test | ||
public void testDivideSmallNumbersTest() { | ||
setUp(); | ||
// Arrange | ||
double num1 = 10, num2 = 20; | ||
double expected = 0.5; | ||
|
||
// Act | ||
double actual = calculator.divide(num1, num2); | ||
|
||
// Assert | ||
Assertions.assertEquals(expected, actual); | ||
tearDown(); | ||
} | ||
|
||
} |
Oops, something went wrong.