1 00:00:00,000 --> 00:00:02,530 [No Audio] 2 00:00:02,530 --> 00:00:04,954 So let's kick off this section now with adding 3 00:00:04,954 --> 00:00:06,466 some more meat on the bones to the domain 4 00:00:06,466 --> 00:00:08,948 and repository classes, because this will allow us to 5 00:00:08,948 --> 00:00:10,856 have something a bit more interesting to test. 6 00:00:10,856 --> 00:00:12,702 And we can also talk about some design decisions we 7 00:00:12,716 --> 00:00:15,160 make, along the way as well, which is always useful. 8 00:00:15,160 --> 00:00:17,537 So let's get started. Right? So let's jump 9 00:00:17,537 --> 00:00:19,484 back into this project we've been working with. 10 00:00:19,484 --> 00:00:22,952 So if we open up myapp, go into src main 11 00:00:22,952 --> 00:00:25,016 java, and look at the production code we can see here 12 00:00:25,016 --> 00:00:27,296 we've got the data, service, and web players, and 13 00:00:27,296 --> 00:00:29,338 we've also got this User object as well. 14 00:00:29,338 --> 00:00:31,856 So let's just double click this User object and 15 00:00:31,856 --> 00:00:34,184 we'll just add an extra field on here to 16 00:00:34,184 --> 00:00:35,996 say if the user is currently live. 17 00:00:35,996 --> 00:00:37,244 In other words, it's currently having 18 00:00:37,244 --> 00:00:38,888 an account which isn't suspended, for example. 19 00:00:38,888 --> 00:00:41,156 So it's live on the system and you can log in. 20 00:00:41,156 --> 00:00:43,446 You can imagine setting this flag to false if we 21 00:00:43,448 --> 00:00:46,364 wanted to suspend the user account for whatever reason. 22 00:00:46,364 --> 00:00:47,847 So let's just put that in. Just gives 23 00:00:47,847 --> 00:00:50,463 us an extra field that we can test with. 24 00:00:50,463 --> 00:00:51,582 And actually, you know what, 25 00:00:51,596 --> 00:00:52,832 We'll always set it to true. 26 00:00:52,832 --> 00:00:53,960 So whenever we create a new 27 00:00:53,960 --> 00:00:55,862 user, we'll always set it to true. 28 00:00:55,862 --> 00:00:57,092 In a normal system, you probably 29 00:00:57,092 --> 00:00:58,382 wouldn't want to do this. 30 00:00:58,382 --> 00:01:01,932 And again, we'll put the Getter on here. 31 00:01:01,932 --> 00:01:03,992 To be is live. 32 00:01:03,992 --> 00:01:06,512 The convention for Getters for boolean type 33 00:01:06,512 --> 00:01:08,144 values is instead of being, for example, 34 00:01:08,144 --> 00:01:10,148 get live, it returns isLive. 35 00:01:10,148 --> 00:01:11,888 So it replaces the word get with is. 36 00:01:11,888 --> 00:01:15,356 And we'll also regenerate the 37 00:01:15,356 --> 00:01:17,060 hashcode and equals methods too. 38 00:01:17,060 --> 00:01:19,010 [No Audio] 39 00:01:19,010 --> 00:01:20,266 By the way, before I did Java 40 00:01:20,266 --> 00:01:21,469 7, you don't have to do that. You can 41 00:01:21,469 --> 00:01:23,998 actually change the template which IntelliJ uses. 42 00:01:23,998 --> 00:01:26,170 So, for example, I can use an IntelliJ Default. 43 00:01:26,170 --> 00:01:27,220 And what this does 44 00:01:27,220 --> 00:01:29,841 [No Audio] 45 00:01:29,841 --> 00:01:32,876 click next through these, is that 46 00:01:32,876 --> 00:01:35,022 it generates equals and hash code methods in a way 47 00:01:35,036 --> 00:01:36,526 that's going to work with any JDK version. 48 00:01:36,526 --> 00:01:38,696 So it's probably better to do and that means we 49 00:01:38,696 --> 00:01:42,226 can just nuke these objects reference from Java 7. 50 00:01:42,226 --> 00:01:44,366 And then we've got our user object. 51 00:01:44,366 --> 00:01:46,892 Let's also add an enumeration now as well. 52 00:01:46,892 --> 00:01:48,932 Now the other thing we want to add with this is 53 00:01:48,932 --> 00:01:51,946 basically the notion of a user being a RegularUser. 54 00:01:51,946 --> 00:01:53,828 So a user who's a customer, if you like, on 55 00:01:53,828 --> 00:01:56,576 the website, or who is a superuser, in other words, 56 00:01:56,576 --> 00:01:58,724 an admin or a manager, that kind of thing. 57 00:01:58,724 --> 00:02:00,874 So for now, let's just say there are two types. 58 00:02:00,874 --> 00:02:02,830 There's a user and an admin. 59 00:02:02,830 --> 00:02:04,789 So how can we represent this? So we can either 60 00:02:04,789 --> 00:02:07,475 represent this by, for example, having an enumeration 61 00:02:07,475 --> 00:02:10,625 reference in the user class, or alternatively, by 62 00:02:10,625 --> 00:02:13,407 introducing a new type, for example, super user or 63 00:02:13,407 --> 00:02:15,740 admin user or whatever you want to name it. 64 00:02:15,740 --> 00:02:17,972 Now we can just put an enum here for now. That's fine. 65 00:02:17,972 --> 00:02:19,038 So let's just go up here 66 00:02:19,038 --> 00:02:34,005 [No Audio] 67 00:02:34,005 --> 00:02:35,832 and we just put the enum in like that. 68 00:02:35,832 --> 00:02:38,016 And then we just need to make sure we 69 00:02:38,016 --> 00:02:41,244 can hold a reference to that user type. 70 00:02:41,244 --> 00:02:42,566 So here we're defining two user 71 00:02:42,566 --> 00:02:44,493 types, RegularUser and AdminUser. 72 00:02:44,493 --> 00:02:46,590 [No Audio] 73 00:02:46,590 --> 00:02:48,710 Let's just say we make that final. 74 00:02:48,710 --> 00:02:51,430 [No Audio] 75 00:02:51,430 --> 00:02:53,260 So should we have to assign here the constructor? 76 00:02:53,260 --> 00:03:04,310 [No Audio] 77 00:03:04,310 --> 00:03:05,204 And so we do this. 78 00:03:05,204 --> 00:03:07,370 So we have to be explicit about the type, 79 00:03:07,370 --> 00:03:09,753 that is the type of user that we're creating. 80 00:03:09,753 --> 00:03:12,420 This means that if I find usages of this now 81 00:03:12,420 --> 00:03:16,369 [No Audio] 82 00:03:16,369 --> 00:03:18,276 with command shift F, then call to 83 00:03:18,276 --> 00:03:20,358 the constructor like this will no longer work. 84 00:03:20,358 --> 00:03:22,265 So I could change them. 85 00:03:22,265 --> 00:03:24,300 I could change them now and do 86 00:03:24,300 --> 00:03:27,949 UserType.REGULAR_USER for example. 87 00:03:27,949 --> 00:03:29,328 And I'll do that in this case for 88 00:03:29,328 --> 00:03:31,262 the sake of this demonstration with Hamcrest. 89 00:03:31,262 --> 00:03:33,084 But in general a better approach in my 90 00:03:33,084 --> 00:03:38,004 opinion is to instead mark a constructor as 91 00:03:38,004 --> 00:03:41,887 package private, then instead create static factory 92 00:03:41,887 --> 00:03:43,994 methods to create the different types of user. 93 00:03:43,994 --> 00:03:45,240 And I will just do this now. 94 00:03:45,240 --> 00:03:48,352 In fact, I think about it. So public static user, 95 00:03:48,352 --> 00:03:52,035 public static user create regular user, 96 00:03:52,035 --> 00:03:56,585 [No Audio] 97 00:03:56,585 --> 00:03:58,302 which needs to take the username and password. 98 00:03:58,302 --> 00:04:02,030 [No Audio] 99 00:04:02,030 --> 00:04:09,770 Then we'll do return new user username password. 100 00:04:09,770 --> 00:04:13,120 And now we'll put the user type in here. 101 00:04:13,120 --> 00:04:15,936 So this is nice because basically we're sort of 102 00:04:15,936 --> 00:04:18,861 encapsulating to a very mild degree the logic of what 103 00:04:18,861 --> 00:04:21,310 it means to create a RegularUser or AdminUser 104 00:04:21,310 --> 00:04:25,004 inside this class itself and create another one 105 00:04:25,004 --> 00:04:27,737 therefore add AdminUser 106 00:04:27,737 --> 00:04:33,878 [No Audio] 107 00:04:33,878 --> 00:04:35,778 and you might want to read in these as well too 108 00:04:35,778 --> 00:04:37,944 regular and admin or whatever else. 109 00:04:37,944 --> 00:04:40,310 And additionally, put an extra user role type specific 110 00:04:40,310 --> 00:04:42,504 behavior in each of these constructors to set up what 111 00:04:42,504 --> 00:04:43,836 it means to be an AdminUser, or what 112 00:04:43,836 --> 00:04:44,906 it means to be a RegularUser. 113 00:04:44,906 --> 00:04:47,018 For example, if you had different access rights 114 00:04:47,018 --> 00:04:48,408 or ACL permissions you want to put in, 115 00:04:48,408 --> 00:04:49,974 you can put those in as well. Anyway, let's not get 116 00:04:49,974 --> 00:04:51,396 carried away. We don't really care about that. 117 00:04:51,396 --> 00:04:53,029 Let's just go back to the UserRepository 118 00:04:53,029 --> 00:04:54,852 and now we can do this. 119 00:04:54,852 --> 00:04:56,774 That is, we can use our factory method. 120 00:04:56,774 --> 00:04:58,778 You'll notice here we've got the user 121 00:04:58,778 --> 00:05:00,204 which we can no longer use because 122 00:05:00,204 --> 00:05:02,186 the visibility was marked as package private. 123 00:05:02,186 --> 00:05:07,884 So we can just do now User.createRegularUser and 124 00:05:07,884 --> 00:05:10,956 then we take this out now because we don't need it 125 00:05:10,956 --> 00:05:13,955 anymore, optimize the imports 126 00:05:13,955 --> 00:05:17,490 [No Audio] 127 00:05:17,490 --> 00:05:19,276 and then make the same call here. 128 00:05:19,276 --> 00:05:21,400 So a little bit of refactoring there 129 00:05:21,400 --> 00:05:23,925 just to tie things up a bit nicer. 130 00:05:23,925 --> 00:05:25,324 And now we have this userType. 131 00:05:25,324 --> 00:05:27,542 We probably want to get the userType as well. 132 00:05:27,542 --> 00:05:30,703 Just put a Getter on there for the userType. 133 00:05:30,703 --> 00:05:33,606 And once again please get into the habit. 134 00:05:33,606 --> 00:05:36,180 Whenever you add new fields of regenerating 135 00:05:36,180 --> 00:05:37,530 hashcode and equals 136 00:05:37,530 --> 00:05:42,430 [No Audio] 137 00:05:42,430 --> 00:05:45,279 just accept all those, that's fine. 138 00:05:45,279 --> 00:05:47,396 So this user class is flushed out 139 00:05:47,396 --> 00:05:48,692 a bit more now, which is nice. 140 00:05:48,692 --> 00:05:51,133 So let's go back into UserRepository 141 00:05:51,133 --> 00:05:53,014 and put in some extra users. 142 00:05:53,014 --> 00:05:54,776 And this is because we're also going to create 143 00:05:54,776 --> 00:05:56,336 admin users in this as well, which we can 144 00:05:56,336 --> 00:06:00,760 test with, so let's add another RegularUser 145 00:06:00,760 --> 00:06:02,094 Say suzie 146 00:06:02,094 --> 00:06:09,974 [No Audio] 147 00:06:09,974 --> 00:06:11,332 Debra 148 00:06:11,332 --> 00:06:13,248 And these are regular users 149 00:06:13,248 --> 00:06:15,614 [No Audio] 150 00:06:15,614 --> 00:06:20,274 and now we'll have some admin users. 151 00:06:20,274 --> 00:06:24,360 And let's just say we've got one admin for now. 152 00:06:24,360 --> 00:06:25,590 Which is thomas. 153 00:06:25,590 --> 00:06:32,410 [No Audio] 154 00:06:32,410 --> 00:06:33,952 Yep that looks good. 155 00:06:33,952 --> 00:06:36,927 So that gives us enough code for now, so let's move on.