diff --git a/src/test/java/org/gcube/accounting/aggregator/plugin/MyTest.java b/src/test/java/org/gcube/accounting/aggregator/plugin/MyTest.java index 2df84d4..44accf7 100644 --- a/src/test/java/org/gcube/accounting/aggregator/plugin/MyTest.java +++ b/src/test/java/org/gcube/accounting/aggregator/plugin/MyTest.java @@ -15,13 +15,18 @@ import org.gcube.accounting.aggregator.utility.Utility; import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord; +import org.gcube.accounting.datamodel.aggregation.AggregatedStorageUsageRecord; import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; +import org.gcube.accounting.datamodel.usagerecords.StorageUsageRecord; +import org.gcube.documentstore.records.AggregatedRecord; import org.gcube.documentstore.records.Record; import org.gcube.documentstore.records.RecordUtility; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.couchbase.client.java.document.json.JsonObject; + public class MyTest { private static Logger logger = LoggerFactory.getLogger(Elaborator.class); @@ -41,7 +46,104 @@ public class MyTest { durationForHuman); } + + private static final String USAGE_RECORD_TYPE = "usageRecordType"; + private static final String SINGLE = "Single"; + private static final String SIMPLE = "Simple"; + + @Test + public void testGetRecord() throws Exception { + RecordUtility.addRecordPackage(StorageUsageRecord.class.getPackage()); + RecordUtility.addRecordPackage(AggregatedStorageUsageRecord.class.getPackage()); + + String jsonString = "{\"operationCount\":41.0,\"creationTime\":1.454284803916E12,\"consumerId\":\"wps.statisticalmanager\",\"resourceOwner\":\"wps.statisticalmanager\",\"recordType\":\"SingleStorageUsageRecord\",\"dataType\":\"STORAGE\",\"_rev\":\"1-05c467553141723f51dad0fa1aab2ce0\",\"resourceURI\":\"56aea0025caf8b5b69c0071e\",\"providerURI\":\"data.d4science.org\",\"resourceScope\":\"/d4science.research-infrastructures.eu\",\"dataVolume\":95807.0,\"scope\":\"/d4science.research-infrastructures.eu\",\"startTime\":1.454284803059E12,\"operationType\":\"CREATE\",\"endTime\":1.454284803059E12,\"id\":\"ff5f2669-0abb-45e9-99df-f401db579680\",\"_id\":\"ff5f2669-0abb-45e9-99df-f401db579680\",\"operationResult\":\"SUCCESS\"};"; + + JsonObject content = JsonObject.fromJson(jsonString); + + if(content.containsKey(USAGE_RECORD_TYPE)){ + String recordType = content.getString(USAGE_RECORD_TYPE); + content.removeKey(USAGE_RECORD_TYPE); + content.put(Record.RECORD_TYPE, recordType); + } + + Boolean aggregated = false; + + if(content.containsKey(AggregatedRecord.CREATION_TIME)) { + Object object = content.get(AggregatedRecord.CREATION_TIME); + if(object instanceof Double) { + Double d = ((Double) object); + content.put(AggregatedRecord.CREATION_TIME, d.longValue()); + } + } + if(content.containsKey(AggregatedRecord.START_TIME)) { + aggregated = true; + Object object = content.get(AggregatedRecord.START_TIME); + if(object instanceof Double) { + Double d = ((Double) object); + content.put(AggregatedRecord.START_TIME, d.longValue()); + } + } + + if(content.containsKey(AggregatedRecord.END_TIME)) { + aggregated = true; + Object object = content.get(AggregatedRecord.END_TIME); + if(object instanceof Double) { + Double d = ((Double) object); + content.put(AggregatedRecord.END_TIME, d.longValue()); + } + } + + if(content.containsKey(AggregatedRecord.OPERATION_COUNT)) { + Object object = content.get(AggregatedRecord.OPERATION_COUNT); + if(object instanceof Double) { + Double d = ((Double) object); + content.put(AggregatedRecord.OPERATION_COUNT, d.intValue()); + } + + if(content.getInt(AggregatedRecord.OPERATION_COUNT)>1) { + aggregated = true; + } + } + + if(aggregated) { + content.put(AggregatedRecord.AGGREGATED, true); + } + + String recordType = content.getString(Record.RECORD_TYPE); + + if(!aggregated){ + if(recordType.startsWith(SIMPLE)){ + recordType = recordType.replace(SIMPLE, SINGLE); + content.put(Record.RECORD_TYPE, recordType); + } + + if(!recordType.startsWith(SINGLE)) { + recordType = SINGLE + recordType; + content.put(Record.RECORD_TYPE, recordType); + } + }else { + if(recordType.startsWith(SIMPLE)){ + recordType = recordType.replace(SIMPLE, ""); + content.put(Record.RECORD_TYPE, recordType); + } + + if(recordType.startsWith(SINGLE)) { + recordType = recordType.replace(SINGLE, ""); + content.put(Record.RECORD_TYPE, recordType); + } + } + + jsonString = content.toString(); + + try { + Record r = RecordUtility.getRecord(jsonString); + logger.info("{}", r); + }catch (Exception e) { + logger.error("", e); + } + } + @Test public void classesTest() throws InstantiationException, IllegalAccessException { RecordUtility.addRecordPackage(ServiceUsageRecord.class.getPackage());