|
| 1 | +package com.oneapm.tps.agent.service.domain; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.lang.reflect.Modifier; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import static com.google.common.base.CaseFormat.LOWER_CAMEL; |
| 14 | +import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE; |
| 15 | +import static org.hamcrest.core.Is.is; |
| 16 | +import static org.junit.Assert.assertArrayEquals; |
| 17 | +import static org.junit.Assert.assertThat; |
| 18 | + |
| 19 | +public class SqlTraceDataEntityReflectionTest { |
| 20 | + private final Map<String, String> expectedFields = new HashMap<String, String>() { |
| 21 | + { |
| 22 | + put("sql_trace_id", "bigint"); |
| 23 | + put("blame_metric_name", "varchar"); |
| 24 | + put("uri", "varchar"); |
| 25 | + put("sql_id", "bigint"); |
| 26 | + put("metric_name", "varchar"); |
| 27 | + put("call_count", "int"); |
| 28 | + put("min_time", "float"); |
| 29 | + put("max_time", "float"); |
| 30 | + put("application_id", "bigint"); |
| 31 | + put("parameters", "varchar"); |
| 32 | + put("total_time", "float"); |
| 33 | + put("avg_time", "float"); |
| 34 | + put("sql_info", "varchar"); |
| 35 | + put("record_time", "bigint"); |
| 36 | + put("agent_run_id", "bigint"); |
| 37 | + put("user_id", "bigint"); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + private final Map<String, String> types = new HashMap<String, String>() {{ |
| 42 | + put("long", "bigint"); |
| 43 | + put("int", "int"); |
| 44 | + put("float", "float"); |
| 45 | + put("class java.lang.String", "varchar"); |
| 46 | + }}; |
| 47 | + |
| 48 | + @Test |
| 49 | + public void dataSourceHasExpectedFields() throws ClassNotFoundException { |
| 50 | + Class<?> dataSourceEntity = Class.forName("com.oneapm.tps.agent.service.domain.SqltraceDataEntity"); |
| 51 | + |
| 52 | + List<String> actualFieldNames = new ArrayList<>(); |
| 53 | + Map<String, String> actualFields = new HashMap<>(); |
| 54 | + for (Field field : dataSourceEntity.getDeclaredFields()) { |
| 55 | + String nameWithUnderscore = LOWER_CAMEL.to(LOWER_UNDERSCORE, field.getName()); |
| 56 | + actualFieldNames.add(nameWithUnderscore); |
| 57 | + actualFields.put(nameWithUnderscore, types.get(field.getType().toString())); |
| 58 | + |
| 59 | + assertThat(field.getModifiers(), is(Modifier.PRIVATE)); |
| 60 | + } |
| 61 | + |
| 62 | + List<String> expectedNames = new ArrayList<>(expectedFields.keySet()); |
| 63 | + Collections.sort(expectedNames); |
| 64 | + Collections.sort(actualFieldNames); |
| 65 | + |
| 66 | + assertArrayEquals(expectedNames.toArray(), actualFieldNames.toArray()); |
| 67 | + |
| 68 | + for (String fieldName : expectedFields.keySet()) { |
| 69 | + assertThat(actualFields.containsKey(fieldName), is(true)); |
| 70 | + |
| 71 | + assertThat( |
| 72 | + String.format("field type of %s", fieldName), |
| 73 | + actualFields.get(fieldName), |
| 74 | + is(expectedFields.get(fieldName))); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments