Skip to content

Commit f7dede4

Browse files
marijahorvat171ivicac
authored andcommitted
2040 - fix tests and actions
1 parent 080f65f commit f7dede4

7 files changed

Lines changed: 238 additions & 59 deletions

File tree

server/libs/modules/components/brevo/src/main/java/com/bytechef/component/brevo/action/BrevoUpdateContactAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import static com.bytechef.component.definition.ComponentDsl.action;
2323
import static com.bytechef.component.definition.ComponentDsl.string;
2424
import static com.bytechef.component.definition.Context.Http.Body;
25+
import static com.bytechef.component.definition.Context.Http.responseType;
2526

2627
import com.bytechef.component.brevo.util.BrevoUtils;
2728
import com.bytechef.component.definition.ActionDefinition.OptionsFunction;
2829
import com.bytechef.component.definition.ComponentDsl.ModifiableActionDefinition;
2930
import com.bytechef.component.definition.Context;
31+
import com.bytechef.component.definition.Context.Http;
3032
import com.bytechef.component.definition.Parameters;
3133

3234
/**
@@ -66,6 +68,7 @@ public static Object perform(Parameters inputParameters, Parameters connectionPa
6668
FIRST_NAME, inputParameters.getString(FIRST_NAME),
6769
LAST_NAME, inputParameters.getString(LAST_NAME)
6870
}))
71+
.configuration(responseType(Http.ResponseType.JSON))
6972
.execute();
7073

7174
return null;

server/libs/modules/components/brevo/src/test/java/com/bytechef/component/brevo/action/AbstractBrevoActionTest.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,34 @@
1616

1717
package com.bytechef.component.brevo.action;
1818

19+
import static org.mockito.ArgumentCaptor.forClass;
1920
import static org.mockito.ArgumentMatchers.any;
20-
import static org.mockito.Mockito.mock;
2121
import static org.mockito.Mockito.when;
2222

23-
import com.bytechef.component.definition.Context;
24-
import com.bytechef.component.definition.Context.Http;
23+
import com.bytechef.component.definition.Context.Http.Body;
24+
import com.bytechef.component.definition.Context.Http.Executor;
25+
import com.bytechef.component.definition.Context.Http.Response;
2526
import com.bytechef.component.definition.TypeReference;
27+
import com.bytechef.component.test.definition.extension.MockContextSetupExtension;
2628
import java.util.Map;
2729
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.extension.ExtendWith;
2831
import org.mockito.ArgumentCaptor;
2932

3033
/**
3134
* @author Marija Horvat
3235
*/
36+
@ExtendWith(MockContextSetupExtension.class)
3337
abstract class AbstractBrevoActionTest {
3438

35-
protected Context mockedContext = mock(Context.class);
36-
protected Http.Executor mockedExecutor = mock(Http.Executor.class);
37-
protected Http.Response mockedResponse = mock(Http.Response.class);
3839
protected Map<String, Object> responseMap = Map.of("key", "value");
39-
protected ArgumentCaptor<Http.Body> bodyArgumentCaptor = ArgumentCaptor.forClass(Http.Body.class);
40+
protected ArgumentCaptor<Body> bodyArgumentCaptor = forClass(Body.class);
41+
protected ArgumentCaptor<String> stringArgumentCaptor = forClass(String.class);
4042

4143
@BeforeEach
42-
void beforeEach() {
43-
when(mockedContext.http(any()))
44-
.thenReturn(mockedExecutor);
44+
void beforeEach(Response mockedResponse, Executor mockedExecutor) {
4545
when(mockedExecutor.body(bodyArgumentCaptor.capture()))
4646
.thenReturn(mockedExecutor);
47-
when(mockedExecutor.configuration(any()))
48-
.thenReturn(mockedExecutor);
49-
when(mockedExecutor.execute())
50-
.thenReturn(mockedResponse);
5147
when(mockedResponse.getBody(any(TypeReference.class)))
5248
.thenReturn(responseMap);
5349
}

server/libs/modules/components/brevo/src/test/java/com/bytechef/component/brevo/action/BrevoCreateContactActionTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,22 @@
2020
import static com.bytechef.component.brevo.constant.BrevoConstants.FIRST_NAME;
2121
import static com.bytechef.component.brevo.constant.BrevoConstants.LAST_NAME;
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.mockito.Mockito.when;
2325

26+
import com.bytechef.component.definition.Context;
27+
import com.bytechef.component.definition.Context.ContextFunction;
2428
import com.bytechef.component.definition.Context.Http;
29+
import com.bytechef.component.definition.Context.Http.Body;
30+
import com.bytechef.component.definition.Context.Http.Configuration;
31+
import com.bytechef.component.definition.Context.Http.Configuration.ConfigurationBuilder;
32+
import com.bytechef.component.definition.Context.Http.Executor;
33+
import com.bytechef.component.definition.Context.Http.ResponseType;
2534
import com.bytechef.component.definition.Parameters;
2635
import com.bytechef.component.test.definition.MockParametersFactory;
2736
import java.util.Map;
2837
import org.junit.jupiter.api.Test;
38+
import org.mockito.ArgumentCaptor;
2939

3040
/**
3141
* @author Marija Horvat
@@ -36,12 +46,32 @@ class BrevoCreateContactActionTest extends AbstractBrevoActionTest {
3646
Map.of(EMAIL, "test@test.com", FIRST_NAME, "test", LAST_NAME, "test"));
3747

3848
@Test
39-
void testPerform() {
40-
Object result = BrevoCreateContactAction.perform(mockedParameters, mockedParameters, mockedContext);
49+
void testPerform(
50+
Context mockedContext, Executor mockedExecutor, Http mockedHttp,
51+
ArgumentCaptor<ContextFunction<Http, Executor>> httpFunctionArgumentCaptor,
52+
ArgumentCaptor<ConfigurationBuilder> configurationBuilderArgumentCaptor) {
53+
54+
when(mockedHttp.post(stringArgumentCaptor.capture()))
55+
.thenReturn(mockedExecutor);
56+
57+
Object result = BrevoCreateContactAction.perform(mockedParameters, null, mockedContext);
4158

4259
assertEquals(responseMap, result);
4360

44-
Http.Body body = bodyArgumentCaptor.getValue();
61+
ContextFunction<Http, Executor> capturedFunction = httpFunctionArgumentCaptor.getValue();
62+
63+
assertNotNull(capturedFunction);
64+
65+
ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue();
66+
67+
Configuration configuration = configurationBuilder.build();
68+
69+
ResponseType responseType = configuration.getResponseType();
70+
71+
assertEquals(ResponseType.Type.JSON, responseType.getType());
72+
assertEquals("/contacts/", stringArgumentCaptor.getValue());
73+
74+
Body body = bodyArgumentCaptor.getValue();
4575
Map<String, Object> expected = Map.of(
4676
EMAIL, "test@test.com",
4777
"attributes", Map.of(FIRST_NAME, "test", LAST_NAME, "test"));

server/libs/modules/components/brevo/src/test/java/com/bytechef/component/brevo/action/BrevoSendTransactionalEmailActionTest.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@
2525
import static com.bytechef.component.brevo.constant.BrevoConstants.SUBJECT;
2626
import static com.bytechef.component.brevo.constant.BrevoConstants.TO;
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.mockito.Mockito.when;
2830

31+
import com.bytechef.component.definition.Context;
32+
import com.bytechef.component.definition.Context.ContextFunction;
2933
import com.bytechef.component.definition.Context.Http;
34+
import com.bytechef.component.definition.Context.Http.Body;
35+
import com.bytechef.component.definition.Context.Http.Configuration;
36+
import com.bytechef.component.definition.Context.Http.Configuration.ConfigurationBuilder;
37+
import com.bytechef.component.definition.Context.Http.Executor;
38+
import com.bytechef.component.definition.Context.Http.ResponseType;
3039
import com.bytechef.component.definition.Parameters;
3140
import com.bytechef.component.test.definition.MockParametersFactory;
3241
import java.util.List;
3342
import java.util.Map;
3443
import org.junit.jupiter.api.Test;
44+
import org.mockito.ArgumentCaptor;
3545

3646
/**
3747
* @author Marija Horvat
@@ -49,11 +59,32 @@ class BrevoSendTransactionalEmailActionTest extends AbstractBrevoActionTest {
4959
CONTENT, "this is a test."));
5060

5161
@Test
52-
void testPerform() {
53-
Object result = BrevoSendTransactionalEmailAction.perform(mockedParameters, mockedParameters, mockedContext);
62+
void testPerform(
63+
Context mockedContext, Executor mockedExecutor, Http mockedHttp,
64+
ArgumentCaptor<ContextFunction<Http, Executor>> httpFunctionArgumentCaptor,
65+
ArgumentCaptor<ConfigurationBuilder> configurationBuilderArgumentCaptor) {
66+
67+
when(mockedHttp.post(stringArgumentCaptor.capture()))
68+
.thenReturn(mockedExecutor);
69+
70+
Object result = BrevoSendTransactionalEmailAction.perform(mockedParameters, null, mockedContext);
5471

5572
assertEquals(responseMap, result);
56-
Http.Body body = bodyArgumentCaptor.getValue();
73+
74+
ContextFunction<Http, Executor> capturedFunction = httpFunctionArgumentCaptor.getValue();
75+
76+
assertNotNull(capturedFunction);
77+
78+
ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue();
79+
80+
Configuration configuration = configurationBuilder.build();
81+
82+
ResponseType responseType = configuration.getResponseType();
83+
84+
assertEquals(ResponseType.Type.JSON, responseType.getType());
85+
assertEquals("/smtp/email/", stringArgumentCaptor.getValue());
86+
87+
Body body = bodyArgumentCaptor.getValue();
5788

5889
Map<String, Object> expectedBody = Map.of(
5990
"sender", Map.of(EMAIL, "sender@test.com"),

server/libs/modules/components/brevo/src/test/java/com/bytechef/component/brevo/action/BrevoUpdateContactActionTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@
2020
import static com.bytechef.component.brevo.constant.BrevoConstants.FIRST_NAME;
2121
import static com.bytechef.component.brevo.constant.BrevoConstants.LAST_NAME;
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2324
import static org.junit.jupiter.api.Assertions.assertNull;
25+
import static org.mockito.Mockito.when;
2426

27+
import com.bytechef.component.definition.Context;
28+
import com.bytechef.component.definition.Context.ContextFunction;
2529
import com.bytechef.component.definition.Context.Http;
30+
import com.bytechef.component.definition.Context.Http.Body;
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.Executor;
34+
import com.bytechef.component.definition.Context.Http.ResponseType;
2635
import com.bytechef.component.definition.Parameters;
2736
import com.bytechef.component.test.definition.MockParametersFactory;
2837
import java.util.Map;
2938
import org.junit.jupiter.api.Test;
39+
import org.mockito.ArgumentCaptor;
3040

3141
/**
3242
* @author Marija Horvat
@@ -37,12 +47,32 @@ class BrevoUpdateContactActionTest extends AbstractBrevoActionTest {
3747
Map.of(EMAIL, "test@test.com", FIRST_NAME, "test", LAST_NAME, "test"));
3848

3949
@Test
40-
void testPerform() {
41-
Object result = BrevoUpdateContactAction.perform(mockedParameters, mockedParameters, mockedContext);
50+
void testPerform(
51+
Context mockedContext, Executor mockedExecutor, Http mockedHttp,
52+
ArgumentCaptor<ContextFunction<Http, Executor>> httpFunctionArgumentCaptor,
53+
ArgumentCaptor<ConfigurationBuilder> configurationBuilderArgumentCaptor) {
54+
55+
when(mockedHttp.put(stringArgumentCaptor.capture()))
56+
.thenReturn(mockedExecutor);
57+
58+
Object result = BrevoUpdateContactAction.perform(mockedParameters, null, mockedContext);
4259

4360
assertNull(result);
4461

45-
Http.Body body = bodyArgumentCaptor.getValue();
62+
ContextFunction<Http, Executor> capturedFunction = httpFunctionArgumentCaptor.getValue();
63+
64+
assertNotNull(capturedFunction);
65+
66+
ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue();
67+
68+
Configuration configuration = configurationBuilder.build();
69+
70+
ResponseType responseType = configuration.getResponseType();
71+
72+
assertEquals(ResponseType.Type.JSON, responseType.getType());
73+
assertEquals("/contacts/test@test.com", stringArgumentCaptor.getValue());
74+
75+
Body body = bodyArgumentCaptor.getValue();
4676

4777
Map<String, Object> expectedMap = Map.of("attributes", Map.of(FIRST_NAME, "test", LAST_NAME, "test"));
4878

server/libs/modules/components/brevo/src/test/java/com/bytechef/component/brevo/trigger/BrevoTransactionalEmailOpenedTriggerTest.java

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@
1818

1919
import static com.bytechef.component.brevo.constant.BrevoConstants.ID;
2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.mockito.ArgumentCaptor.forClass;
2123
import static org.mockito.ArgumentMatchers.any;
2224
import static org.mockito.Mockito.mock;
2325
import static org.mockito.Mockito.when;
2426

27+
import com.bytechef.component.definition.Context.ContextFunction;
2528
import com.bytechef.component.definition.Context.Http;
29+
import com.bytechef.component.definition.Context.Http.Body;
30+
import com.bytechef.component.definition.Context.Http.Configuration;
31+
import com.bytechef.component.definition.Context.Http.Configuration.ConfigurationBuilder;
32+
import com.bytechef.component.definition.Context.Http.Executor;
33+
import com.bytechef.component.definition.Context.Http.Response;
34+
import com.bytechef.component.definition.Context.Http.ResponseType;
2635
import com.bytechef.component.definition.Parameters;
2736
import com.bytechef.component.definition.TriggerContext;
28-
import com.bytechef.component.definition.TriggerDefinition.HttpHeaders;
29-
import com.bytechef.component.definition.TriggerDefinition.HttpParameters;
3037
import com.bytechef.component.definition.TriggerDefinition.WebhookBody;
3138
import com.bytechef.component.definition.TriggerDefinition.WebhookEnableOutput;
32-
import com.bytechef.component.definition.TriggerDefinition.WebhookMethod;
3339
import com.bytechef.component.definition.TypeReference;
3440
import com.bytechef.component.test.definition.MockParametersFactory;
3541
import java.util.List;
@@ -42,54 +48,98 @@
4248
*/
4349
class BrevoTransactionalEmailOpenedTriggerTest {
4450

45-
private final Parameters mockedWebhookEnableOutput = mock(Parameters.class);
46-
private final ArgumentCaptor<Http.Body> bodyArgumentCaptor = ArgumentCaptor.forClass(Http.Body.class);
51+
private final ArgumentCaptor<Body> bodyArgumentCaptor = forClass(Body.class);
4752
private final WebhookBody mockedWebhookBody = mock(WebhookBody.class);
48-
private final HttpHeaders mockedHttpHeaders = mock(HttpHeaders.class);
49-
private final HttpParameters mockedHttpParameters = mock(HttpParameters.class);
50-
private final WebhookMethod mockedWebhookMethod = mock(WebhookMethod.class);
51-
private final Parameters mockedParameters = MockParametersFactory.create(Map.of(ID, "id"));
5253
private final TriggerContext mockedTriggerContext = mock(TriggerContext.class);
5354
private final Object mockedObject = mock(Object.class);
54-
private final Http.Executor mockedExecutor = mock(Http.Executor.class);
55-
private final Http.Response mockedResponse = mock(Http.Response.class);
55+
private final Executor mockedExecutor = mock(Executor.class);
56+
private final Response mockedResponse = mock(Response.class);
57+
private final Http mockedHttp = mock(Http.class);
58+
private final ArgumentCaptor<ContextFunction<Http, Executor>> httpFunctionArgumentCaptor =
59+
forClass(ContextFunction.class);
60+
private final ArgumentCaptor<String> stringArgumentCaptor = forClass(String.class);
61+
private final ArgumentCaptor<ConfigurationBuilder> configurationBuilderArgumentCaptor =
62+
forClass(ConfigurationBuilder.class);
5663

5764
@Test
5865
void testWebhookEnable() {
5966
String webhookUrl = "testWebhookUrl";
60-
String workflowExecutionId = "testWorkflowExecutionId";
6167

62-
when(mockedTriggerContext.http(any()))
68+
when(mockedTriggerContext.http(httpFunctionArgumentCaptor.capture()))
69+
.thenAnswer(inv -> {
70+
ContextFunction<Http, Executor> value = httpFunctionArgumentCaptor.getValue();
71+
72+
return value.apply(mockedHttp);
73+
});
74+
when(mockedHttp.post(stringArgumentCaptor.capture()))
6375
.thenReturn(mockedExecutor);
6476
when(mockedExecutor.body(bodyArgumentCaptor.capture()))
6577
.thenReturn(mockedExecutor);
66-
when(mockedExecutor.configuration(any()))
78+
when(mockedExecutor.configuration(configurationBuilderArgumentCaptor.capture()))
6779
.thenReturn(mockedExecutor);
6880
when(mockedExecutor.execute())
6981
.thenReturn(mockedResponse);
7082
when(mockedResponse.getBody(any(TypeReference.class)))
7183
.thenReturn(Map.of(ID, "123"));
7284

7385
WebhookEnableOutput webhookEnableOutput = BrevoTransactionalEmailOpenedTrigger.webhookEnable(
74-
mockedParameters, mockedParameters, webhookUrl, workflowExecutionId, mockedTriggerContext);
86+
null, null, webhookUrl, null, mockedTriggerContext);
7587

76-
WebhookEnableOutput expectedWebhookEnableOutput = new WebhookEnableOutput(Map.of(ID, "123"), null);
88+
WebhookEnableOutput expectedWebhookEnableOutput =
89+
new WebhookEnableOutput(Map.of(ID, "123"), null);
7790

7891
assertEquals(expectedWebhookEnableOutput, webhookEnableOutput);
7992

80-
Http.Body body = bodyArgumentCaptor.getValue();
93+
ContextFunction<Http, Executor> capturedFunction = httpFunctionArgumentCaptor.getValue();
94+
95+
assertNotNull(capturedFunction);
96+
97+
ConfigurationBuilder configurationBuilder = configurationBuilderArgumentCaptor.getValue();
98+
99+
Configuration configuration = configurationBuilder.build();
100+
101+
ResponseType responseType = configuration.getResponseType();
102+
103+
assertEquals(ResponseType.Type.JSON, responseType.getType());
104+
assertEquals("/webhooks", stringArgumentCaptor.getValue());
105+
106+
Body body = bodyArgumentCaptor.getValue();
81107

82108
assertEquals(Map.of("url", webhookUrl, "events", List.of("opened")), body.getContent());
83109
}
84110

111+
@Test
112+
void testWebhookDisable() {
113+
Parameters mockedParameters = MockParametersFactory.create(Map.of(ID, 123));
114+
115+
when(mockedTriggerContext.http(httpFunctionArgumentCaptor.capture()))
116+
.thenAnswer(inv -> {
117+
ContextFunction<Http, Executor> value = httpFunctionArgumentCaptor.getValue();
118+
119+
return value.apply(mockedHttp);
120+
});
121+
when(mockedHttp.delete(stringArgumentCaptor.capture()))
122+
.thenReturn(mockedExecutor);
123+
when(mockedExecutor.execute())
124+
.thenReturn(mockedResponse);
125+
126+
BrevoTransactionalEmailOpenedTrigger.webhookDisable(
127+
null, null, mockedParameters, null, mockedTriggerContext);
128+
129+
ContextFunction<Http, Executor> capturedFunction = httpFunctionArgumentCaptor.getValue();
130+
131+
assertNotNull(capturedFunction);
132+
assertEquals("/webhooks/123", stringArgumentCaptor.getValue());
133+
}
134+
85135
@Test
86136
void testWebhookRequest() {
87137
when(mockedWebhookBody.getContent())
88138
.thenReturn(mockedObject);
89139

90140
Object result = BrevoTransactionalEmailOpenedTrigger.webhookRequest(
91-
mockedParameters, mockedParameters, mockedHttpHeaders, mockedHttpParameters, mockedWebhookBody,
92-
mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext);
141+
null, null, null, null, mockedWebhookBody,
142+
null, null, null);
93143

94144
assertEquals(mockedObject, result);
95145
}

0 commit comments

Comments
 (0)