diff --git a/src/test/java/assertions/PersonTest.java b/src/test/java/assertions/PersonTest.java index 423eaea..6bc6efc 100644 --- a/src/test/java/assertions/PersonTest.java +++ b/src/test/java/assertions/PersonTest.java @@ -1,38 +1,97 @@ package assertions; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.time.LocalDate; +import java.time.Period; class PersonTest { // getFullName @Test - void getFullNameReturnsFirstnameSpaceLastname(){ + void getFullName_ReturnsFirstnameSpaceLastname(){ // TODO implement - throw new IllegalArgumentException("you should implement code here"); - } + Person p = new Person("Hannes", "Meier", LocalDate.of(1992, 3, 9)); + + String fullName = p.getFullName(); - // TODO some more useful tests + Assertions.assertEquals(fullName, "Hannes Meier"); + } // getAge + @Test + void getAge_Returns10YearsIf10YearsAgo(){ + Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(10)); + Period age = p.getAge(); + + Assertions.assertEquals(10, age.getYears()); + Assertions.assertEquals(0, age.getMonths()); + Assertions.assertEquals(0, age.getDays()); + } - // TODO verbessern. Hinweis: Repeatable (wiederholbar) zu jeder Zeit. @Test - void getAgeReturns10YearsIfBornIn2009() throws Exception { - Person p = new Person("", "", LocalDate.of(2009, 1, 1)); + void getMonth_Returns1MonthIf1MonthAgo(){ + Person p = new Person("Hannes", "Meier", LocalDate.now().minusMonths(1)); - throw new IllegalArgumentException("you should implement code here"); + Period age = p.getAge(); + + Assertions.assertEquals(0, age.getYears()); + Assertions.assertEquals(1, age.getMonths()); + Assertions.assertEquals(0, age.getDays()); } + @Test - void getAgeReturns1DayIfYesterday() throws Exception { - Person p = new Person("", "", LocalDate.now().minusDays(1)); + void getAge_Returns1DayIfYesterday(){ + Person p = new Person("Hannes", "Meier", LocalDate.now().minusDays(1)); - // TODO implement - throw new IllegalArgumentException("you should implement code here"); + Period age = p.getAge(); + + Assertions.assertEquals(0, age.getYears()); + Assertions.assertEquals(0, age.getMonths()); + Assertions.assertEquals(1, age.getDays()); + } + + + // some more tests + @Test + void getName_ReturnsTrueWhenDifferentName(){ + Person p = new Person("1Hannes", "2Meier", LocalDate.of(1992, 3, 9)); + + String fullName = p.getFullName(); + + Assertions.assertFalse(Boolean.parseBoolean(fullName), "Hannes Meier"); + } + + @Test + void getAge_ReturnsTrueWhenDifferentAge(){ + Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(11)); + + Period age = p.getAge(); + + Assertions.assertNotEquals(age, 10); + } + + @Test + void Person_TrueIfObjectIsNotNul(){ + Person p = new Person("Hannes", "Meier", LocalDate.now().minusYears(80)); + Assertions.assertNotNull(p); + } + + @Test + void Person_TrueIf2ObjectsAreNotSame(){ + Person p = new Person("Hannes", "Meier", LocalDate.of(1940, 6, 30)); + Person c = new Person("Petra", "Meier", LocalDate.of(1950, 3, 12)); + Assertions.assertNotSame(p, c); + } + + @Test + void Person_TrueIf2ObjectsAreSame(){ + Person p = new Person("Hannes", "Meier", LocalDate.of(1940, 6, 30)); + Person c = p; + Assertions.assertSame(p, c); } - // TODO some more useful tests -} \ No newline at end of file +} diff --git a/src/test/java/fakes/FakeUserValidator.java b/src/test/java/fakes/FakeUserValidator.java new file mode 100644 index 0000000..2593a7c --- /dev/null +++ b/src/test/java/fakes/FakeUserValidator.java @@ -0,0 +1,16 @@ +package fakes; + +public class FakeUserValidator extends UserValidator { + + private boolean isValid; //injection sagt man dem, von aussen bestimmen was passieren soll + + public FakeUserValidator(boolean isValid, boolean isExisting){ + this.isValid=isValid; + this.isExisting=isExisting; + } + + @Override + public boolean isValidUsername(String username){ + return isValid; + } +} diff --git a/src/test/java/fakes/UserControllerTest.java b/src/test/java/fakes/UserControllerTest.java index 714b4c3..b95428e 100644 --- a/src/test/java/fakes/UserControllerTest.java +++ b/src/test/java/fakes/UserControllerTest.java @@ -12,28 +12,52 @@ class create { // --- Testing with Fakes --- - @Test - void withValidInexistingUsername_returnsOK__NO_FAKE() { - UserController ctrl = new UserController(); - User user = new User("kalua"); + @Test + void withValidInexistingUsername_returnsOK_NO_FAKE(){ + UserController ctrl = new UserController(); + User user = new User("kalua"); Message result = ctrl.create(user); Assertions.assertEquals(result.status, Message.Status.OK); } - @Test - void withValidInexistingUsername_returnsOK__FAKE() { - // TODO - // 1. Test schneller machen - // 2. UserController.create so beinflussen, - // dass einmal der "if"- und einmal der "else"-Fall durchlaufen wird - } + @Test + void withValidInexistingUsername_returnsOK_FAKE(){ + UserController userController = new UserController(); - @Test - void withValidInexistingUsername_returnsOK__MOCKITO() { - // TODO - } + } + + /* @Test + // I DON'T UNDERSTAND THIS ONE :@ + void MOCKITO_FAKE_withValidInexistingUsername_returnsOK() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + // TODO + User user1= new User("Kalua"); + User user2= new User("Hannes"); + + UserController test1 = new UserController(); + UserController test2 = new UserController(); + + Message message1 = new Message(); + Method privateMessage = Message.class.getDeclaredMethod("Message", String.class); + privateMessage.setAccessible(true); + String returnValue = (String) privateMessage.invoke(message1, null); + + + db.addUser(user1); // user1 bereits in die db rein + + //durchläuft else + test1.create(user1); + //durchläuft if + test2.create(user2); + + //Assertions.assertTrue(!(!UserValidator.doesUsernameExist(user1.getUsername()) Message.createOK())); + Assertions.assertEquals(message1, "OK"); + // 1. Test schneller machen + // 2. UserController.create so beinflussen, + // dass einmal der "if"- und einmal der "else"-Fall durchlaufen wird + } +*/ @Test void withValidInexitingUserName_addUserToDB__FAKE() { diff --git a/src/test/java/fakes/UserValidatorTest.java b/src/test/java/fakes/UserValidatorTest.java index 4cffc57..b22a55b 100644 --- a/src/test/java/fakes/UserValidatorTest.java +++ b/src/test/java/fakes/UserValidatorTest.java @@ -1,8 +1,10 @@ package fakes; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; + class UserValidatorTest { @Nested @@ -10,40 +12,76 @@ class isValidUsername{ @Test void returnsTrueIfOnlyLetters(){ - throw new IllegalArgumentException("you should implement code here"); + User user1 = new User("kalua"); + User user2 = new User ("Hannes"); + User user3 = new User("123Hannes123"); + User user4 = new User("!!@"); + String person1; + String person2; + String person3; + String person4; + person1 = user1.getUsername(); + person2 = user2.getUsername(); + person3 = user3.getUsername(); + person4 = user4.getUsername(); + Assertions.assertTrue(isAlpha(person1)); + Assertions.assertTrue(isAlpha(person2)); + Assertions.assertFalse(isAlpha(person3)); + Assertions.assertFalse(isAlpha(person4)); + } + //Test only Letters + public boolean isAlpha(String name){ + char[] chars = name.toCharArray(); + + for (char c : chars){ + if(!Character.isLetter(c)){ + return false; + } + } + return true; } @Test void returnsFalseIfStartsWithNumber(){ - throw new IllegalArgumentException("you should implement code here"); + } @Test void returnsTrueIfContainsNumberButNotAsFirstChar(){ - throw new IllegalArgumentException("you should implement code here"); + } @Test void returnsFalseIfContainsAnyNonAlphanumericChar(){ - throw new IllegalArgumentException("you should implement code here"); } } static class doesUsernameExist{ + //DB used for the test + private static Database db = FileDatabase.getInstance(); @Test - void returnsFalseIfUsernameNotInDBYet(){ - throw new IllegalArgumentException("you should implement code here"); - } + void returnsTrueIfUsernameNotInDBYet(){ + User user1= new User("Kalua"); + User user2= new User("Hannes"); - @Test - void returnsTrueIfUsernameInDB(){ - throw new IllegalArgumentException("you should implement code here"); + db.addUser(user1); + + Assertions.assertTrue(UserValidator.isValidUsername(user2.getUsername()) && !UserValidator.doesUsernameExist(user2.getUsername())); + //Also testing the opposite with user1 + Assertions.assertTrue(UserValidator.isValidUsername(user1.getUsername()) && UserValidator.doesUsernameExist(user1.getUsername())); } @Test - void returnsTrueIfSameNameInDBButWithDifferentLetterCasing(){ - throw new IllegalArgumentException("you should implement code here"); + void returnsFalseIfUsernameInDB(){ + User user1= new User("Kalua"); + User user2= new User("Kalue"); + + db.addUser(user1); + + Assertions.assertTrue(UserValidator.isValidUsername(user2.getUsername()) && !UserValidator.doesUsernameExist(user2.getUsername())); + //False if in DB + Assertions.assertFalse(!UserValidator.doesUsernameExist(user1.getUsername())); } } }