View Javadoc
1   package de.dlr.shepard.common.subscription.services;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertThrows;
5   import static org.junit.jupiter.api.Assertions.assertTrue;
6   import static org.mockito.ArgumentMatchers.any;
7   import static org.mockito.Mockito.when;
8   
9   import de.dlr.shepard.auth.users.entities.User;
10  import de.dlr.shepard.auth.users.services.UserService;
11  import de.dlr.shepard.common.exceptions.InvalidPathException;
12  import de.dlr.shepard.common.subscription.daos.SubscriptionDAO;
13  import de.dlr.shepard.common.subscription.entities.Subscription;
14  import de.dlr.shepard.common.subscription.io.SubscriptionIO;
15  import de.dlr.shepard.common.util.DateHelper;
16  import de.dlr.shepard.common.util.RequestMethod;
17  import io.quarkus.test.InjectMock;
18  import io.quarkus.test.component.QuarkusComponentTest;
19  import jakarta.inject.Inject;
20  import java.util.Date;
21  import java.util.List;
22  import org.junit.jupiter.api.Test;
23  import org.neo4j.ogm.cypher.Filter;
24  
25  @QuarkusComponentTest
26  public class SubscriptionServiceTest {
27  
28    @InjectMock
29    SubscriptionDAO dao;
30  
31    @InjectMock
32    UserService userService;
33  
34    @InjectMock
35    DateHelper dateHelper;
36  
37    @Inject
38    SubscriptionService service;
39  
40    @Test
41    public void getSubscriptionTest() {
42      var sub = new Subscription(1L);
43      var subscriptionUser = new User("bob");
44      sub.setCreatedBy(subscriptionUser);
45      when(dao.findByNeo4jId(1L)).thenReturn(sub);
46      var actual = service.getSubscription(1L, "bob");
47      assertEquals(sub, actual);
48    }
49  
50    @Test
51    public void getSubscriptionTestNull() {
52      when(dao.findByNeo4jId(1L)).thenThrow(InvalidPathException.class);
53  
54      assertThrows(InvalidPathException.class, () -> service.getSubscription(1L, "bob"));
55    }
56  
57    @Test
58    public void getAllSubscriptionsTest() {
59      var sub = new Subscription(1L);
60      var user = new User("bob");
61      user.setSubscriptions(List.of(sub));
62  
63      when(userService.getUser("bob")).thenReturn(user);
64      var actual = service.getAllSubscriptions("bob");
65  
66      assertEquals(List.of(sub), actual);
67    }
68  
69    @Test
70    public void getMatchingSubscriptionsTest() {
71      var sub = new Subscription(1L);
72  
73      // unfortunately two equally created filters are not equal,
74      // so we have to use any here
75      when(dao.findMatching(any(Filter.class))).thenReturn(List.of(sub));
76      var actual = service.getMatchingSubscriptions(RequestMethod.GET);
77  
78      assertEquals(List.of(sub), actual);
79    }
80  
81    @Test
82    public void getAllSubscriptionsTest_noUser() {
83      when(userService.getUser("bob")).thenThrow(InvalidPathException.class);
84  
85      assertThrows(InvalidPathException.class, () -> service.getAllSubscriptions("bob"));
86    }
87  
88    @Test
89    public void deleteSubscriptionTest() {
90      var sub = new Subscription(1L);
91      var subscriptionUser = new User("bob");
92      sub.setCreatedBy(subscriptionUser);
93      when(dao.findByNeo4jId(1L)).thenReturn(sub);
94      when(dao.deleteByNeo4jId(1L)).thenReturn(true);
95      var actual = service.deleteSubscription(1L, "bob");
96  
97      assertTrue(actual);
98    }
99  
100   @Test
101   public void createTest() {
102     var user = new User("bob");
103     var date = new Date(30L);
104 
105     var input = new SubscriptionIO() {
106       {
107         setCallbackURL("callback");
108         setName("MySub");
109         setRequestMethod(RequestMethod.GET);
110         setSubscribedURL("subUrl");
111       }
112     };
113 
114     var toCreate = new Subscription() {
115       {
116         setCallbackURL("callback");
117         setName("MySub");
118         setRequestMethod(RequestMethod.GET);
119         setSubscribedURL("subUrl");
120         setCreatedAt(date);
121         setCreatedBy(user);
122       }
123     };
124 
125     var created = new Subscription() {
126       {
127         setId(1L);
128         setCallbackURL("callback");
129         setName("MySub");
130         setRequestMethod(RequestMethod.GET);
131         setSubscribedURL("subUrl");
132         setCreatedAt(date);
133         setCreatedBy(user);
134       }
135     };
136 
137     when(userService.getUser("bob")).thenReturn(user);
138     when(dateHelper.getDate()).thenReturn(date);
139     when(dao.createOrUpdate(toCreate)).thenReturn(created);
140 
141     var actual = service.createSubscription(input, "bob");
142     assertEquals(created, actual);
143   }
144 }