|
| 1 | +/* |
| 2 | + * Copyright 2025 ByteChef |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.component.form.trigger; |
| 18 | + |
| 19 | +import static com.bytechef.component.form.constant.FormConstants.FIELD_NAME; |
| 20 | +import static com.bytechef.component.form.constant.FormConstants.FIELD_TYPE; |
| 21 | +import static com.bytechef.component.form.constant.FormConstants.IGNORE_BOTS; |
| 22 | +import static com.bytechef.component.form.constant.FormConstants.INPUTS; |
| 23 | +import static com.bytechef.component.form.constant.FormConstants.MULTIPLE_CHOICE; |
| 24 | +import static com.bytechef.component.form.constant.FormConstants.USE_WORKFLOW_TIMEZONE; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +import com.bytechef.component.definition.Parameters; |
| 34 | +import com.bytechef.component.definition.TriggerContext; |
| 35 | +import com.bytechef.component.definition.TriggerDefinition.HttpHeaders; |
| 36 | +import com.bytechef.component.definition.TriggerDefinition.HttpParameters; |
| 37 | +import com.bytechef.component.definition.TriggerDefinition.WebhookBody; |
| 38 | +import com.bytechef.component.definition.TriggerDefinition.WebhookMethod; |
| 39 | +import com.bytechef.component.form.constant.FormConstants.FieldType; |
| 40 | +import com.bytechef.component.test.definition.MockParametersFactory; |
| 41 | +import com.bytechef.definition.BaseOutputDefinition.OutputResponse; |
| 42 | +import com.bytechef.definition.BaseProperty; |
| 43 | +import com.bytechef.definition.BaseProperty.BaseObjectProperty; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.Optional; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | + |
| 49 | +/** |
| 50 | + * @author Ivica Cardic |
| 51 | + */ |
| 52 | +class NewFormRequestTriggerTest { |
| 53 | + |
| 54 | + private final TriggerContext mockedTriggerContext = mock(TriggerContext.class); |
| 55 | + private final HttpHeaders mockedHttpHeaders = mock(HttpHeaders.class); |
| 56 | + private final HttpParameters mockedHttpParameters = mock(HttpParameters.class); |
| 57 | + private final WebhookBody mockedWebhookBody = mock(WebhookBody.class); |
| 58 | + private final WebhookMethod mockedWebhookMethod = mock(WebhookMethod.class); |
| 59 | + private final Parameters mockedWebhookEnableOutput = mock(Parameters.class); |
| 60 | + |
| 61 | + @Test |
| 62 | + void testGetOutput() { |
| 63 | + Parameters inputParameters = MockParametersFactory.create( |
| 64 | + Map.of( |
| 65 | + INPUTS, List.of( |
| 66 | + Map.of(FIELD_NAME, "field1", FIELD_TYPE, FieldType.CHECKBOX.getValue()), |
| 67 | + Map.of(FIELD_NAME, "field2", FIELD_TYPE, FieldType.DATE_PICKER.getValue()), |
| 68 | + Map.of(FIELD_NAME, "field3", FIELD_TYPE, FieldType.DATETIME_PICKER.getValue()), |
| 69 | + Map.of(FIELD_NAME, "field4", FIELD_TYPE, FieldType.FILE_INPUT.getValue()), |
| 70 | + Map.of(FIELD_NAME, "field5", FIELD_TYPE, FieldType.NUMBER_INPUT.getValue()), |
| 71 | + Map.of(FIELD_NAME, "field6", FIELD_TYPE, FieldType.SELECT.getValue(), MULTIPLE_CHOICE, true), |
| 72 | + Map.of(FIELD_NAME, "field7", FIELD_TYPE, FieldType.SELECT.getValue(), MULTIPLE_CHOICE, false), |
| 73 | + Map.of(FIELD_NAME, "field8", FIELD_TYPE, FieldType.INPUT.getValue()), |
| 74 | + Map.of(FIELD_TYPE, FieldType.INPUT.getValue()) // Missing field name |
| 75 | + ))); |
| 76 | + |
| 77 | + OutputResponse outputResponse = NewFormRequestTrigger.getOutput( |
| 78 | + inputParameters, mock(Parameters.class), mockedTriggerContext); |
| 79 | + |
| 80 | + assertNotNull(outputResponse); |
| 81 | + |
| 82 | + BaseProperty.BaseValueProperty<?> outputSchema = outputResponse.getOutputSchema(); |
| 83 | + assertInstanceOf(BaseObjectProperty.class, outputSchema); |
| 84 | + |
| 85 | + BaseObjectProperty<?> objectProperty = (BaseObjectProperty<?>) outputSchema; |
| 86 | + |
| 87 | + List<? extends BaseProperty> properties = objectProperty.getProperties() |
| 88 | + .orElseThrow(); |
| 89 | + |
| 90 | + assertEquals(2, properties.size()); |
| 91 | + |
| 92 | + BaseProperty property1 = properties.get(0); |
| 93 | + |
| 94 | + assertEquals("submittedAt", property1.getName()); |
| 95 | + |
| 96 | + BaseProperty property2 = properties.get(1); |
| 97 | + |
| 98 | + assertEquals("body", property2.getName()); |
| 99 | + |
| 100 | + BaseObjectProperty<?> bodyProperty = (BaseObjectProperty<?>) property2; |
| 101 | + |
| 102 | + List<? extends BaseProperty> bodyProperties = bodyProperty.getProperties() |
| 103 | + .orElseThrow(); |
| 104 | + |
| 105 | + assertEquals(8, bodyProperties.size()); |
| 106 | + |
| 107 | + BaseProperty bodyProperty1 = bodyProperties.getFirst(); |
| 108 | + |
| 109 | + assertEquals("field1", bodyProperty1.getName()); |
| 110 | + assertInstanceOf(BaseProperty.BaseBooleanProperty.class, bodyProperty1); |
| 111 | + |
| 112 | + BaseProperty bodyProperty2 = bodyProperties.get(1); |
| 113 | + |
| 114 | + assertEquals("field2", bodyProperty2.getName()); |
| 115 | + assertInstanceOf(BaseProperty.BaseDateProperty.class, bodyProperty2); |
| 116 | + |
| 117 | + BaseProperty bodyProperty3 = bodyProperties.get(2); |
| 118 | + |
| 119 | + assertEquals("field3", bodyProperty3.getName()); |
| 120 | + assertInstanceOf(BaseProperty.BaseDateTimeProperty.class, bodyProperty3); |
| 121 | + |
| 122 | + BaseProperty bodyProperty4 = bodyProperties.get(3); |
| 123 | + |
| 124 | + assertEquals("field4", bodyProperty4.getName()); |
| 125 | + assertInstanceOf(BaseProperty.BaseFileEntryProperty.class, bodyProperty4); |
| 126 | + |
| 127 | + BaseProperty bodyProperty5 = bodyProperties.get(4); |
| 128 | + |
| 129 | + assertEquals("field5", bodyProperty5.getName()); |
| 130 | + assertInstanceOf(BaseProperty.BaseNumberProperty.class, bodyProperty5); |
| 131 | + |
| 132 | + BaseProperty bodyProperty6 = bodyProperties.get(5); |
| 133 | + |
| 134 | + assertEquals("field6", bodyProperty6.getName()); |
| 135 | + assertInstanceOf(BaseProperty.BaseArrayProperty.class, bodyProperty6); |
| 136 | + |
| 137 | + BaseProperty bodyProperty7 = bodyProperties.get(6); |
| 138 | + |
| 139 | + assertEquals("field7", bodyProperty7.getName()); |
| 140 | + assertInstanceOf(BaseProperty.BaseStringProperty.class, bodyProperty7); |
| 141 | + |
| 142 | + BaseProperty bodyProperty8 = bodyProperties.get(7); |
| 143 | + |
| 144 | + assertEquals("field8", bodyProperty8.getName()); |
| 145 | + assertInstanceOf(BaseProperty.BaseStringProperty.class, bodyProperty8); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testGetWebhookResult() { |
| 150 | + Map<String, Object> bodyContent = Map.of("key1", "value1"); |
| 151 | + |
| 152 | + when(mockedWebhookBody.getContent()).thenReturn(bodyContent); |
| 153 | + |
| 154 | + Parameters inputParameters = |
| 155 | + MockParametersFactory.create(Map.of(IGNORE_BOTS, false, USE_WORKFLOW_TIMEZONE, false)); |
| 156 | + |
| 157 | + Map<String, ?> result = NewFormRequestTrigger.getWebhookResult( |
| 158 | + inputParameters, mock(Parameters.class), mockedHttpHeaders, mockedHttpParameters, |
| 159 | + mockedWebhookBody, mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext); |
| 160 | + |
| 161 | + assertEquals("value1", result.get("key1")); |
| 162 | + assertTrue(result.containsKey("submittedAt")); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + void testGetWebhookResultIgnoreBots() { |
| 167 | + Map<String, Object> bodyContent = Map.of("key1", "value1"); |
| 168 | + |
| 169 | + when(mockedWebhookBody.getContent()).thenReturn(bodyContent); |
| 170 | + |
| 171 | + Parameters inputParameters = MockParametersFactory.create(Map.of(IGNORE_BOTS, true)); |
| 172 | + |
| 173 | + // Test with bot user agent |
| 174 | + when(mockedHttpHeaders.firstValue("User-Agent")).thenReturn(Optional.of("Googlebot")); |
| 175 | + |
| 176 | + Map<String, ?> result = NewFormRequestTrigger.getWebhookResult( |
| 177 | + inputParameters, mock(Parameters.class), mockedHttpHeaders, mockedHttpParameters, |
| 178 | + mockedWebhookBody, mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext); |
| 179 | + |
| 180 | + assertTrue(result.isEmpty()); |
| 181 | + |
| 182 | + // Test with the regular user agent |
| 183 | + when(mockedHttpHeaders.firstValue("User-Agent")).thenReturn(Optional.of("Mozilla/5.0")); |
| 184 | + |
| 185 | + result = NewFormRequestTrigger.getWebhookResult( |
| 186 | + inputParameters, mock(Parameters.class), mockedHttpHeaders, mockedHttpParameters, |
| 187 | + mockedWebhookBody, mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext); |
| 188 | + |
| 189 | + assertEquals("value1", result.get("key1")); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testGetWebhookResultUseWorkflowTimezone() { |
| 194 | + Map<String, Object> bodyContent = Map.of("key1", "value1"); |
| 195 | + |
| 196 | + when(mockedWebhookBody.getContent()).thenReturn(bodyContent); |
| 197 | + |
| 198 | + Parameters inputParameters = MockParametersFactory.create(Map.of(USE_WORKFLOW_TIMEZONE, true)); |
| 199 | + |
| 200 | + Map<String, ?> result = NewFormRequestTrigger.getWebhookResult( |
| 201 | + inputParameters, mock(Parameters.class), mockedHttpHeaders, mockedHttpParameters, |
| 202 | + mockedWebhookBody, mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext); |
| 203 | + |
| 204 | + assertEquals("value1", result.get("key1")); |
| 205 | + assertFalse(result.containsKey("submittedAt")); |
| 206 | + } |
| 207 | +} |
0 commit comments