|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.jmx.yaml; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
| 13 | +import io.opentelemetry.instrumentation.jmx.engine.MetricAttribute; |
| 14 | +import javax.management.MBeanAttributeInfo; |
| 15 | +import javax.management.MBeanInfo; |
| 16 | +import javax.management.MBeanServerConnection; |
| 17 | +import javax.management.ObjectName; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.CsvSource; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
| 21 | + |
| 22 | +public class MetricStructureTest { |
| 23 | + |
| 24 | + @ParameterizedTest |
| 25 | + @CsvSource({"const(Hello),Hello", "lowercase(const(Hello)),hello"}) |
| 26 | + void metricAttribute_constant(String target, String expectedValue) { |
| 27 | + MetricAttribute ma = MetricStructure.buildMetricAttribute("name", target); |
| 28 | + assertThat(ma.getAttributeName()).isEqualTo("name"); |
| 29 | + assertThat(ma.isStateAttribute()).isFalse(); |
| 30 | + assertThat(ma.acquireAttributeValue(null, null)).isEqualTo(expectedValue); |
| 31 | + } |
| 32 | + |
| 33 | + @ParameterizedTest |
| 34 | + @CsvSource({ |
| 35 | + "beanattr(beanAttribute),Hello,Hello", |
| 36 | + "lowercase(beanattr(beanAttribute)),Hello,hello", |
| 37 | + }) |
| 38 | + void metricAttribute_beanAttribute(String target, String value, String expectedValue) |
| 39 | + throws Exception { |
| 40 | + MetricAttribute ma = MetricStructure.buildMetricAttribute("name", target); |
| 41 | + assertThat(ma.getAttributeName()).isEqualTo("name"); |
| 42 | + assertThat(ma.isStateAttribute()).isFalse(); |
| 43 | + |
| 44 | + ObjectName objectName = new ObjectName("test:name=_beanAttribute"); |
| 45 | + MBeanServerConnection mockConnection = mock(MBeanServerConnection.class); |
| 46 | + |
| 47 | + MBeanInfo mockBeanInfo = mock(MBeanInfo.class); |
| 48 | + when(mockBeanInfo.getAttributes()) |
| 49 | + .thenReturn( |
| 50 | + new MBeanAttributeInfo[] { |
| 51 | + new MBeanAttributeInfo("beanAttribute", "java.lang.String", "", true, false, false) |
| 52 | + }); |
| 53 | + when(mockConnection.getMBeanInfo(objectName)).thenReturn(mockBeanInfo); |
| 54 | + when(mockConnection.getAttribute(objectName, "beanAttribute")).thenReturn(value); |
| 55 | + |
| 56 | + assertThat(ma.acquireAttributeValue(mockConnection, objectName)).isEqualTo(expectedValue); |
| 57 | + } |
| 58 | + |
| 59 | + @ParameterizedTest |
| 60 | + @CsvSource({ |
| 61 | + "param(name),Hello,Hello", |
| 62 | + "lowercase(param(name)),Hello,hello", |
| 63 | + }) |
| 64 | + void metricAttribute_beanParam(String target, String value, String expectedValue) |
| 65 | + throws Exception { |
| 66 | + MetricAttribute ma = MetricStructure.buildMetricAttribute("name", target); |
| 67 | + assertThat(ma.getAttributeName()).isEqualTo("name"); |
| 68 | + assertThat(ma.isStateAttribute()).isFalse(); |
| 69 | + |
| 70 | + ObjectName objectName = new ObjectName("test:name=" + value); |
| 71 | + MBeanServerConnection mockConnection = mock(MBeanServerConnection.class); |
| 72 | + |
| 73 | + assertThat(ma.acquireAttributeValue(mockConnection, objectName)).isEqualTo(expectedValue); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @ValueSource( |
| 78 | + strings = { |
| 79 | + "missing(name)", // non-existing target |
| 80 | + "param()", // missing parameter |
| 81 | + "param( )", // missing parameter with empty string |
| 82 | + "param(name)a", // something after parenthesis |
| 83 | + "lowercase()", // misng target in modifier |
| 84 | + "lowercase(param(name)", // missing parenthesis for modifier |
| 85 | + "lowercase(missing(name))", // non-existing target within modifier |
| 86 | + "lowercase(param())", // missing parameter in modifier |
| 87 | + "lowercase(param( ))", // missing parameter in modifier with empty string |
| 88 | + "lowercase(param))", // missing parenthesis within modifier |
| 89 | + }) |
| 90 | + void invalidTargetSyntax(String target) { |
| 91 | + assertThatThrownBy(() -> MetricStructure.buildMetricAttribute("metric_attribute", target)) |
| 92 | + .isInstanceOf(IllegalArgumentException.class) |
| 93 | + .describedAs( |
| 94 | + "exception should be thrown with original expression to help end-user understand the syntax error") |
| 95 | + .hasMessageContaining(target); |
| 96 | + } |
| 97 | +} |
0 commit comments