提交 9e4a4553 编写于 作者: L lican 提交者: wu-sheng

Add collector agent test (#1137)

* init test

* update

* agent-grpc-test

* part

* fix based on org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan.logs

* Fix Code Bug

* Add Test

* Fix Text

* add SegmentCounterTest
上级 03e3eb11
......@@ -27,6 +27,9 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>agent-grpc-provider</artifactId>
<properties>
<grpc.version>1.10.0</grpc.version>
</properties>
<dependencies>
<dependency>
......@@ -69,5 +72,17 @@
<artifactId>register-define</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
<version>${grpc.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider;
import org.apache.skywalking.apm.collector.agent.grpc.define.AgentGRPCModule;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.core.module.ServiceNotProvidedException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentModuleGRPCProviderTest {
private AgentModuleGRPCProvider agentModuleGRPCProvider;
@Mock
private AgentModuleGRPCConfig config;
@Mock
private ModuleManager moduleManager;
@Before
public void setUp() throws Exception {
agentModuleGRPCProvider = new AgentModuleGRPCProvider();
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
Whitebox.setInternalState(agentModuleGRPCProvider, "config", config);
Whitebox.setInternalState(agentModuleGRPCProvider, "manager", moduleManager);
}
@Test
public void name() {
Assert.assertEquals(agentModuleGRPCProvider.name(), "gRPC");
}
@Test
public void module() {
Assert.assertEquals(agentModuleGRPCProvider.module(), AgentGRPCModule.class);
}
@Test
public void createConfigBeanIfAbsent() {
assertEquals(agentModuleGRPCProvider.createConfigBeanIfAbsent(), config);
}
@Test
public void prepare() {
agentModuleGRPCProvider.prepare();
}
@Test
public void start() throws ServiceNotProvidedException {
String host = "127.0.0.1";
Integer port = 12801;
Mockito.when(config.getHost()).thenReturn(host);
Mockito.when(config.getPort()).thenReturn(port);
Mockito.when(config.getAuthentication()).thenReturn("test_token");
agentModuleGRPCProvider.start();
}
@Test
public void notifyAfterCompleted() {
agentModuleGRPCProvider.notifyAfterCompleted();
}
@Test
public void requiredModules() {
String[] strings = agentModuleGRPCProvider.requiredModules();
assertTrue(strings.length > 0);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider;
import org.apache.skywalking.apm.collector.cluster.ModuleRegistration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentModuleGRPCRegistrationTest {
private AgentModuleGRPCRegistration agentModuleGRPCRegistration;
@Before
public void setUp() throws Exception {
agentModuleGRPCRegistration = new AgentModuleGRPCRegistration("127.0.0.1", 10900);
}
@Test
public void buildValue() {
ModuleRegistration.Value value = agentModuleGRPCRegistration.buildValue();
Assert.assertEquals(value.getHostPort(),"127.0.0.1:10900");
Assert.assertEquals(value.getContextPath(),"");
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider;
import io.grpc.ServerServiceDefinition;
import io.grpc.StatusRuntimeException;
import io.grpc.testing.GrpcServerRule;
import org.apache.skywalking.apm.collector.agent.grpc.provider.handler.ApplicationRegisterServiceHandler;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.ServerException;
import org.apache.skywalking.apm.collector.server.grpc.GRPCServer;
import org.apache.skywalking.apm.network.proto.Application;
import org.apache.skywalking.apm.network.proto.ApplicationMapping;
import org.apache.skywalking.apm.network.proto.ApplicationRegisterServiceGrpc;
import org.apache.skywalking.apm.network.proto.KeyWithIntegerValue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AuthenticationSimpleCheckerTest {
@Rule
public final GrpcServerRule grpcServerRule = new GrpcServerRule().directExecutor();
@Mock
private ModuleManager moduleManager;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
}
@Test(expected = StatusRuntimeException.class)
public void build() throws ServerException {
ApplicationRegisterServiceHandler applicationRegisterServiceHandler = new ApplicationRegisterServiceHandler(moduleManager);
MockGRPCServer mockGRPCServer = new MockGRPCServer(grpcServerRule);
mockGRPCServer.initialize();
AuthenticationSimpleChecker.INSTANCE.build(mockGRPCServer, applicationRegisterServiceHandler);
grpcServerRule.getServiceRegistry().addService(applicationRegisterServiceHandler);
ApplicationRegisterServiceGrpc.ApplicationRegisterServiceBlockingStub stub = ApplicationRegisterServiceGrpc.newBlockingStub(grpcServerRule.getChannel());
Application application = Application.newBuilder().setApplicationCode("test").build();
ApplicationMapping applicationMapping = stub.applicationCodeRegister(application);
assertEquals(applicationMapping.getApplication(), KeyWithIntegerValue.getDefaultInstance());
AuthenticationSimpleChecker.INSTANCE.setExpectedToken("test");
AuthenticationSimpleChecker.INSTANCE.build(mockGRPCServer, applicationRegisterServiceHandler);
stub.applicationCodeRegister(application);
}
@Test
public void setExpectedToken() {
}
class MockGRPCServer extends GRPCServer {
private GrpcServerRule grpcServerRule;
public MockGRPCServer(String host, int port) {
super(host, port);
}
public MockGRPCServer(GrpcServerRule grpcServerRule) {
super("127.0.0.1", 0);
this.grpcServerRule = grpcServerRule;
}
public void addHandler(ServerServiceDefinition definition) {
grpcServerRule.getServiceRegistry().addService(definition);
}
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.register.define.service.IApplicationIDService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.Application;
import org.apache.skywalking.apm.network.proto.ApplicationMapping;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class ApplicationRegisterServiceHandlerTest {
private ApplicationRegisterServiceHandler applicationRegisterServiceHandler;
@Mock
private IApplicationIDService applicationIDService;
@Before
public void setUp() {
// MockitoAnnotations.initMocks(this);
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
applicationRegisterServiceHandler = new ApplicationRegisterServiceHandler(moduleManager);
Whitebox.setInternalState(applicationRegisterServiceHandler, "applicationIDService", applicationIDService);
}
@Test
public void applicationCodeRegister() {
Application application = Application.newBuilder().setApplicationCode("test_code").build();
when(applicationIDService.getOrCreateForApplicationCode(anyString())).thenReturn(1000);
applicationRegisterServiceHandler.applicationCodeRegister(application, new StreamObserver<ApplicationMapping>() {
@Override
public void onNext(ApplicationMapping applicationMapping) {
Assert.assertTrue(applicationMapping.getApplication().getValue() == 1000);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.metric.define.service.IInstanceHeartBeatService;
import org.apache.skywalking.apm.collector.analysis.register.define.service.IInstanceIDService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.UUID;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class InstanceDiscoveryServiceHandlerTest {
private InstanceDiscoveryServiceHandler instanceDiscoveryServiceHandler;
@Mock
private IInstanceIDService instanceIDService;
@Mock
private IInstanceHeartBeatService instanceHeartBeatService;
@Before
public void setUp() throws Exception {
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
instanceDiscoveryServiceHandler = new InstanceDiscoveryServiceHandler(moduleManager);
Whitebox.setInternalState(instanceDiscoveryServiceHandler, "instanceIDService", instanceIDService);
Whitebox.setInternalState(instanceDiscoveryServiceHandler, "instanceHeartBeatService", instanceHeartBeatService);
}
@Test
public void registerInstance() {
ApplicationInstance applicationInstance = ApplicationInstance.newBuilder()
.setAgentUUID(UUID.randomUUID().toString())
.setApplicationId(10)
.setRegisterTime(System.currentTimeMillis())
.setOsinfo(
OSInfo.newBuilder()
.setOsName("MAC OS")
.setHostname("test")
.addIpv4S("127.0.0.1")
.setProcessNo(123456)
.build()
).build();
when(instanceIDService.getOrCreateByAgentUUID(anyInt(), anyString(), anyLong(), anyString())).thenReturn(100);
instanceDiscoveryServiceHandler.registerInstance(applicationInstance, new StreamObserver<ApplicationInstanceMapping>() {
@Override
public void onNext(ApplicationInstanceMapping applicationInstanceMapping) {
Assert.assertEquals(100,applicationInstanceMapping.getApplicationInstanceId());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
@Test
public void heartbeat() {
ApplicationInstanceHeartbeat heartbeat = ApplicationInstanceHeartbeat.newBuilder()
.setApplicationInstanceId(100)
.setHeartbeatTime(System.currentTimeMillis())
.build();
instanceDiscoveryServiceHandler.heartbeat(heartbeat, new StreamObserver<Downstream>() {
@Override
public void onNext(Downstream downstream) {
Assert.assertEquals(Downstream.getDefaultInstance(),downstream);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.jvm.define.service.ICpuMetricService;
import org.apache.skywalking.apm.collector.analysis.jvm.define.service.IGCMetricService;
import org.apache.skywalking.apm.collector.analysis.jvm.define.service.IMemoryMetricService;
import org.apache.skywalking.apm.collector.analysis.jvm.define.service.IMemoryPoolMetricService;
import org.apache.skywalking.apm.collector.analysis.metric.define.service.IInstanceHeartBeatService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import java.lang.management.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class JVMMetricsServiceHandlerTest {
@Mock
private ICpuMetricService cpuMetricService;
@Mock
private IGCMetricService gcMetricService;
@Mock
private IMemoryMetricService memoryMetricService;
@Mock
private IMemoryPoolMetricService memoryPoolMetricService;
@Mock
private IInstanceHeartBeatService instanceHeartBeatService;
private JVMMetricsServiceHandler jvmMetricsServiceHandler;
@Before
public void setUp() {
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
jvmMetricsServiceHandler = new JVMMetricsServiceHandler(moduleManager);
Whitebox.setInternalState(jvmMetricsServiceHandler, "cpuMetricService", cpuMetricService);
Whitebox.setInternalState(jvmMetricsServiceHandler, "gcMetricService", gcMetricService);
Whitebox.setInternalState(jvmMetricsServiceHandler, "memoryMetricService", memoryMetricService);
Whitebox.setInternalState(jvmMetricsServiceHandler, "memoryPoolMetricService", memoryPoolMetricService);
Whitebox.setInternalState(jvmMetricsServiceHandler, "instanceHeartBeatService", instanceHeartBeatService);
}
@Test
public void collect() {
List<GarbageCollectorMXBean> beans = ManagementFactory.getGarbageCollectorMXBeans();
List<GC> gcList = new ArrayList<>();
for (GarbageCollectorMXBean bean : beans) {
gcList.add(GC.newBuilder().setPhrase(GCPhrase.NEW)
.setCount(10)
.setTime(100)
.build());
}
CPU cpu = CPU.newBuilder()
.setUsagePercent(80.0d)
.build();
List<Memory> memoryList = new LinkedList<Memory>();
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
Memory.Builder heapMemoryBuilder = Memory.newBuilder();
heapMemoryBuilder.setIsHeap(true);
heapMemoryBuilder.setInit(heapMemoryUsage.getInit());
heapMemoryBuilder.setUsed(heapMemoryUsage.getUsed());
heapMemoryBuilder.setCommitted(heapMemoryUsage.getCommitted());
heapMemoryBuilder.setMax(heapMemoryUsage.getMax());
memoryList.add(heapMemoryBuilder.build());
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
Memory.Builder nonHeapMemoryBuilder = Memory.newBuilder();
nonHeapMemoryBuilder.setIsHeap(false);
nonHeapMemoryBuilder.setInit(nonHeapMemoryUsage.getInit());
nonHeapMemoryBuilder.setUsed(nonHeapMemoryUsage.getUsed());
nonHeapMemoryBuilder.setCommitted(nonHeapMemoryUsage.getCommitted());
nonHeapMemoryBuilder.setMax(nonHeapMemoryUsage.getMax());
memoryList.add(nonHeapMemoryBuilder.build());
List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
List<MemoryPool> poolList = new LinkedList<MemoryPool>();
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
MemoryUsage usage = memoryPoolMXBean.getUsage();
poolList.add(MemoryPool.newBuilder().setType(PoolType.CODE_CACHE_USAGE)
.setInit(usage.getInit())
.setMax(usage.getMax())
.setCommited(usage.getCommitted())
.setUsed(usage.getUsed())
.build());
}
JVMMetric jvmMetric = JVMMetric.newBuilder()
.addAllGc(gcList)
.setCpu(cpu)
.addAllMemory(memoryList)
.addAllMemoryPool(poolList)
.setTime(System.currentTimeMillis())
.build();
JVMMetrics request = JVMMetrics.newBuilder()
.addMetrics(jvmMetric)
.setApplicationInstanceId(120)
.build();
jvmMetricsServiceHandler.collect(request, new StreamObserver<Downstream>() {
@Override
public void onNext(Downstream downstream) {
Assert.assertEquals(downstream.isInitialized(), true);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.register.define.service.INetworkAddressIDService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.NetworkAddressMappings;
import org.apache.skywalking.apm.network.proto.NetworkAddresses;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class NetworkAddressRegisterServiceHandlerTest {
@Mock
private INetworkAddressIDService networkAddressIDService;
private NetworkAddressRegisterServiceHandler networkAddressRegisterServiceHandler;
@Before
public void setUp() throws Exception {
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
networkAddressRegisterServiceHandler = new NetworkAddressRegisterServiceHandler(moduleManager);
Whitebox.setInternalState(networkAddressRegisterServiceHandler, "networkAddressIDService", networkAddressIDService);
}
@Test
public void batchRegister() {
NetworkAddresses networkAddresses = NetworkAddresses.newBuilder()
.addAddresses("127.0.0.1:6379")
.build();
when(networkAddressIDService.get(anyString())).thenReturn(1);
networkAddressRegisterServiceHandler.batchRegister(networkAddresses, new StreamObserver<NetworkAddressMappings>() {
@Override
public void onNext(NetworkAddressMappings networkAddressMappings) {
Assert.assertEquals(networkAddressMappings.getAddressIdsList().get(0).getValue(), 1);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.internal.util.reflection.Whitebox;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author lican
*/
public class SegmentCounterTest {
@Test
public void incrementAndGet() {
//set default value in case in other test has increments
Whitebox.setInternalState(SegmentCounter.INSTANCE, "counter", new AtomicLong(0));
long l = SegmentCounter.INSTANCE.incrementAndGet();
Assert.assertEquals(1L, l);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.register.define.service.IServiceNameService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class ServiceNameDiscoveryServiceHandlerTest {
@Mock
private IServiceNameService serviceNameService;
private ServiceNameDiscoveryServiceHandler serviceNameDiscoveryServiceHandler;
@Before
public void setUp() throws Exception {
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
serviceNameDiscoveryServiceHandler = new ServiceNameDiscoveryServiceHandler(moduleManager);
Whitebox.setInternalState(serviceNameDiscoveryServiceHandler, "serviceNameService", serviceNameService);
}
@Test
public void discovery() {
ServiceNameElement element = ServiceNameElement.newBuilder()
.setApplicationId(10)
.setServiceName("/hello/world")
.setSrcSpanType(SpanType.Entry)
.build();
ServiceNameCollection nameCollection = ServiceNameCollection.newBuilder()
.addElements(element)
.build();
when(serviceNameService.get(anyInt(), anyInt(), anyString())).thenReturn(1);
serviceNameDiscoveryServiceHandler.discovery(nameCollection, new StreamObserver<ServiceNameMappingCollection>() {
@Override
public void onNext(ServiceNameMappingCollection serviceNameMappingCollection) {
ServiceNameMappingElement mappingElement = serviceNameMappingCollection.getElementsList().get(0);
assertEquals(mappingElement.getElement(), element);
int serviceId = mappingElement.getServiceId();
assertEquals(serviceId, 1);
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
}
});
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler;
import io.grpc.stub.StreamObserver;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.service.ISegmentParseService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.network.proto.Downstream;
import org.apache.skywalking.apm.network.proto.UpstreamSegment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class TraceSegmentServiceHandlerTest {
private TraceSegmentServiceHandler traceSegmentServiceHandler;
@Mock
private ISegmentParseService segmentParseService;
@Before
public void setUp() {
System.setProperty("debug", "true");
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
traceSegmentServiceHandler = new TraceSegmentServiceHandler(moduleManager);
Whitebox.setInternalState(traceSegmentServiceHandler, "segmentParseService", segmentParseService);
}
@Test
public void collect() {
StreamObserver<UpstreamSegment> upstreamSegmentStreamObserver = traceSegmentServiceHandler.collect(new StreamObserver<Downstream>() {
@Override
public void onNext(Downstream downstream) {
assertTrue(downstream.isInitialized());
}
@Override
public void onError(Throwable throwable) {
assertTrue(throwable instanceof IllegalArgumentException);
}
@Override
public void onCompleted() {
}
});
UpstreamSegment upstreamSegment = UpstreamSegment.newBuilder().build();
upstreamSegmentStreamObserver.onNext(upstreamSegment);
upstreamSegmentStreamObserver.onError(new IllegalArgumentException("exception"));
upstreamSegmentStreamObserver.onCompleted();
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler.naming;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import javax.servlet.http.HttpServletRequest;
import static org.junit.Assert.assertEquals;
/**
* @author lican
*/
public class AgentGRPCNamingHandlerTest {
private AgentGRPCNamingHandler handler;
@Mock
private HttpServletRequest request;
@Before
public void setUp() throws Exception {
AgentGRPCNamingListener listener = new AgentGRPCNamingListener();
listener.addAddress("127.0.0.1:10080");
handler = new AgentGRPCNamingHandler(listener);
}
@Test
public void pathSpec() {
assertEquals(handler.pathSpec(), "/agent/gRPC");
}
@Test
public void doGet() throws ArgumentsParseException {
JsonElement jsonElement = handler.doGet(request);
String s = ((JsonArray) jsonElement).get(0).getAsString();
assertEquals(s, "127.0.0.1:10080");
}
@Test(expected = UnsupportedOperationException.class)
public void doPost() throws ArgumentsParseException {
handler.doPost(request);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.grpc.provider.handler.naming;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author lican
*/
public class AgentGRPCNamingListenerTest {
private AgentGRPCNamingListener listener;
@Before
public void setUp() throws Exception {
listener = new AgentGRPCNamingListener();
}
@Test
public void path() {
assertEquals(listener.path(), "/agent_gRPC/gRPC");
}
@Test
public void serverJoinNotify() {
listener.serverJoinNotify(null);
}
@Test
public void serverQuitNotify() {
listener.serverQuitNotify(null);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.core.module;
import com.google.common.collect.Lists;
import org.apache.skywalking.apm.collector.grpc.manager.service.GRPCManagerService;
import org.apache.skywalking.apm.collector.server.grpc.GRPCServer;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import java.util.LinkedList;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.when;
/**
* @author lican
*/
public class MockModule extends Module {
public MockModule() throws ServiceNotProvidedException {
ModuleProvider moduleProvider = Mockito.mock(ModuleProvider.class);
LinkedList<ModuleProvider> linkedList = Lists.newLinkedList();
linkedList.add(moduleProvider);
Whitebox.setInternalState(this, "loadedProviders", linkedList);
when(moduleProvider.getService(any())).then(invocation -> {
Class argumentAt = invocation.getArgumentAt(0, Class.class);
Object mock = Mockito.mock(argumentAt);
if (mock instanceof GRPCManagerService) {
when(((GRPCManagerService) mock).createIfAbsent(anyString(), anyInt())).then(invocation1 -> {
GRPCServer grpcServer = new GRPCServer("127.0.0.1", 18098);
grpcServer.initialize();
return grpcServer;
});
}
return mock;
});
}
@Override
public String name() {
return null;
}
@Override
public Class[] services() {
return new Class[0];
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector;
import org.junit.Assert;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author lican
*/
public class DelegatingServletInputStream extends ServletInputStream {
private final InputStream sourceStream;
/**
* Create a DelegatingServletInputStream for the given source stream.
* @param sourceStream the source stream (never <code>null</code>)
*/
public DelegatingServletInputStream(InputStream sourceStream) {
Assert.assertNotNull("Source InputStream must not be null",sourceStream);
this.sourceStream = sourceStream;
}
/**
* Return the underlying source stream (never <code>null</code>).
*/
public final InputStream getSourceStream() {
return this.sourceStream;
}
public int read() throws IOException {
return this.sourceStream.read();
}
public void close() throws IOException {
super.close();
this.sourceStream.close();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider;
import org.apache.skywalking.apm.collector.agent.jetty.define.AgentJettyModule;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentModuleJettyProviderTest {
@Mock
private ModuleManager moduleManager;
@Mock
private AgentModuleJettyConfig config;
private AgentModuleJettyProvider provider;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
provider = new AgentModuleJettyProvider();
Whitebox.setInternalState(provider, "manager", moduleManager);
Whitebox.setInternalState(provider, "config", config);
}
@Test
public void name() {
assertEquals(provider.name(), "jetty");
}
@Test
public void module() {
assertEquals(provider.module(), AgentJettyModule.class);
}
@Test
public void createConfigBeanIfAbsent() {
assertEquals(provider.createConfigBeanIfAbsent(), config);
}
@Test
public void prepare() {
provider.prepare();
}
@Test
public void start() {
provider.start();
}
@Test
public void notifyAfterCompleted() {
provider.notifyAfterCompleted();
}
@Test
public void requiredModules() {
assertTrue(provider.requiredModules().length > 0);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider;
import org.apache.skywalking.apm.collector.cluster.ModuleRegistration;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentModuleJettyRegistrationTest {
private AgentModuleJettyRegistration registration;
@Before
public void setUp() throws Exception {
registration = new AgentModuleJettyRegistration("127.0.0.1", 8080, "/");
}
@Test
public void buildValue() {
ModuleRegistration.Value value = registration.buildValue();
Assert.assertEquals(value.getHostPort(), "127.0.0.1:8080");
assertEquals(value.getContextPath(), "/");
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class ApplicationRegisterServletHandlerTest {
private ApplicationRegisterServletHandler applicationRegisterServletHandler;
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private Gson gson = new Gson();
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
applicationRegisterServletHandler = new ApplicationRegisterServletHandler(moduleManager);
}
@Test
public void pathSpec() {
assertEquals(applicationRegisterServletHandler.pathSpec(), "/application/register");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
applicationRegisterServletHandler.doGet(request);
}
@Test
public void doPost() throws ArgumentsParseException, IOException {
JsonArray array = new JsonArray();
array.add("test_code");
String s = gson.toJson(array);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = applicationRegisterServletHandler.doPost(request);
JsonObject jsonObject = (JsonObject) ((JsonArray) jsonElement).get(0);
assertEquals(jsonObject.get("c").getAsString(), "test_code");
assertEquals(jsonObject.get("i").getAsInt(), 0);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class InstanceDiscoveryServletHandlerTest {
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private Gson gson = new Gson();
private InstanceDiscoveryServletHandler instanceDiscoveryServletHandler;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
instanceDiscoveryServletHandler = new InstanceDiscoveryServletHandler(moduleManager);
}
@Test
public void pathSpec() {
assertEquals(instanceDiscoveryServletHandler.pathSpec(), "/instance/register");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
instanceDiscoveryServletHandler.doGet(request);
}
@Test
public void doPost() throws ArgumentsParseException, IOException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("ai", 1);
jsonObject.addProperty("au", "123");
jsonObject.addProperty("rt", System.currentTimeMillis());
JsonObject osInfo = new JsonObject();
osInfo.addProperty("osName", "test");
jsonObject.add("oi", osInfo);
String s = gson.toJson(jsonObject);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = instanceDiscoveryServletHandler.doPost(request);
int ii = ((JsonObject) jsonElement).get("ii").getAsInt();
assertEquals(ii, 0);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class InstanceHeartBeatServletHandlerTest {
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private Gson gson = new Gson();
private InstanceHeartBeatServletHandler heartBeatServletHandler;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
heartBeatServletHandler = new InstanceHeartBeatServletHandler(moduleManager);
}
@Test
public void pathSpec() {
assertEquals(heartBeatServletHandler.pathSpec(), "/instance/heartbeat");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
heartBeatServletHandler.doGet(request);
}
@Test
public void doPost() throws IOException, ArgumentsParseException {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("ii", 1);
jsonObject.addProperty("ht", System.currentTimeMillis());
String s = gson.toJson(jsonObject);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = heartBeatServletHandler.doPost(request);
assertTrue(jsonElement.isJsonObject());
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class NetworkAddressRegisterServletHandlerTest {
private NetworkAddressRegisterServletHandler handler;
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private Gson gson = new Gson();
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
handler = new NetworkAddressRegisterServletHandler(moduleManager);
}
@Test
public void pathSpec() {
assertEquals(handler.pathSpec(), "/networkAddress/register");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
handler.doGet(request);
}
@Test
public void doPost() throws ArgumentsParseException, IOException {
JsonArray array = new JsonArray();
array.add("127.0.0.1:6379");
array.add("127.0.0.2:6379");
String s = gson.toJson(array);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = handler.doPost(request);
Assert.assertTrue(jsonElement.isJsonArray());
JsonArray js = (JsonArray) jsonElement;
assertTrue(js.size() > 0);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.analysis.register.define.service.IServiceNameService;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class ServiceNameDiscoveryServiceHandlerTest {
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private ServiceNameDiscoveryServiceHandler handler;
private Gson gson = new Gson();
@Mock
private IServiceNameService serviceNameService;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
handler = new ServiceNameDiscoveryServiceHandler(moduleManager);
Whitebox.setInternalState(handler, "serviceNameService", serviceNameService);
}
@Test
public void pathSpec() {
assertEquals(handler.pathSpec(), "/servicename/discovery");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
handler.doGet(request);
}
@Test
public void doPost() throws IOException, ArgumentsParseException {
JsonObject json = new JsonObject();
json.addProperty("ai", 1);
json.addProperty("sn", "test");
json.addProperty("st", 5);
JsonArray array = new JsonArray();
array.add(json);
String s = gson.toJson(array);
when(serviceNameService.getOrCreate(anyInt(), anyInt(), anyString())).thenReturn(2);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = handler.doPost(request);
int serviceId = jsonElement.getAsJsonArray().get(0).getAsJsonObject().get("si").getAsInt();
assertEquals(serviceId, 2);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.collector.DelegatingServletInputStream;
import org.apache.skywalking.apm.collector.core.module.MockModule;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class TraceSegmentServletHandlerTest {
@Mock
private ModuleManager moduleManager;
@Mock
private HttpServletRequest request;
private Gson gson = new Gson();
private TraceSegmentServletHandler handler;
@Before
public void setUp() throws Exception {
when(moduleManager.find(anyString())).then(invocation -> new MockModule());
handler = new TraceSegmentServletHandler(moduleManager);
}
@Test
public void pathSpec() {
Assert.assertEquals(handler.pathSpec(), "/segments");
}
@Test(expected = UnsupportedOperationException.class)
public void doGet() throws ArgumentsParseException {
handler.doGet(request);
}
@Test
public void doPost() throws IOException, ArgumentsParseException {
JsonArray array = new JsonArray();
array.add(230150);
array.add(185809);
array.add(24040000);
JsonArray gtArray = new JsonArray();
gtArray.add(array);
JsonObject json = new JsonObject();
json.add("gt", gtArray);
json.add("sg", new JsonObject());
JsonArray finalArr = new JsonArray();
finalArr.add(json);
String s = gson.toJson(finalArr);
Mockito.when(request.getReader()).then(invocation -> {
DelegatingServletInputStream delegatingServletInputStream = new DelegatingServletInputStream(new ByteArrayInputStream(s.getBytes()));
return new BufferedReader(new InputStreamReader(delegatingServletInputStream));
});
JsonElement jsonElement = handler.doPost(request);
Assert.assertNull(jsonElement);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.naming;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.http.HttpServletRequest;
import static org.junit.Assert.assertEquals;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentJettyNamingHandlerTest {
private AgentJettyNamingHandler agentJettyNamingHandler;
@Mock
private HttpServletRequest request;
private String address = "127.0.0.1:8080";
@Before
public void setUp() {
AgentJettyNamingListener agentJettyNamingListener = new AgentJettyNamingListener();
agentJettyNamingListener.addAddress(address);
agentJettyNamingHandler = new AgentJettyNamingHandler(agentJettyNamingListener);
}
@Test
public void pathSpec() {
assertEquals(agentJettyNamingHandler.pathSpec(), "/agent/jetty");
}
@Test
public void doGet() throws ArgumentsParseException {
JsonElement jsonElement = agentJettyNamingHandler.doGet(request);
assertEquals(((JsonArray) jsonElement).get(0).getAsString(), address);
}
@Test(expected = UnsupportedOperationException.class)
public void doPost() throws ArgumentsParseException {
agentJettyNamingHandler.doPost(request);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.naming;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class AgentJettyNamingListenerTest {
private AgentJettyNamingListener agentJettyNamingListener;
@Before
public void setUp() throws Exception {
agentJettyNamingListener = new AgentJettyNamingListener();
}
@Test
public void path() {
assertEquals(agentJettyNamingListener.path(), "/agent_jetty/jetty");
}
@Test
public void serverJoinNotify() {
agentJettyNamingListener.serverJoinNotify(null);
}
@Test
public void serverQuitNotify() {
agentJettyNamingListener.serverQuitNotify(null);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.stream.JsonReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
/**
* @author lican
*/
public class BaseReader {
private Gson gson = new Gson();
protected JsonReader getReader(JsonElement element) {
return new JsonReader(new InputStreamReader(new ByteArrayInputStream(gson.toJson(element).getBytes())));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.KeyWithStringValue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class KeyWithStringValueJsonReaderTest extends BaseReader {
private KeyWithStringValueJsonReader jsonReader;
@Before
public void setUp() throws Exception {
jsonReader = new KeyWithStringValueJsonReader();
}
@Test
public void read() throws IOException {
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("k", "hello");
jsonObject.addProperty("v", "world");
KeyWithStringValue read = jsonReader.read(getReader(jsonObject));
assertEquals(read.getKey(),"hello");
assertEquals(read.getValue(),"world");
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.LogMessage;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class LogJsonReaderTest extends BaseReader {
private LogJsonReader logJsonReader;
@Before
public void setUp() throws Exception {
logJsonReader = new LogJsonReader();
}
@Test
public void read() throws IOException {
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("k", "hello");
jsonObject.addProperty("v", "world");
JsonArray array = new JsonArray();
array.add(jsonObject);
long l = System.currentTimeMillis();
JsonObject json = new JsonObject();
json.addProperty("ti", l);
json.add("ld", array);
LogMessage read = logJsonReader.read(getReader(json));
Assert.assertEquals(read.getTime(), l);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.TraceSegmentReference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import static org.junit.Assert.assertTrue;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class ReferenceJsonReaderTest extends BaseReader {
private ReferenceJsonReader referenceJsonReader;
@Before
public void setUp() throws Exception {
referenceJsonReader = new ReferenceJsonReader();
}
/**
* {
* "pts": [230150, 185809, 24040000], //parentTraceSegmentId
* "pii": 2, //parentApplicationInstanceId
* "psp": 1, //parentSpanId
* "psi": 0, //parentServiceId
* "psn": "/dubbox-case/case/dubbox-rest", //parentServiceName
* "ni": 0, //networkAddressId
* "nn": "172.25.0.4:20880", //networkAddress
* "eii": 2, //entryApplicationInstanceId
* "esi": 0, //entryServiceId
* "esn": "/dubbox-case/case/dubbox-rest", //entryServiceName
* "rv": 0 //RefType
* }
*/
@Test
public void read() throws IOException {
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
JsonArray ptsArray = new JsonArray();
ptsArray.add(230150L);
ptsArray.add(185809L);
ptsArray.add(24040000L);
jsonObject.add("pts", ptsArray);
jsonObject.addProperty("pii", 2);
jsonObject.addProperty("psp", 1);
jsonObject.addProperty("psi", 0);
jsonObject.addProperty("psn", "/dubbox-case/case/dubbox-rest");
jsonObject.addProperty("ni", 0);
jsonObject.addProperty("nn", "172.25.0.4:20880");
jsonObject.addProperty("eii", 2);
jsonObject.addProperty("esi", 0);
jsonObject.addProperty("esn", "/dubbox-case/case/dubbox-rest");
// jsonObject.addProperty("rn", 0);
//add
jsonObject.addProperty("rv", 1);
TraceSegmentReference read = referenceJsonReader.read(getReader(jsonObject));
assertTrue(read.getParentTraceSegmentId().getIdPartsList().size() == 3);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.TraceSegmentObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import static org.junit.Assert.assertTrue;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class SegmentJsonReaderTest extends BaseReader {
private SegmentJsonReader segmentJsonReader;
@Before
public void setUp() throws Exception {
segmentJsonReader = new SegmentJsonReader();
}
/**
* { //TraceSegmentObject
* "ts": [137150, 185809, 48780000],
* "ai": 2, //applicationId
* "ii": 3, //applicationInstanceId
* "ss": []//SpanObject
*/
@Test
public void read() throws IOException {
JsonArray tsArray = new JsonArray();
tsArray.add(137150);
tsArray.add(185809);
tsArray.add(48780000);
JsonObject json = new JsonObject();
json.add("ts", tsArray);
json.addProperty("ai", 2);
json.addProperty("ii", 3);
json.add("ss", new JsonArray());
TraceSegmentObject.Builder read = segmentJsonReader.read(getReader(json));
TraceSegmentObject build = read.build();
assertTrue(build.getTraceSegmentId().getIdPartsList().size() == 3);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.SpanObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class SpanJsonReaderTest extends BaseReader {
private SpanJsonReader spanJsonReader;
@Before
public void setUp() throws Exception {
spanJsonReader = new SpanJsonReader();
}
/**
* {
* "si": 0, //spanId
* "tv": 0, //SpanType
* "lv": 2, //SpanLayer
* "ps": -1, //parentSpanId
* "st": 1501858094726, //startTime
* "et": 1501858096804, //endTime
* "ci": 3, //componentId
* "cn": "", //component
* "oi": 0, //operationNameId
* "on": "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()", //operationName
* "pi": 0, //peerId
* "pn": "", //peer
* "ie": false, //isError
* "rs": [ //TraceSegmentReference],
* "to": [ //KeyWithStringValue ],
* "lo": [] //log
* }
*/
@Test
public void read() throws IOException {
JsonObject json = new JsonObject();
json.addProperty("si", 1);
json.addProperty("tv", 0);
json.addProperty("lv", 2);
json.addProperty("ps", -1);
json.addProperty("st", 1501858094726L);
json.addProperty("et", 1501858096804L);
json.addProperty("ci", 3);
json.addProperty("cn", "redis");
json.addProperty("oi", 0);
json.addProperty("on", "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()");
json.addProperty("pi", 0);
json.addProperty("pn", "127.0.0.1:6379");
json.addProperty("ie", false);
json.add("rs", new JsonArray());
json.add("to", new JsonArray());
json.add("lo", new JsonArray());
SpanObject read = spanJsonReader.read(getReader(json));
assertEquals(read.getSpanId(), 1);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.agent.jetty.provider.handler.reader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.skywalking.apm.network.proto.UniqueId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertTrue;
/**
* @author lican
*/
@RunWith(MockitoJUnitRunner.class)
public class TraceSegmentJsonReaderTest extends BaseReader {
private TraceSegmentJsonReader traceSegmentJsonReader;
@Before
public void setUp() throws Exception {
traceSegmentJsonReader = new TraceSegmentJsonReader();
}
/**
* {
* "gt": [[230150, 185809, 24040000]],
* "sg": { }//TraceSegmentObject
*/
@Test
public void read() throws IOException {
JsonArray array = new JsonArray();
array.add(230150);
array.add(185809);
array.add(24040000);
JsonArray gtArray = new JsonArray();
gtArray.add(array);
JsonObject json = new JsonObject();
json.add("gt", gtArray);
json.add("sg", new JsonObject());
TraceSegment read = traceSegmentJsonReader.read(getReader(json));
List<UniqueId> globalTraceIdsList = read.getUpstreamSegment().getGlobalTraceIdsList();
assertTrue(globalTraceIdsList.size() == 1);
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.skywalking.apm.collector.core.module;
import com.google.common.collect.Lists;
import org.apache.skywalking.apm.collector.jetty.manager.service.JettyManagerService;
import org.apache.skywalking.apm.collector.server.jetty.JettyServer;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.api.mockito.PowerMockito;
import java.util.LinkedList;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
/**
* @author lican
*/
public class MockModule extends Module {
public MockModule() throws ServiceNotProvidedException {
ModuleProvider moduleProvider = Mockito.mock(ModuleProvider.class);
LinkedList<ModuleProvider> linkedList = Lists.newLinkedList();
linkedList.add(moduleProvider);
Whitebox.setInternalState(this, "loadedProviders", linkedList);
when(moduleProvider.getService(any())).then(invocation -> {
Class argumentAt = invocation.getArgumentAt(0, Class.class);
Object mock = Mockito.mock(argumentAt);
if (mock instanceof JettyManagerService) {
PowerMockito.when(((JettyManagerService) mock).createIfAbsent(anyString(), anyInt(), anyString())).then(invocation1 -> {
JettyServer jettyServer = new JettyServer("127.0.0.1", 10806, "/");
jettyServer.initialize();
return jettyServer;
});
}
return mock;
});
}
@Override
public String name() {
return null;
}
@Override
public Class[] services() {
return new Class[0];
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册