|
18 | 18 |
|
19 | 19 | import static com.bytechef.component.brevo.constant.BrevoConstants.ID; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.mockito.ArgumentCaptor.forClass; |
21 | 23 | import static org.mockito.ArgumentMatchers.any; |
22 | 24 | import static org.mockito.Mockito.mock; |
23 | 25 | import static org.mockito.Mockito.when; |
24 | 26 |
|
| 27 | +import com.bytechef.component.definition.Context.ContextFunction; |
25 | 28 | 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; |
26 | 35 | import com.bytechef.component.definition.Parameters; |
27 | 36 | import com.bytechef.component.definition.TriggerContext; |
28 | | -import com.bytechef.component.definition.TriggerDefinition.HttpHeaders; |
29 | | -import com.bytechef.component.definition.TriggerDefinition.HttpParameters; |
30 | 37 | import com.bytechef.component.definition.TriggerDefinition.WebhookBody; |
31 | 38 | import com.bytechef.component.definition.TriggerDefinition.WebhookEnableOutput; |
32 | | -import com.bytechef.component.definition.TriggerDefinition.WebhookMethod; |
33 | 39 | import com.bytechef.component.definition.TypeReference; |
34 | 40 | import com.bytechef.component.test.definition.MockParametersFactory; |
35 | 41 | import java.util.List; |
|
42 | 48 | */ |
43 | 49 | class BrevoTransactionalEmailOpenedTriggerTest { |
44 | 50 |
|
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); |
47 | 52 | 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")); |
52 | 53 | private final TriggerContext mockedTriggerContext = mock(TriggerContext.class); |
53 | 54 | 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); |
56 | 63 |
|
57 | 64 | @Test |
58 | 65 | void testWebhookEnable() { |
59 | 66 | String webhookUrl = "testWebhookUrl"; |
60 | | - String workflowExecutionId = "testWorkflowExecutionId"; |
61 | 67 |
|
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())) |
63 | 75 | .thenReturn(mockedExecutor); |
64 | 76 | when(mockedExecutor.body(bodyArgumentCaptor.capture())) |
65 | 77 | .thenReturn(mockedExecutor); |
66 | | - when(mockedExecutor.configuration(any())) |
| 78 | + when(mockedExecutor.configuration(configurationBuilderArgumentCaptor.capture())) |
67 | 79 | .thenReturn(mockedExecutor); |
68 | 80 | when(mockedExecutor.execute()) |
69 | 81 | .thenReturn(mockedResponse); |
70 | 82 | when(mockedResponse.getBody(any(TypeReference.class))) |
71 | 83 | .thenReturn(Map.of(ID, "123")); |
72 | 84 |
|
73 | 85 | WebhookEnableOutput webhookEnableOutput = BrevoTransactionalEmailOpenedTrigger.webhookEnable( |
74 | | - mockedParameters, mockedParameters, webhookUrl, workflowExecutionId, mockedTriggerContext); |
| 86 | + null, null, webhookUrl, null, mockedTriggerContext); |
75 | 87 |
|
76 | | - WebhookEnableOutput expectedWebhookEnableOutput = new WebhookEnableOutput(Map.of(ID, "123"), null); |
| 88 | + WebhookEnableOutput expectedWebhookEnableOutput = |
| 89 | + new WebhookEnableOutput(Map.of(ID, "123"), null); |
77 | 90 |
|
78 | 91 | assertEquals(expectedWebhookEnableOutput, webhookEnableOutput); |
79 | 92 |
|
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(); |
81 | 107 |
|
82 | 108 | assertEquals(Map.of("url", webhookUrl, "events", List.of("opened")), body.getContent()); |
83 | 109 | } |
84 | 110 |
|
| 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 | + |
85 | 135 | @Test |
86 | 136 | void testWebhookRequest() { |
87 | 137 | when(mockedWebhookBody.getContent()) |
88 | 138 | .thenReturn(mockedObject); |
89 | 139 |
|
90 | 140 | Object result = BrevoTransactionalEmailOpenedTrigger.webhookRequest( |
91 | | - mockedParameters, mockedParameters, mockedHttpHeaders, mockedHttpParameters, mockedWebhookBody, |
92 | | - mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext); |
| 141 | + null, null, null, null, mockedWebhookBody, |
| 142 | + null, null, null); |
93 | 143 |
|
94 | 144 | assertEquals(mockedObject, result); |
95 | 145 | } |
|
0 commit comments