Skip to content

Commit 820d021

Browse files
monikakusterivicac
authored andcommitted
2352 fix tests
1 parent 3442c74 commit 820d021

File tree

2 files changed

+1450
-40
lines changed

2 files changed

+1450
-40
lines changed

server/libs/modules/components/agile-crm/src/test/java/com/bytechef/component/agile/crm/trigger/AgileCrmNewTaskTriggerTest.java

Lines changed: 116 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@
1919
import static com.bytechef.component.agile.crm.constant.AgileCrmConstants.CREATED_TIME;
2020
import static com.bytechef.component.agile.crm.constant.AgileCrmConstants.LAST_TIME_CHECKED;
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.mockito.ArgumentCaptor.forClass;
2224
import static org.mockito.ArgumentMatchers.any;
2325
import static org.mockito.Mockito.mock;
2426
import static org.mockito.Mockito.mockStatic;
2527
import static org.mockito.Mockito.when;
2628

29+
import com.bytechef.component.definition.Context.ContextFunction;
2730
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;
2834
import com.bytechef.component.definition.Parameters;
2935
import com.bytechef.component.definition.TriggerContext;
3036
import com.bytechef.component.definition.TriggerDefinition.PollOutput;
@@ -34,52 +40,146 @@
3440
import java.util.List;
3541
import java.util.Map;
3642
import org.junit.jupiter.api.Test;
43+
import org.mockito.ArgumentCaptor;
3744
import org.mockito.MockedStatic;
3845

3946
/**
4047
* @author Nikolina Spehar
4148
*/
4249
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);
4356
private final Http.Executor mockedExecutor = mock(Http.Executor.class);
57+
private final Http mockedHttp = mock(Http.class);
4458
private final Http.Response mockedResponse = mock(Http.Response.class);
4559
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);
4961

5062
@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()))
5371
.thenReturn(mockedExecutor);
54-
when(mockedExecutor.configuration(any()))
72+
when(mockedExecutor.configuration(configurationBuilderArgumentCaptor.capture()))
5573
.thenReturn(mockedExecutor);
5674
when(mockedExecutor.execute())
5775
.thenReturn(mockedResponse);
58-
when(mockedResponse.getBody(any(TypeReference.class)))
59-
.thenReturn(responseList);
6076

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")
6282
.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");
6398

64-
Instant mockedNow = Instant.parse("2025-01-01T07:00:00Z");
99+
Instant mockedNow = Instant.parse("2025-01-01T08:00:00Z");
65100

66101
try (MockedStatic<Instant> instantMockedStatic = mockStatic(Instant.class)) {
67-
instantMockedStatic
68-
.when(Instant::now)
102+
instantMockedStatic.when(Instant::now)
69103
.thenReturn(mockedNow);
70104

71105
Parameters mockedParameters = MockParametersFactory.create(
72106
Map.of(LAST_TIME_CHECKED, mockedLastTimeChecked));
73107

74-
PollOutput pollOutput = AgileCrmNewTaskTrigger.poll(
75-
mockedParameters, mockedParameters, mockedParameters, mockedTriggerContext);
108+
PollOutput pollOutput = AgileCrmNewTaskTrigger.poll(null, null, mockedParameters, mockedTriggerContext);
76109

77110
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),
80117
false);
81118

82119
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());
83183
}
84184
}
85185
}

0 commit comments

Comments
 (0)