|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.tracing.decorators; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.OffsetDateTime; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.camel.Endpoint; |
| 24 | +import org.apache.camel.Exchange; |
| 25 | +import org.apache.camel.Message; |
| 26 | +import org.apache.camel.tracing.MockSpanAdapter; |
| 27 | +import org.apache.camel.tracing.TagConstants; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | + |
| 33 | +public class AzureStorageDataLakeSpanDecoratorTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testGetOperationNameFromHeader() { |
| 37 | + String operation = "upload"; |
| 38 | + Exchange exchange = Mockito.mock(Exchange.class); |
| 39 | + Message message = Mockito.mock(Message.class); |
| 40 | + |
| 41 | + Mockito.when(exchange.getIn()).thenReturn(message); |
| 42 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.OPERATION, String.class)).thenReturn(operation); |
| 43 | + |
| 44 | + AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator(); |
| 45 | + |
| 46 | + assertEquals(operation, decorator.getOperationName(exchange, null)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testGetOperationNameFromHeaderWithEnum() { |
| 51 | + operationEnum operation = operationEnum.upload; |
| 52 | + |
| 53 | + Exchange exchange = Mockito.mock(Exchange.class); |
| 54 | + Message message = Mockito.mock(Message.class); |
| 55 | + Mockito.when(exchange.getIn()).thenReturn(message); |
| 56 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.OPERATION, String.class)) |
| 57 | + .thenReturn(operation.toString()); |
| 58 | + |
| 59 | + AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator(); |
| 60 | + |
| 61 | + assertEquals(operation.toString(), decorator.getOperationName(exchange, null)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testGetOperationNameFromQueryParameter() { |
| 66 | + Endpoint endpoint = Mockito.mock(Endpoint.class); |
| 67 | + Exchange exchange = Mockito.mock(Exchange.class); |
| 68 | + Message message = Mockito.mock(Message.class); |
| 69 | + |
| 70 | + Mockito.when(endpoint.getEndpointUri()).thenReturn("azure-storage-datalake:myAccount/myFileSystem?operation=upload"); |
| 71 | + Mockito.when(exchange.getIn()).thenReturn(message); |
| 72 | + |
| 73 | + AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator(); |
| 74 | + |
| 75 | + assertEquals("upload", decorator.getOperationName(exchange, endpoint)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testPre() { |
| 80 | + String fileSystemName = "myFileSystem"; |
| 81 | + String directoryName = "myDirectory"; |
| 82 | + String fileName = "myFile"; |
| 83 | + String path = "myPath"; |
| 84 | + String expression = "myExpression"; |
| 85 | + String contentType = "myContentType"; |
| 86 | + Duration timeout = Duration.ofDays(7); |
| 87 | + Map<String, String> metadata = Map.of("key1", "value1", "key2", "value2"); |
| 88 | + OffsetDateTime lastModified = OffsetDateTime.now(); |
| 89 | + Long position = 21L; |
| 90 | + |
| 91 | + Endpoint endpoint = Mockito.mock(Endpoint.class); |
| 92 | + Exchange exchange = Mockito.mock(Exchange.class); |
| 93 | + Message message = Mockito.mock(Message.class); |
| 94 | + |
| 95 | + Mockito.when(endpoint.getEndpointUri()).thenReturn("azure-storage-datalake:account/myFileSystem"); |
| 96 | + Mockito.when(exchange.getIn()).thenReturn(message); |
| 97 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.FILESYSTEM_NAME, String.class)) |
| 98 | + .thenReturn(fileSystemName); |
| 99 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.DIRECTORY_NAME, String.class)) |
| 100 | + .thenReturn(directoryName); |
| 101 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.FILE_NAME, String.class)).thenReturn(fileName); |
| 102 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.PATH, String.class)).thenReturn(path); |
| 103 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.EXPRESSION, String.class)).thenReturn(expression); |
| 104 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.CONTENT_TYPE, String.class)).thenReturn(contentType); |
| 105 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.TIMEOUT, Duration.class)).thenReturn(timeout); |
| 106 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.METADATA, Map.class)) |
| 107 | + .thenReturn(metadata); |
| 108 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.LAST_MODIFIED, OffsetDateTime.class)) |
| 109 | + .thenReturn(lastModified); |
| 110 | + Mockito.when(message.getHeader(AzureStorageDataLakeSpanDecorator.POSITION, Long.class)).thenReturn(position); |
| 111 | + |
| 112 | + AbstractSpanDecorator decorator = new AzureStorageDataLakeSpanDecorator(); |
| 113 | + |
| 114 | + MockSpanAdapter span = new MockSpanAdapter(); |
| 115 | + |
| 116 | + decorator.pre(span, exchange, endpoint); |
| 117 | + |
| 118 | + assertEquals("azure-storage-datalake", span.tags().get(TagConstants.DB_SYSTEM)); |
| 119 | + assertEquals(fileSystemName, span.tags().get(TagConstants.DB_NAME)); |
| 120 | + assertEquals(directoryName, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_DIRECTORY_NAME)); |
| 121 | + assertEquals(fileName, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_FILE_NAME)); |
| 122 | + assertEquals(path, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_PATH)); |
| 123 | + assertEquals(expression, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_EXPRESSION)); |
| 124 | + assertEquals(contentType, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_CONTENT_TYPE)); |
| 125 | + assertEquals(timeout.toString(), span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_TIMEOUT)); |
| 126 | + assertEquals(metadata.toString(), span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_METADATA)); |
| 127 | + assertEquals(lastModified.toString(), |
| 128 | + span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_LAST_MODIFIED)); |
| 129 | + assertEquals(position, span.tags().get(AzureStorageDataLakeSpanDecorator.STORAGE_DATALAKE_POSITION)); |
| 130 | + } |
| 131 | + |
| 132 | + enum operationEnum { |
| 133 | + upload |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments