dnet-hadoop/dhp-common/src/test/java/eu/dnetlib/dhp/monitor/AggregationMetricsTest.java

117 lines
3.7 KiB
Java

package eu.dnetlib.dhp.monitor;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import eu.dnetlib.dhp.monitor.model.AggregationMetric;
import eu.dnetlib.dhp.monitor.model.MonitorException;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
/**
* The type Aggregation metrics test.
*/
public class AggregationMetricsTest {
/**
* Test aggregation builder
* Here we test case where builder should work
* and where it should raise exception
*
* @throws Exception the exception
*/
@Test
public void testAggregationBuilder() throws Exception {
// TEST AGGREGATION METRICS INITIALIZED CORRECTLY
AggregationMetric metric = new AggregationMetric.AggregationBuilder()
.createCollectionMetrics()
.withRefreshMode()
.withDatasourceAPI("API___1234")
.withDatasourceName("A Datasource")
.withDatasourceId("DS_ID_1")
.build("2021-06-27T13:03:14+00:00", 100L);
assertNotNull(metric);
assertEquals(MonitorConstant.AGGREGATION_COLLECTION_METRIC, metric.getName());
assertEquals(100L, metric.getTotal());
assertEquals(1624791794000L, metric.getTimestampDate().toEpochMilli());
assertEquals(4, metric.getLabels().size());
System.out.println(metric.asInfluxDBPoint().toLineProtocol());
assertThrows(MonitorException.class, ()-> {
AggregationMetric.AggregationBuilder builder = new AggregationMetric.AggregationBuilder();
builder.build("A-b_c",null);
});
assertThrows(MonitorException.class, ()-> {
AggregationMetric.AggregationBuilder builder = new AggregationMetric.AggregationBuilder()
.createTransformationMetrics()
.withIncrementalMode();
builder.build("A-b_c",null);
});
assertThrows(MonitorException.class, ()-> {
AggregationMetric.AggregationBuilder builder = new AggregationMetric.AggregationBuilder()
.createTransformationMetrics()
.withIncrementalMode();
builder.build("2021-06-27T13:03:14+00:00",null);
});
assertDoesNotThrow(() -> {
AggregationMetric.AggregationBuilder builder = new AggregationMetric.AggregationBuilder()
.createTransformationMetrics()
.withIncrementalMode()
.withDatasourceAPI("PAI")
.withDatasourceId("ID")
.withDatasourceName("NAME");
builder.build("2021-06-27T13:03:14+00:00",100L);
});
}
@Test
public void testConnection () throws Exception {
String token = "jhB7Ixgn5uAUhy2Jgwod3rGscifmc30woUln05v0RgRYOqpGzpPTnTIA8bjwbqTXccSVcEfUwiHl_ESrmqz8Lg==";
String bucket = "Aggregation";
String org = "ISTI CNR";
InfluxDBClient client = InfluxDBClientFactory.create("https://ip-90-147-167-221.ct1.garrservices.it:8086", token.toCharArray());
String fileName = "/Users/sandro/Develop/python/monitor/metrics/part-00000";
//read file into stream, try-with-resources
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
final AtomicInteger count=new AtomicInteger(0);
stream.forEach(s-> count.incrementAndGet());
System.out.println("count = " + count.get());
} catch (IOException e) {
e.printStackTrace();
}
}
}