Skip to content

Commit

Permalink
[no issue] add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
koo-taejin committed Feb 8, 2017
1 parent 3868170 commit a7d87e9
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo;
import com.navercorp.pinpoint.web.mapper.stat.sampling.sampler.DataSourceSampler;
import com.navercorp.pinpoint.web.test.util.DataSourceTestUtils;
import com.navercorp.pinpoint.web.vo.stat.SampledDataSource;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

Expand All @@ -41,32 +41,13 @@ public class DataSourceSamplerTest {
public void sampleDataPointsTest1() throws Exception {
int testObjectSize = RANDOM.nextInt(CREATE_TEST_OBJECT_MAX_SIZE) + 1;
int maxConnectionSize = RANDOM.nextInt(MIN_VALUE_OF_MAX_CONNECTION_SIZE) + MIN_VALUE_OF_MAX_CONNECTION_SIZE;
List<DataSourceBo> dataSourceBoList = createDataSourceBoList(testObjectSize, maxConnectionSize);
List<DataSourceBo> dataSourceBoList = DataSourceTestUtils.createDataSourceBoList(1, testObjectSize, maxConnectionSize);

SampledDataSource sampledDataSource = sampler.sampleDataPoints(0, System.currentTimeMillis(), dataSourceBoList, null);

assertEquals(sampledDataSource, dataSourceBoList);
}

private List<DataSourceBo> createDataSourceBoList(int dataSourceSize, int maxConnectionSize) {
List<DataSourceBo> result = new ArrayList<>(dataSourceSize);

for (int i = 0; i < dataSourceSize; i++) {
DataSourceBo dataSourceBo = createDataSourceBo(maxConnectionSize);
result.add(dataSourceBo);
}

return result;
}

private DataSourceBo createDataSourceBo(int maxConnectionSize) {
DataSourceBo dataSourceBo = new DataSourceBo();
dataSourceBo.setActiveConnectionSize(RANDOM.nextInt(maxConnectionSize));
dataSourceBo.setMaxConnectionSize(maxConnectionSize);

return dataSourceBo;
}

private void assertEquals(SampledDataSource sampledDataSource, List<DataSourceBo> dataSourceBoList) {
int minActiveConnectionSize = Integer.MAX_VALUE;
int maxActiveConnectionSize = Integer.MIN_VALUE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.web.test.util;

import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* @author Taejin Koo
*/
public class DataSourceTestUtils {

private static final Random RANDOM = new Random(System.currentTimeMillis());

public static List<DataSourceBo> createDataSourceBoList(int id, int dataSourceSize, int maxConnectionSize) {
List<DataSourceBo> result = new ArrayList<>(dataSourceSize);

for (int i = 0; i < dataSourceSize; i++) {
DataSourceBo dataSourceBo = createDataSourceBo(id, maxConnectionSize);
result.add(dataSourceBo);
}

return result;
}

private static DataSourceBo createDataSourceBo(int id, int maxConnectionSize) {
DataSourceBo dataSourceBo = new DataSourceBo();
dataSourceBo.setId(id);
dataSourceBo.setActiveConnectionSize(RANDOM.nextInt(maxConnectionSize));
dataSourceBo.setMaxConnectionSize(maxConnectionSize);
return dataSourceBo;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2017 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.web.view;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.mapper.stat.sampling.sampler.DataSourceSampler;
import com.navercorp.pinpoint.web.test.util.DataSourceTestUtils;
import com.navercorp.pinpoint.web.util.TimeWindow;
import com.navercorp.pinpoint.web.vo.Range;
import com.navercorp.pinpoint.web.vo.stat.SampledDataSource;
import com.navercorp.pinpoint.web.vo.stat.chart.DataSourceChartGroup;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

/**
* @author Taejin Koo
*/
public class DataSourceChartGroupSerializerTest {

private static final Random RANDOM = new Random(System.currentTimeMillis());
private static final int MIN_VALUE_OF_MAX_CONNECTION_SIZE = 20;
private static final int CREATE_TEST_OBJECT_MAX_SIZE = 10;

private final DataSourceSampler sampler = new DataSourceSampler();

private ObjectMapper mapper = new ObjectMapper();

@Mock
private ServiceTypeRegistryService serviceTypeRegistryService;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(serviceTypeRegistryService.findServiceType(any(Short.class))).thenReturn(ServiceType.UNKNOWN);
}

@Test
public void serializeTest() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
TimeWindow timeWindow = new TimeWindow(new Range(currentTimeMillis - 300000, currentTimeMillis));

List<SampledDataSource> sampledDataSourceList = createSampledDataSourceList(timeWindow);
DataSourceChartGroup dataSourceChartGroup = new DataSourceChartGroup(timeWindow, sampledDataSourceList, serviceTypeRegistryService);

String jsonValue = mapper.writeValueAsString(dataSourceChartGroup);
Map map = mapper.readValue(jsonValue, Map.class);

Assert.assertTrue(map.containsKey("id"));
Assert.assertTrue(map.containsKey("jdbcUrl"));
Assert.assertTrue(map.containsKey("poolName"));
Assert.assertTrue(map.containsKey("serviceType"));
Assert.assertTrue(map.containsKey("charts"));
}

private List<SampledDataSource> createSampledDataSourceList(TimeWindow timeWindow) {
List<SampledDataSource> sampledDataSourceList = new ArrayList<>();

int maxConnectionSize = RANDOM.nextInt(MIN_VALUE_OF_MAX_CONNECTION_SIZE) + MIN_VALUE_OF_MAX_CONNECTION_SIZE;

long from = timeWindow.getWindowRange().getFrom();
long to = timeWindow.getWindowRange().getTo();

for (long i = from; i < to; i += timeWindow.getWindowSlotSize()) {
sampledDataSourceList.add(createSampledDataSource(i, maxConnectionSize));
}

return sampledDataSourceList;
}

private SampledDataSource createSampledDataSource(long timestamp, int maxConnectionSize) {
int testObjectSize = RANDOM.nextInt(CREATE_TEST_OBJECT_MAX_SIZE) + 1;
List<DataSourceBo> dataSourceBoList = DataSourceTestUtils.createDataSourceBoList(1, testObjectSize, maxConnectionSize);
return sampler.sampleDataPoints(0, timestamp, dataSourceBoList, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.mapper.stat.sampling.sampler.DataSourceSampler;
import com.navercorp.pinpoint.web.test.util.DataSourceTestUtils;
import com.navercorp.pinpoint.web.util.TimeWindow;
import com.navercorp.pinpoint.web.vo.Range;
import com.navercorp.pinpoint.web.vo.chart.Chart;
Expand All @@ -32,6 +33,7 @@
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -60,7 +62,7 @@ public void setUp() throws Exception {
}

@Test
public void basicFunctionTest() throws Exception {
public void basicFunctionTest1() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
TimeWindow timeWindow = new TimeWindow(new Range(currentTimeMillis - 300000, currentTimeMillis));

Expand All @@ -70,6 +72,20 @@ public void basicFunctionTest() throws Exception {
assertEquals(sampledDataSourceList, dataSourceChartGroup);
}

@Test
public void basicFunctionTest2() throws Exception {
long currentTimeMillis = System.currentTimeMillis();
TimeWindow timeWindow = new TimeWindow(new Range(currentTimeMillis - 300000, currentTimeMillis));

List<SampledDataSource> sampledDataSourceList = Collections.emptyList();
DataSourceChartGroup dataSourceChartGroup = new DataSourceChartGroup(timeWindow, sampledDataSourceList, serviceTypeRegistryService);

Assert.assertEquals(-1, dataSourceChartGroup.getId());
Assert.assertEquals(null, dataSourceChartGroup.getJdbcUrl());
Assert.assertEquals(null, dataSourceChartGroup.getPoolName());
Assert.assertEquals(null, dataSourceChartGroup.getServiceTypeName());
}

private List<SampledDataSource> createSampledDataSourceList(TimeWindow timeWindow) {
List<SampledDataSource> sampledDataSourceList = new ArrayList<>();

Expand All @@ -87,29 +103,10 @@ private List<SampledDataSource> createSampledDataSourceList(TimeWindow timeWindo

private SampledDataSource createSampledDataSource(long timestamp, int maxConnectionSize) {
int testObjectSize = RANDOM.nextInt(CREATE_TEST_OBJECT_MAX_SIZE) + 1;
List<DataSourceBo> dataSourceBoList = createDataSourceBoList(testObjectSize, maxConnectionSize);
List<DataSourceBo> dataSourceBoList = DataSourceTestUtils.createDataSourceBoList(1, testObjectSize, maxConnectionSize);
return sampler.sampleDataPoints(0, timestamp, dataSourceBoList, null);
}

private List<DataSourceBo> createDataSourceBoList(int dataSourceSize, int maxConnectionSize) {
List<DataSourceBo> result = new ArrayList<>(dataSourceSize);

for (int i = 0; i < dataSourceSize; i++) {
DataSourceBo dataSourceBo = createDataSourceBo(maxConnectionSize);
result.add(dataSourceBo);
}

return result;
}

private DataSourceBo createDataSourceBo(int maxConnectionSize) {
DataSourceBo dataSourceBo = new DataSourceBo();
dataSourceBo.setId(1);
dataSourceBo.setActiveConnectionSize(RANDOM.nextInt(maxConnectionSize));
dataSourceBo.setMaxConnectionSize(maxConnectionSize);
return dataSourceBo;
}

private void assertEquals(List<SampledDataSource> sampledDataSourceList, DataSourceChartGroup dataSourceChartGroup) {
Map<AgentStatChartGroup.ChartType, Chart> charts = dataSourceChartGroup.getCharts();

Expand Down

0 comments on commit a7d87e9

Please sign in to comment.