diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/pom.xml b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/pom.xml index 3ded0e2ec2259f66410696f541b51abc55510c1b..c5b254cb6a3af61d4408589fc10edfb42f7e4b16 100644 --- a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/pom.xml +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/pom.xml @@ -27,6 +27,9 @@ 4.0.0 agent-grpc-provider + + 1.10.0 + @@ -69,5 +72,17 @@ register-define ${project.version} + + io.grpc + grpc-testing + ${grpc.version} + test + + + mockito-core + org.mockito + + + \ No newline at end of file diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCProviderTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCProviderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8196f4445e65ae4a660d9e19b6e06aac4dec6a95 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCProviderTest.java @@ -0,0 +1,102 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCRegistrationTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCRegistrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5239ca19f5e97ffd7430b7e36d55898d4fb439a3 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AgentModuleGRPCRegistrationTest.java @@ -0,0 +1,48 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AuthenticationSimpleCheckerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AuthenticationSimpleCheckerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..fea67e916a9fb24137b12285c1636dfb00483232 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/AuthenticationSimpleCheckerTest.java @@ -0,0 +1,105 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ApplicationRegisterServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ApplicationRegisterServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..554aead6dd21d648a4a2b2f7f91bb8e394d9d2e5 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ApplicationRegisterServiceHandlerTest.java @@ -0,0 +1,81 @@ +/* + * 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() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/InstanceDiscoveryServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/InstanceDiscoveryServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d3ab0801097227d553c8e399b5f26e132933b9b7 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/InstanceDiscoveryServiceHandlerTest.java @@ -0,0 +1,121 @@ +/* + * 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() { + @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() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/JVMMetricsServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/JVMMetricsServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3194d9254affc65bc2f36d0ae08dde43af124f4c --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/JVMMetricsServiceHandlerTest.java @@ -0,0 +1,154 @@ +/* + * 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 beans = ManagementFactory.getGarbageCollectorMXBeans(); + List 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 memoryList = new LinkedList(); + + 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 memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); + List poolList = new LinkedList(); + 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() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/NetworkAddressRegisterServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/NetworkAddressRegisterServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5becd467adff2096f8805f7c3f6016ba166ec036 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/NetworkAddressRegisterServiceHandlerTest.java @@ -0,0 +1,81 @@ +/* + * 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() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/SegmentCounterTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/SegmentCounterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..30f8191d32ed1db5a9b73022262ea8f1c63bafc0 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/SegmentCounterTest.java @@ -0,0 +1,38 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..cf91d47a067b757957daab1e991c0b1cdea57ba1 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java @@ -0,0 +1,89 @@ +/* + * 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() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/TraceSegmentServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/TraceSegmentServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3a260c535fe5f00e755fe9e7ff532eaf7deb5ebd --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/TraceSegmentServiceHandlerTest.java @@ -0,0 +1,83 @@ +/* + * 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 upstreamSegmentStreamObserver = traceSegmentServiceHandler.collect(new StreamObserver() { + @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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingHandlerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4db8a54b11e701a7d6b0adcf200fb0625054dcc4 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingHandlerTest.java @@ -0,0 +1,65 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingListenerTest.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingListenerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..997ac4872ef48e0b6e4eef33dd4ee0168c67a6b3 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/agent/grpc/provider/handler/naming/AgentGRPCNamingListenerTest.java @@ -0,0 +1,51 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java new file mode 100644 index 0000000000000000000000000000000000000000..8a90879a3bb446a52eccf62c5dc915c8dea8e14a --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-grpc/agent-grpc-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java @@ -0,0 +1,66 @@ +/* + * 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 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]; + } + + +} diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/DelegatingServletInputStream.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/DelegatingServletInputStream.java new file mode 100644 index 0000000000000000000000000000000000000000..c88b1613f2fe999c3d7e148257f0135407156772 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/DelegatingServletInputStream.java @@ -0,0 +1,75 @@ +/* + * 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 null) + */ + public DelegatingServletInputStream(InputStream sourceStream) { + Assert.assertNotNull("Source InputStream must not be null",sourceStream); + this.sourceStream = sourceStream; + } + + /** + * Return the underlying source stream (never null). + */ + 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) { + + } +} diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyProviderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyProviderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f09acca70f973f32ef11bc6122f41d918a62b843 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyProviderTest.java @@ -0,0 +1,92 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyRegistrationTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyRegistrationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1524eca9cebfce6d7788d67dcb424346d5e8e215 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/AgentModuleJettyRegistrationTest.java @@ -0,0 +1,48 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ApplicationRegisterServletHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ApplicationRegisterServletHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..423e01a8f1979be27f5fed67dbd3f322c7aa9095 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ApplicationRegisterServletHandlerTest.java @@ -0,0 +1,90 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceDiscoveryServletHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceDiscoveryServletHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f23d13305da469b4ee42f54bdb12ba212ea148c8 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceDiscoveryServletHandlerTest.java @@ -0,0 +1,94 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceHeartBeatServletHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceHeartBeatServletHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..028b9654cdf436c661866a670e86feadb8feb0ef --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/InstanceHeartBeatServletHandlerTest.java @@ -0,0 +1,90 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/NetworkAddressRegisterServletHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/NetworkAddressRegisterServletHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5d544a80e1a2ac4c978a9b758c650dfe9e20f878 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/NetworkAddressRegisterServletHandlerTest.java @@ -0,0 +1,92 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..226f79d5e58df0bdb003c3719ad0f1e68b951caf --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/ServiceNameDiscoveryServiceHandlerTest.java @@ -0,0 +1,105 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/TraceSegmentServletHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/TraceSegmentServletHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d3ff32016fa97dfb8bf5dde343f6c3b74d34bade --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/TraceSegmentServletHandlerTest.java @@ -0,0 +1,106 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingHandlerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingHandlerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bfc830a2048c405c3d678a2e191568b85954af50 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingHandlerTest.java @@ -0,0 +1,69 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingListenerTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingListenerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..c1608defd24f532cfe7ace7a1d587f9afed094e8 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/naming/AgentJettyNamingListenerTest.java @@ -0,0 +1,55 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/BaseReader.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/BaseReader.java new file mode 100644 index 0000000000000000000000000000000000000000..46c7e07d0ef1b258e4fe215e78c8a5ba7d46cc9d --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/BaseReader.java @@ -0,0 +1,37 @@ +/* + * 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()))); + } +} diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/KeyWithStringValueJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/KeyWithStringValueJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7607ba22dc0014f5f88d387e0a0ca740efae2fe0 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/KeyWithStringValueJsonReaderTest.java @@ -0,0 +1,55 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/LogJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/LogJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b3ca1cfbff0534ce0bf1764a9b5068400cc75ce7 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/LogJsonReaderTest.java @@ -0,0 +1,67 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/ReferenceJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/ReferenceJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4ceeabac42461605f5459eb8b15d2f9adb3edfa3 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/ReferenceJsonReaderTest.java @@ -0,0 +1,87 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SegmentJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SegmentJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..712948dac49af45654e44df3dc0cc01d8293515c --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SegmentJsonReaderTest.java @@ -0,0 +1,68 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SpanJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SpanJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..eb4e649bd0eb5f58ba91df00cd7728900e8b39d1 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/SpanJsonReaderTest.java @@ -0,0 +1,87 @@ +/* + * 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 diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/TraceSegmentJsonReaderTest.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/TraceSegmentJsonReaderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..969c9b00aa570330b6425c6c17acc690eefa0d50 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/agent/jetty/provider/handler/reader/TraceSegmentJsonReaderTest.java @@ -0,0 +1,66 @@ +/* + * 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 globalTraceIdsList = read.getUpstreamSegment().getGlobalTraceIdsList(); + assertTrue(globalTraceIdsList.size() == 1); + } +} \ No newline at end of file diff --git a/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java new file mode 100644 index 0000000000000000000000000000000000000000..d987292b7e6495e79f47edc676c6e8f6cc3b7fc4 --- /dev/null +++ b/apm-collector/apm-collector-agent/agent-jetty/agent-jetty-provider/src/test/java/org/apache/skywalking/apm/collector/core/module/MockModule.java @@ -0,0 +1,70 @@ +/* + * 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 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]; + } + + +}