|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.remote; |
| 19 | + |
| 20 | +import static org.mockito.Mockito.times; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import org.openqa.selenium.remote.tracing.Span; |
| 26 | +import org.openqa.selenium.remote.tracing.TraceContext; |
| 27 | +import org.openqa.selenium.remote.tracing.Tracer; |
| 28 | +import org.openqa.selenium.testing.UnitTests; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.MockitoAnnotations; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.UUID; |
| 39 | + |
| 40 | +@Category(UnitTests.class) |
| 41 | +public class TracedCommandExecutorTest { |
| 42 | + @Mock |
| 43 | + private CommandExecutor commandExecutor; |
| 44 | + @Mock |
| 45 | + private Tracer tracer; |
| 46 | + @Mock |
| 47 | + private TraceContext traceContext; |
| 48 | + @Mock |
| 49 | + private Span span; |
| 50 | + |
| 51 | + private TracedCommandExecutor tracedCommandExecutor; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void createMocksAndTracedCommandExecutor() { |
| 55 | + MockitoAnnotations.initMocks(this); |
| 56 | + when(tracer.getCurrentContext()).thenReturn(traceContext); |
| 57 | + when(traceContext.createSpan("command")).thenReturn(span); |
| 58 | + tracedCommandExecutor = new TracedCommandExecutor(commandExecutor, tracer); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void canCreateSpanWithAllAttributes() throws IOException { |
| 63 | + SessionId sessionId = new SessionId(UUID.randomUUID()); |
| 64 | + Map<String, Object> parameters = new HashMap<>(); |
| 65 | + parameters.put("param1", "value1"); |
| 66 | + parameters.put("param2", "value2"); |
| 67 | + Command command = new Command(sessionId, "findElement", parameters); |
| 68 | + |
| 69 | + tracedCommandExecutor.execute(command); |
| 70 | + |
| 71 | + verify(span, times(1)).setAttribute("sessionId", sessionId.toString()); |
| 72 | + verify(span, times(1)).setAttribute("command", "findElement"); |
| 73 | + verify(span, times(1)).setAttribute("parameter.param1", "value1"); |
| 74 | + verify(span, times(1)).setAttribute("parameter.param2", "value2"); |
| 75 | + verify(span, times(1)).close(); |
| 76 | + verifyNoMoreInteractions(span); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void canCreateSpanWithSessionIdAndCommandName() throws IOException { |
| 81 | + SessionId sessionId = new SessionId(UUID.randomUUID()); |
| 82 | + Command command = new Command(sessionId, "findElement"); |
| 83 | + |
| 84 | + tracedCommandExecutor.execute(command); |
| 85 | + |
| 86 | + verify(span, times(1)).setAttribute("sessionId", sessionId.toString()); |
| 87 | + verify(span, times(1)).setAttribute("command", "findElement"); |
| 88 | + verify(span, times(1)).close(); |
| 89 | + verifyNoMoreInteractions(span); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void canCreateSpanWithCommandName() throws IOException { |
| 94 | + Command command = new Command(null, "createSession"); |
| 95 | + |
| 96 | + tracedCommandExecutor.execute(command); |
| 97 | + |
| 98 | + verify(span, times(1)).setAttribute("command", "createSession"); |
| 99 | + verify(span, times(1)).close(); |
| 100 | + verifyNoMoreInteractions(span); |
| 101 | + } |
| 102 | +} |
0 commit comments