|
| 1 | +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | + ~ Copyright 2024 Adobe |
| 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 | + ~ http://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.adobe.cq.forms.core.components.internal.models.v1.form; |
| 18 | + |
| 19 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | + |
| 24 | +import com.adobe.cq.forms.core.Utils; |
| 25 | +import com.adobe.cq.forms.core.components.datalayer.FormComponentData; |
| 26 | +import com.adobe.cq.forms.core.components.models.form.DateTime; |
| 27 | +import com.adobe.cq.forms.core.components.models.form.FieldType; |
| 28 | +import com.adobe.cq.forms.core.context.FormsCoreComponentTestContext; |
| 29 | +import io.wcm.testing.mock.aem.junit5.AemContext; |
| 30 | +import io.wcm.testing.mock.aem.junit5.AemContextExtension; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.junit.Assert.assertNull; |
| 34 | + |
| 35 | +@ExtendWith(AemContextExtension.class) |
| 36 | +public class DateTimeImplTest { |
| 37 | + |
| 38 | + private static final String BASE = "/form/datetime"; |
| 39 | + private static final String CONTENT_ROOT = "/content"; |
| 40 | + private static final String PATH_DATETIME_DATALAYER = CONTENT_ROOT + "/datetime-datalayer"; |
| 41 | + private static final String PATH_DATETIME_CUSTOMIZED = CONTENT_ROOT + "/datetime-customized"; |
| 42 | + private static final String PATH_NUMBER_DATETIME_EXCLUSIVE = CONTENT_ROOT + "/number-datetime-exclusive"; |
| 43 | + private static final String PATH_NUMBER_DATETIME_INPUT = CONTENT_ROOT + "/number-datetime"; |
| 44 | + |
| 45 | + private final AemContext context = FormsCoreComponentTestContext.newAemContext(); |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() { |
| 49 | + context.load().json(BASE + FormsCoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testFieldType() { |
| 54 | + DateTime dateTime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 55 | + assertEquals(FieldType.DATETIME.getValue(), dateTime.getFieldType()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testGetLabel() { |
| 60 | + DateTime dateTime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 61 | + assertEquals("pwd", dateTime.getLabel().getValue()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testPlaceholder() { |
| 66 | + DateTime dateTime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 67 | + assertEquals("Enter valid date & time", dateTime.getPlaceHolder()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testGetName() { |
| 72 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 73 | + assertEquals("datetime", datetime.getName()); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testDorProperties() { |
| 79 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 80 | + assertEquals(true, datetime.getDorProperties().get("dorExclusion")); |
| 81 | + assertEquals("4", datetime.getDorProperties().get("dorColspan")); |
| 82 | + assertEquals("Text1", datetime.getDorProperties().get("dorBindRef")); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testGetDescription() { |
| 88 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 89 | + assertEquals("datetime field", datetime.getDescription()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testGetRequired() { |
| 94 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 95 | + assertEquals(true, datetime.isRequired()); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testGetPattern() { |
| 100 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 101 | + assertEquals("/^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/", datetime.getPattern()); |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testIsEnabled() { |
| 107 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 108 | + assertEquals(true, datetime.isEnabled()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testIsEnabledForCustomized() { |
| 113 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 114 | + assertEquals(true, datetime.isEnabled()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void testIsReadOnly() { |
| 119 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 120 | + assertEquals(false, datetime.isReadOnly()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testIsReadOnlyForCustomized() { |
| 125 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 126 | + assertEquals(false, datetime.isReadOnly()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void testMinLength() { |
| 131 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 132 | + assertEquals(5, datetime.getMinLength().intValue()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void testMaxLength() { |
| 137 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 138 | + assertEquals(10, datetime.getMaxLength().intValue()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testGetExclusiveMinimum() { |
| 143 | + DateTime datetime = Utils.getComponentUnderTest(PATH_NUMBER_DATETIME_EXCLUSIVE, DateTime.class, context); |
| 144 | + assertNull(datetime.getMinimum()); |
| 145 | + assertEquals(8L, datetime.getExclusiveMinimum().longValue()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testGetExclusiveMaximum() { |
| 150 | + DateTime datetime = Utils.getComponentUnderTest(PATH_NUMBER_DATETIME_EXCLUSIVE, DateTime.class, context); |
| 151 | + assertNull(datetime.getMaximum()); |
| 152 | + assertEquals(16L, datetime.getExclusiveMaximum().longValue()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void testGetMinimum() { |
| 157 | + DateTime datetime = Utils.getComponentUnderTest(PATH_NUMBER_DATETIME_INPUT, DateTime.class, context); |
| 158 | + assertEquals(8, datetime.getMinimum().intValue()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testGetMaximum() { |
| 163 | + DateTime datetime = Utils.getComponentUnderTest(PATH_NUMBER_DATETIME_INPUT, DateTime.class, context); |
| 164 | + assertEquals(16, datetime.getMaximum().intValue()); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void testGetDisplayFormat() throws Exception { |
| 169 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_CUSTOMIZED, DateTime.class, context); |
| 170 | + assertEquals("datetime", datetime.getFormat()); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void testDataLayerProperties() throws IllegalAccessException { |
| 175 | + DateTime datetime = Utils.getComponentUnderTest(PATH_DATETIME_DATALAYER, DateTime.class, context); |
| 176 | + FieldUtils.writeField(datetime, "dataLayerEnabled", true, true); |
| 177 | + FormComponentData dataObject = (FormComponentData) datetime.getData(); |
| 178 | + assert (dataObject != null); |
| 179 | + assert (dataObject.getId()).equals("datetime-1c7bc238a6"); |
| 180 | + assert (dataObject.getType()).equals("core/fd/components/form/datetime/v1/datetime"); |
| 181 | + assert (dataObject.getTitle()).equals("Full Name"); |
| 182 | + assert (dataObject.getFieldType()).equals("datetime"); |
| 183 | + assert (dataObject.getDescription()).equals("Enter Full Name"); |
| 184 | + } |
| 185 | +} |
0 commit comments