1+ /*
2+ * Licensed to Elasticsearch B.V. under one or more contributor
3+ * license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright
5+ * ownership. Elasticsearch B.V. licenses this file to you under
6+ * the Apache License, Version 2.0 (the "License"); you may
7+ * not use this file except in compliance with the License.
8+ * 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,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+ package co .elastic .apm .android .sdk .connectivity .opentelemetry ;
20+
21+ import static org .junit .Assert .assertEquals ;
22+ import static org .mockito .Mockito .doReturn ;
23+ import static org .mockito .Mockito .mock ;
24+
25+ import org .junit .After ;
26+ import org .junit .Before ;
27+ import org .junit .Test ;
28+
29+ import java .io .IOException ;
30+ import java .util .Collections ;
31+ import java .util .concurrent .TimeUnit ;
32+
33+ import co .elastic .apm .android .sdk .connectivity .ExportProtocol ;
34+ import co .elastic .apm .android .sdk .internal .configuration .impl .ConnectivityConfiguration ;
35+ import co .elastic .apm .android .sdk .testutils .providers .SimpleProvider ;
36+ import io .opentelemetry .api .trace .SpanKind ;
37+ import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
38+ import io .opentelemetry .sdk .metrics .data .MetricData ;
39+ import io .opentelemetry .sdk .metrics .internal .data .ImmutableMetricData ;
40+ import io .opentelemetry .sdk .metrics .internal .data .ImmutableSumData ;
41+ import io .opentelemetry .sdk .resources .Resource ;
42+ import io .opentelemetry .sdk .testing .logs .TestLogRecordData ;
43+ import io .opentelemetry .sdk .testing .trace .TestSpanData ;
44+ import io .opentelemetry .sdk .trace .data .StatusData ;
45+ import okhttp3 .mockwebserver .MockResponse ;
46+ import okhttp3 .mockwebserver .MockWebServer ;
47+ import okhttp3 .mockwebserver .RecordedRequest ;
48+
49+ public class DefaultSignalConfigurationTest {
50+
51+ private MockWebServer server ;
52+ private ConnectivityConfiguration connectivityConfiguration ;
53+ private DefaultSignalConfiguration signalConfiguration ;
54+
55+ @ Before
56+ public void setUp () throws IOException {
57+ server = new MockWebServer ();
58+ server .start ();
59+ server .enqueue (new MockResponse ());
60+
61+ connectivityConfiguration = mock ();
62+ doReturn ("http://" + server .getHostName () + ":" + server .getPort ()).when (connectivityConfiguration ).getEndpoint ();
63+ signalConfiguration = new DefaultSignalConfiguration (SimpleProvider .create (connectivityConfiguration ));
64+ }
65+
66+ @ Test
67+ public void testSpansHttpEndpoint () throws InterruptedException {
68+ doReturn (ExportProtocol .HTTP ).when (connectivityConfiguration ).getExportProtocol ();
69+ TestSpanData spanData = getTestSpanData ();
70+
71+ signalConfiguration .provideSpanExporter ().export (Collections .singleton (spanData )).join (1 , TimeUnit .SECONDS );
72+
73+ RecordedRequest recordedRequest = server .takeRequest ();
74+ assertEquals ("/v1/traces" , recordedRequest .getPath ());
75+ }
76+
77+ @ Test
78+ public void testMetricsHttpEndpoint () throws InterruptedException {
79+ doReturn (ExportProtocol .HTTP ).when (connectivityConfiguration ).getExportProtocol ();
80+ MetricData metricData = getTestMetricData ();
81+
82+ signalConfiguration .provideMetricExporter ().export (Collections .singleton (metricData )).join (1 , TimeUnit .SECONDS );
83+
84+ RecordedRequest recordedRequest = server .takeRequest ();
85+ assertEquals ("/v1/metrics" , recordedRequest .getPath ());
86+ }
87+
88+ @ Test
89+ public void testLogRecordsHttpEndpoint () throws InterruptedException {
90+ doReturn (ExportProtocol .HTTP ).when (connectivityConfiguration ).getExportProtocol ();
91+ TestLogRecordData logRecordData = TestLogRecordData .builder ().setBody ("Log body" ).build ();
92+
93+ signalConfiguration .provideLogExporter ().export (Collections .singleton (logRecordData )).join (1 , TimeUnit .SECONDS );
94+
95+ RecordedRequest recordedRequest = server .takeRequest ();
96+ assertEquals ("/v1/logs" , recordedRequest .getPath ());
97+ }
98+
99+ private static TestSpanData getTestSpanData () {
100+ return TestSpanData .builder ().setName ("Some name" )
101+ .setStartEpochNanos (1000 )
102+ .setEndEpochNanos (1100 )
103+ .setHasEnded (true )
104+ .setStatus (StatusData .ok ())
105+ .setKind (SpanKind .CLIENT )
106+ .build ();
107+ }
108+
109+ private static MetricData getTestMetricData () {
110+ return ImmutableMetricData .createLongSum (Resource .empty (),
111+ InstrumentationScopeInfo .empty (),
112+ "A Metric" ,
113+ "A description" ,
114+ "m" ,
115+ ImmutableSumData .empty ());
116+ }
117+
118+ @ After
119+ public void tearDown () throws IOException {
120+ server .shutdown ();
121+ }
122+ }
0 commit comments