Lab Manual 1
Lab Manual 1
Welcome to your first lab! In this lab, we will be working hands-on on the content we discussed in the
labs. To follow along with this lab, you need to install the required tools mentioned in the installation
guide uploaded to the CMS.
f) Run the project and visit localhost:8080 to make sure all is running correctly.
a) controllers
b) services
c) repositories
d) models
e) utils
1
3 Create your first controller
In the controllers folder create UserController with URL /users, then create a method called
testController which returns the text Hello from user controller with URL /hello , do not forget
to use the following annotations:
a) @RestController
b) @RequestMapping
c) @GetMapping
for the time being, we will note be annotating our model with any annotation, until we get into the
database, we will use the @Entity annotation.
Then we create getters and setters for the above attributes and create an empty constructor, a constructor
with fields except id, and a constructor with all fields for the class
a) GET /users => getUsers(): This method should return a list containing a dummy User created
on the spot.
b) GET /users/{id} => getUserById(...): This method should take as input the id in the URL
using @PathVariable.
c) POST /users => createUser(...): This method takes the user content from the body using
@RequestBody in the parameters and returns the same user with a generated id.
a) findAll(): read the JSON file and return all users. You can use the block below to read the JSON
file.
2
ObjectMapper objectMapper = new ObjectMapper();
String FILE_PATH = "src/main/resources/users.json";
File file = new ClassPathResource("users.json").getFile();
List<User> users = objectMapper.readValue(file, new TypeReference<List<User>>() {});
make sure to handle the IOException thrown when trying to open the file, if the file does not exist
you should throw the following exception
b) findById(String id): this method should return the user (if exists) by the id you can use the
below code block to perform this task
return findAll().stream()
.filter(user -> user.getId().equals(id))
.findFirst();
take care, this function returns Optional<User> type, which indicates that the output could be
null, so to check if it is null we can user .isEmpty() method on it and use .get() on it to get the
actual user value.
c) save(User user): this is a dummy method to simulate saving the user to db. This method should
return the same user with the id set to UUID.randomUUID().toString();
7 User Service
Now let’s create our UserService class annotated with @Service. Instead of having our functionalities
inside the controller, we will move it to the service and use it via dependency injection.
We need to first inject the UserRepository using the @Autowired annotation.
Finally, the class should have the following methods
Finally, test your controllers by visiting the /users and checking it returns the list of users, visit
/users/cf82b98f-3438-4696-8316-c8f6e4010f36 you should find a user with the name Martha Dow-
ning, and if you visit /users/dummy this should return 404 error. You can send a POST request to /users
with a user created with your data and it should return the same user with a generated ID, to perform
the last one you will need to use any REST client mentioned in the installation guide.
8 Testing
Write a single unit test to test that the user created in the UserService returns a user with the same name.
Your test case should follow the convention discussed in the lab, functionality_input_expectedOutput.
3
b) Create a test case called createUser_withValidInput_shouldReturnSameNameAndEmail
c) Follow the Arrange, Act, and Assert technique.