View Javadoc
1   package de.dlr.shepard.auth.users.services;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.mockito.ArgumentMatchers.any;
5   import static org.mockito.Mockito.never;
6   import static org.mockito.Mockito.verify;
7   import static org.mockito.Mockito.when;
8   
9   import de.dlr.shepard.auth.apikey.entities.ApiKey;
10  import de.dlr.shepard.auth.users.daos.UserDAO;
11  import de.dlr.shepard.auth.users.entities.User;
12  import de.dlr.shepard.common.subscription.entities.Subscription;
13  import io.quarkus.test.InjectMock;
14  import io.quarkus.test.component.QuarkusComponentTest;
15  import jakarta.inject.Inject;
16  import java.util.List;
17  import java.util.UUID;
18  import org.junit.jupiter.api.Test;
19  
20  @QuarkusComponentTest
21  public class UserServiceTest {
22  
23    @InjectMock
24    UserDAO dao;
25  
26    @Inject
27    UserService service;
28  
29    @Test
30    public void createUserTest() {
31      var user = new User("Bob");
32      when(dao.createOrUpdate(user)).thenReturn(user);
33      var actual = service.createOrUpdateUser(user);
34      assertEquals(user, actual);
35    }
36  
37    @Test
38    public void getUserTest() {
39      var user = new User("Bob");
40      when(dao.find("Bob")).thenReturn(user);
41      var actual = service.getUser("Bob");
42      assertEquals(user, actual);
43    }
44  
45    @Test
46    public void updateUserTest_noUpdate() {
47      var old = new User("bob", "John", "Doe", "john.doe@example.com");
48      var user = new User("bob", "John", "Doe", "john.doe@example.com");
49  
50      when(dao.find("bob")).thenReturn(old);
51  
52      var actual = service.createOrUpdateUser(user);
53      verify(dao, never()).createOrUpdate(any(User.class));
54      assertEquals(old, actual);
55    }
56  
57    @Test
58    public void updateUserTest_noUser() {
59      var user = new User("bob", "John", "Doe", "john.doe@example.com");
60  
61      when(dao.find("bob")).thenReturn(null);
62      when(dao.createOrUpdate(user)).thenReturn(user);
63  
64      var actual = service.createOrUpdateUser(user);
65      assertEquals(user, actual);
66    }
67  
68    @Test
69    public void updateUserTest_updateFirstName() {
70      var uid = UUID.randomUUID();
71      var old = new User("bob", "John", "Doe", "john.doe@example.com");
72      old.setApiKeys(List.of(new ApiKey(uid)));
73      old.setSubscriptions(List.of(new Subscription(3L)));
74      var user = new User("bob", "new", "Doe", "john.doe@example.com");
75      var expected = new User("bob", "new", "Doe", "john.doe@example.com");
76      expected.setApiKeys(List.of(new ApiKey(uid)));
77      expected.setSubscriptions(List.of(new Subscription(3L)));
78  
79      when(dao.find("bob")).thenReturn(old);
80      when(dao.createOrUpdate(expected)).thenReturn(expected);
81  
82      var actual = service.createOrUpdateUser(user);
83      assertEquals(expected, actual);
84    }
85  
86    @Test
87    public void updateUserTest_updateLastName() {
88      var uid = UUID.randomUUID();
89      var old = new User("bob", "John", "Doe", "john.doe@example.com");
90      old.setApiKeys(List.of(new ApiKey(uid)));
91      old.setSubscriptions(List.of(new Subscription(3L)));
92      var user = new User("bob", "John", "new", "john.doe@example.com");
93      var expected = new User("bob", "John", "new", "john.doe@example.com");
94      expected.setApiKeys(List.of(new ApiKey(uid)));
95      expected.setSubscriptions(List.of(new Subscription(3L)));
96  
97      when(dao.find("bob")).thenReturn(old);
98      when(dao.createOrUpdate(expected)).thenReturn(expected);
99  
100     var actual = service.createOrUpdateUser(user);
101     assertEquals(expected, actual);
102   }
103 
104   @Test
105   public void updateUserTest_updateEmail() {
106     var uid = UUID.randomUUID();
107     var old = new User("bob", "John", "Doe", "john.doe@example.com");
108     old.setApiKeys(List.of(new ApiKey(uid)));
109     old.setSubscriptions(List.of(new Subscription(3L)));
110     var user = new User("bob", "John", "Doe", "new@example.com");
111     var expected = new User("bob", "John", "Doe", "new@example.com");
112     expected.setApiKeys(List.of(new ApiKey(uid)));
113     expected.setSubscriptions(List.of(new Subscription(3L)));
114 
115     when(dao.find("bob")).thenReturn(old);
116     when(dao.createOrUpdate(expected)).thenReturn(expected);
117 
118     var actual = service.createOrUpdateUser(user);
119     assertEquals(expected, actual);
120   }
121 
122   @Test
123   public void updateUserTest_emptyInput() {
124     var uid = UUID.randomUUID();
125     var old = new User("bob", "John", "Doe", "john.doe@example.com");
126     old.setApiKeys(List.of(new ApiKey(uid)));
127     old.setSubscriptions(List.of(new Subscription(3L)));
128     var user = new User("bob");
129     user.setFirstName(null);
130     user.setLastName(null);
131     user.setEmail(null);
132 
133     when(dao.find("bob")).thenReturn(old);
134 
135     var actual = service.createOrUpdateUser(user);
136     assertEquals(old, actual);
137   }
138 }