|
19 | 19 | import static com.bytechef.component.agile.crm.constant.AgileCrmConstants.CREATED_TIME; |
20 | 20 | import static com.bytechef.component.agile.crm.constant.AgileCrmConstants.LAST_TIME_CHECKED; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.mockito.ArgumentCaptor.forClass; |
22 | 24 | import static org.mockito.ArgumentMatchers.any; |
23 | 25 | import static org.mockito.Mockito.mock; |
24 | 26 | import static org.mockito.Mockito.mockStatic; |
25 | 27 | import static org.mockito.Mockito.when; |
26 | 28 |
|
| 29 | +import com.bytechef.component.definition.Context.ContextFunction; |
27 | 30 | import com.bytechef.component.definition.Context.Http; |
| 31 | +import com.bytechef.component.definition.Context.Http.Configuration; |
| 32 | +import com.bytechef.component.definition.Context.Http.Configuration.ConfigurationBuilder; |
| 33 | +import com.bytechef.component.definition.Context.Http.ResponseType; |
28 | 34 | import com.bytechef.component.definition.Parameters; |
29 | 35 | import com.bytechef.component.definition.TriggerContext; |
30 | 36 | import com.bytechef.component.definition.TriggerDefinition.PollOutput; |
|
34 | 40 | import java.util.List; |
35 | 41 | import java.util.Map; |
36 | 42 | import org.junit.jupiter.api.Test; |
| 43 | +import org.mockito.ArgumentCaptor; |
37 | 44 | import org.mockito.MockedStatic; |
38 | 45 |
|
39 | 46 | /** |
40 | 47 | * @author Nikolina Spehar |
41 | 48 | */ |
42 | 49 | class AgileCrmNewTaskTriggerTest { |
| 50 | + |
| 51 | + private final ArgumentCaptor<ConfigurationBuilder> configurationBuilderArgumentCaptor = |
| 52 | + forClass(ConfigurationBuilder.class); |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + private final ArgumentCaptor<ContextFunction<Http, Http.Executor>> httpFunctionArgumentCaptor = |
| 55 | + forClass(ContextFunction.class); |
43 | 56 | private final Http.Executor mockedExecutor = mock(Http.Executor.class); |
| 57 | + private final Http mockedHttp = mock(Http.class); |
44 | 58 | private final Http.Response mockedResponse = mock(Http.Response.class); |
45 | 59 | private final TriggerContext mockedTriggerContext = mock(TriggerContext.class); |
46 | | - private final long mockedCreatedTime = Instant.parse("2025-01-01T05:00:00Z") |
47 | | - .getEpochSecond(); |
48 | | - private final List<Map<String, Object>> responseList = List.of(Map.of(CREATED_TIME, (int) mockedCreatedTime)); |
| 60 | + private final ArgumentCaptor<String> stringArgumentCaptor = forClass(String.class); |
49 | 61 |
|
50 | 62 | @Test |
51 | | - void poll() { |
52 | | - when(mockedTriggerContext.http(any())) |
| 63 | + void testPoll() { |
| 64 | + when(mockedTriggerContext.http(httpFunctionArgumentCaptor.capture())) |
| 65 | + .thenAnswer(inv -> { |
| 66 | + ContextFunction<Http, Http.Executor> value = httpFunctionArgumentCaptor.getValue(); |
| 67 | + |
| 68 | + return value.apply(mockedHttp); |
| 69 | + }); |
| 70 | + when(mockedHttp.get(stringArgumentCaptor.capture())) |
53 | 71 | .thenReturn(mockedExecutor); |
54 | | - when(mockedExecutor.configuration(any())) |
| 72 | + when(mockedExecutor.configuration(configurationBuilderArgumentCaptor.capture())) |
55 | 73 | .thenReturn(mockedExecutor); |
56 | 74 | when(mockedExecutor.execute()) |
57 | 75 | .thenReturn(mockedResponse); |
58 | | - when(mockedResponse.getBody(any(TypeReference.class))) |
59 | | - .thenReturn(responseList); |
60 | 76 |
|
61 | | - long mockedLastTimeChecked = Instant.parse("2025-01-01T04:00:00Z") |
| 77 | + long older = Instant.parse("2025-01-01T03:30:00Z") |
| 78 | + .getEpochSecond(); |
| 79 | + long equalToThreshold = Instant.parse("2025-01-01T04:00:00Z") |
| 80 | + .getEpochSecond(); |
| 81 | + long newer1 = Instant.parse("2025-01-01T06:00:00Z") |
62 | 82 | .getEpochSecond(); |
| 83 | + long newer2 = Instant.parse("2025-01-01T05:00:00Z") |
| 84 | + .getEpochSecond(); |
| 85 | + long newest = Instant.parse("2025-01-01T07:00:00Z") |
| 86 | + .getEpochSecond(); |
| 87 | + |
| 88 | + when(mockedResponse.getBody(any(TypeReference.class))) |
| 89 | + .thenReturn( |
| 90 | + List.of( |
| 91 | + Map.of(CREATED_TIME, (int) newer1), |
| 92 | + Map.of(CREATED_TIME, (int) older), |
| 93 | + Map.of(CREATED_TIME, (int) newest), |
| 94 | + Map.of(CREATED_TIME, (int) newer2), |
| 95 | + Map.of(CREATED_TIME, (int) equalToThreshold))); |
| 96 | + |
| 97 | + Instant mockedLastTimeChecked = Instant.parse("2025-01-01T04:00:00Z"); |
63 | 98 |
|
64 | | - Instant mockedNow = Instant.parse("2025-01-01T07:00:00Z"); |
| 99 | + Instant mockedNow = Instant.parse("2025-01-01T08:00:00Z"); |
65 | 100 |
|
66 | 101 | try (MockedStatic<Instant> instantMockedStatic = mockStatic(Instant.class)) { |
67 | | - instantMockedStatic |
68 | | - .when(Instant::now) |
| 102 | + instantMockedStatic.when(Instant::now) |
69 | 103 | .thenReturn(mockedNow); |
70 | 104 |
|
71 | 105 | Parameters mockedParameters = MockParametersFactory.create( |
72 | 106 | Map.of(LAST_TIME_CHECKED, mockedLastTimeChecked)); |
73 | 107 |
|
74 | | - PollOutput pollOutput = AgileCrmNewTaskTrigger.poll( |
75 | | - mockedParameters, mockedParameters, mockedParameters, mockedTriggerContext); |
| 108 | + PollOutput pollOutput = AgileCrmNewTaskTrigger.poll(null, null, mockedParameters, mockedTriggerContext); |
76 | 109 |
|
77 | 110 | PollOutput expectedPollOutput = new PollOutput( |
78 | | - List.of(Map.of(CREATED_TIME, (int) mockedCreatedTime)), |
79 | | - Map.of(LAST_TIME_CHECKED, mockedNow.getEpochSecond()), |
| 111 | + List.of( |
| 112 | + Map.of(CREATED_TIME, (int) newest), |
| 113 | + Map.of(CREATED_TIME, (int) newer1), |
| 114 | + Map.of(CREATED_TIME, (int) newer2), |
| 115 | + Map.of(CREATED_TIME, (int) equalToThreshold)), |
| 116 | + Map.of(LAST_TIME_CHECKED, mockedNow), |
80 | 117 | false); |
81 | 118 |
|
82 | 119 | assertEquals(expectedPollOutput, pollOutput); |
| 120 | + |
| 121 | + ContextFunction<Http, Http.Executor> capturedFunction = httpFunctionArgumentCaptor.getValue(); |
| 122 | + |
| 123 | + assertNotNull(capturedFunction); |
| 124 | + |
| 125 | + ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue(); |
| 126 | + Configuration configuration = configurationBuilder.build(); |
| 127 | + ResponseType responseType = configuration.getResponseType(); |
| 128 | + |
| 129 | + assertEquals(ResponseType.Type.JSON, responseType.getType()); |
| 130 | + assertEquals("/tasks/based", stringArgumentCaptor.getValue()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testPollNoNewTasks() { |
| 136 | + when(mockedTriggerContext.http(httpFunctionArgumentCaptor.capture())) |
| 137 | + .thenAnswer(inv -> { |
| 138 | + ContextFunction<Http, Http.Executor> value = httpFunctionArgumentCaptor.getValue(); |
| 139 | + |
| 140 | + return value.apply(mockedHttp); |
| 141 | + }); |
| 142 | + when(mockedHttp.get(stringArgumentCaptor.capture())) |
| 143 | + .thenReturn(mockedExecutor); |
| 144 | + when(mockedExecutor.configuration(configurationBuilderArgumentCaptor.capture())) |
| 145 | + .thenReturn(mockedExecutor); |
| 146 | + when(mockedExecutor.execute()) |
| 147 | + .thenReturn(mockedResponse); |
| 148 | + when(mockedResponse.getBody(any(TypeReference.class))) |
| 149 | + .thenReturn( |
| 150 | + List.of( |
| 151 | + Map.of(CREATED_TIME, (int) Instant.parse("2025-01-01T00:00:00Z") |
| 152 | + .getEpochSecond()), |
| 153 | + Map.of(CREATED_TIME, (int) Instant.parse("2025-01-01T01:00:00Z") |
| 154 | + .getEpochSecond()))); |
| 155 | + |
| 156 | + Instant mockedLastTimeChecked = Instant.parse("2025-01-01T04:00:00Z"); |
| 157 | + |
| 158 | + Instant mockedNow = Instant.parse("2025-01-01T05:00:00Z"); |
| 159 | + |
| 160 | + try (MockedStatic<Instant> instantMockedStatic = mockStatic(Instant.class)) { |
| 161 | + instantMockedStatic.when(Instant::now) |
| 162 | + .thenReturn(mockedNow); |
| 163 | + |
| 164 | + Parameters mockedParameters = MockParametersFactory.create( |
| 165 | + Map.of(LAST_TIME_CHECKED, mockedLastTimeChecked)); |
| 166 | + |
| 167 | + PollOutput pollOutput = AgileCrmNewTaskTrigger.poll(null, null, mockedParameters, mockedTriggerContext); |
| 168 | + |
| 169 | + PollOutput expectedPollOutput = new PollOutput(List.of(), Map.of(LAST_TIME_CHECKED, mockedNow), false); |
| 170 | + |
| 171 | + assertEquals(expectedPollOutput, pollOutput); |
| 172 | + |
| 173 | + ContextFunction<Http, Http.Executor> capturedFunction = httpFunctionArgumentCaptor.getValue(); |
| 174 | + |
| 175 | + assertNotNull(capturedFunction); |
| 176 | + |
| 177 | + ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue(); |
| 178 | + Configuration configuration = configurationBuilder.build(); |
| 179 | + ResponseType responseType = configuration.getResponseType(); |
| 180 | + |
| 181 | + assertEquals(ResponseType.Type.JSON, responseType.getType()); |
| 182 | + assertEquals("/tasks/based", stringArgumentCaptor.getValue()); |
83 | 183 | } |
84 | 184 | } |
85 | 185 | } |
0 commit comments