|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.atlas.notification.preprocessor; |
| 20 | + |
| 21 | +import org.apache.atlas.kafka.AtlasKafkaMessage; |
| 22 | +import org.apache.atlas.kafka.KafkaNotification; |
| 23 | +import org.apache.atlas.model.instance.AtlasEntity; |
| 24 | +import org.apache.atlas.model.notification.HookNotification; |
| 25 | +import org.apache.atlas.notification.hook.HookMessageDeserializer; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.testng.annotations.Test; |
| 29 | + |
| 30 | +import java.util.Map; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.ArrayList; |
| 34 | + |
| 35 | +import java.util.regex.Pattern; |
| 36 | + |
| 37 | +import static org.testng.Assert.assertEquals; |
| 38 | + |
| 39 | +public class SparkPreprocessorTest { |
| 40 | + private static final Logger LOG = LoggerFactory.getLogger(SparkPreprocessorTest.class); |
| 41 | + private final HookMessageDeserializer deserializer = new HookMessageDeserializer(); |
| 42 | + public static final String TYPE_SPARK_PROCESS = "spark_process"; |
| 43 | + public static final String ATTRIBUTE_DETAILS = "details"; |
| 44 | + public static final String ATTRIBUTE_SPARKPLANDESCRIPTION = "sparkPlanDescription"; |
| 45 | + public static final String ATTRIBUTE_QUALIFIED_NAME = "qualifiedName"; |
| 46 | + public static final String ATTRIBUTE_NAME = "name"; |
| 47 | + public static final String ATTRIBUTE_ISINCOMPLETE = "isIncomplete"; |
| 48 | + public static final String ATTRIBUTE_REMOTEUSER = "remoteUser"; |
| 49 | + public static final String ATTRIBUTE_EXECUTIONID = "executionId"; |
| 50 | + public static final String ATTRIBUTE_QUERYTEXT = "queryText"; |
| 51 | + public static final String ATTRIBUTE_CURRUSER = "currUser"; |
| 52 | + public static final String ATTRIBUTE_GUID = "guid"; |
| 53 | + private static final List<String> EMPTY_STR_LIST = new ArrayList<>(); |
| 54 | + private static final List<Pattern> EMPTY_PATTERN_LIST = new ArrayList<>(); |
| 55 | + |
| 56 | + private void getPreprocessorContext(AtlasEntity entity) { |
| 57 | + EntityPreprocessor preprocessor = EntityPreprocessor.getSparkPreprocessor(entity.getTypeName()); |
| 58 | + HookNotification hookNotification = new HookNotification.EntityCreateRequestV2("test", new AtlasEntity.AtlasEntitiesWithExtInfo(entity)); |
| 59 | + |
| 60 | + AtlasKafkaMessage<HookNotification> kafkaMsg = new AtlasKafkaMessage(hookNotification, -1, KafkaNotification.ATLAS_HOOK_TOPIC, -1); |
| 61 | + |
| 62 | + PreprocessorContext context = new PreprocessorContext(kafkaMsg, null, EMPTY_PATTERN_LIST, EMPTY_PATTERN_LIST, null, |
| 63 | + EMPTY_STR_LIST, EMPTY_STR_LIST, EMPTY_STR_LIST, false, |
| 64 | + false, true, false, null); |
| 65 | + if (preprocessor != null) { |
| 66 | + preprocessor.preprocess(entity, context); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public Object[][] provideSparkProcessData() { |
| 71 | + Map<String, Object> attributes = new HashMap<>(); |
| 72 | + attributes.put(ATTRIBUTE_NAME, "execution-1"); |
| 73 | + attributes.put(ATTRIBUTE_QUALIFIED_NAME, "application_1740993925593_0006-execution-1sparkTab1"); |
| 74 | + attributes.put(ATTRIBUTE_DETAILS, "== Parsed Logical Plan ==\\nCreateHiveTableAsSelectCommand ..."); |
| 75 | + attributes.put(ATTRIBUTE_SPARKPLANDESCRIPTION, "Execute CreateHiveTableAsSelectCommand ..."); |
| 76 | + attributes.put(ATTRIBUTE_GUID, "-32055574130361399"); |
| 77 | + attributes.put(ATTRIBUTE_ISINCOMPLETE, "false"); |
| 78 | + attributes.put(ATTRIBUTE_REMOTEUSER, "spark"); |
| 79 | + attributes.put(ATTRIBUTE_EXECUTIONID, 1); |
| 80 | + attributes.put(ATTRIBUTE_QUERYTEXT, null); |
| 81 | + attributes.put(ATTRIBUTE_CURRUSER, "spark"); |
| 82 | + |
| 83 | + return new Object[][] { { attributes } }; |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void replaceAttributesInSparkProcess() { |
| 88 | + Object[][] testData = provideSparkProcessData(); |
| 89 | + |
| 90 | + for (Object[] data : testData) { |
| 91 | + Map<String, Object> attributes = (Map<String, Object>) data[0]; // Extract attributes |
| 92 | + |
| 93 | + AtlasEntity atlasEntity = new AtlasEntity(); |
| 94 | + atlasEntity.setTypeName(TYPE_SPARK_PROCESS); |
| 95 | + attributes.forEach(atlasEntity::setAttribute); |
| 96 | + getPreprocessorContext(atlasEntity); |
| 97 | + |
| 98 | + assertEquals(atlasEntity.getAttribute(ATTRIBUTE_DETAILS), null); |
| 99 | + assertEquals(atlasEntity.getAttribute(ATTRIBUTE_SPARKPLANDESCRIPTION), null); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments