363 lines
13 KiB
Java
363 lines
13 KiB
Java
package org.gcube.application;
|
|
|
|
import java.io.IOException;
|
|
import java.time.LocalDate;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
import java.util.stream.StreamSupport;
|
|
|
|
import org.gcube.application.geoportal.common.model.configuration.Configuration;
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
|
|
import org.gcube.application.geoportal.common.model.document.relationships.Relationship;
|
|
import org.gcube.application.geoportal.common.model.document.relationships.RelationshipNavigationObject;
|
|
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
|
|
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
|
|
import org.gcube.application.geoportalcommon.geoportal.GeoportalClientCaller;
|
|
import org.gcube.application.geoportalcommon.geoportal.ProjectsCaller;
|
|
import org.gcube.application.geoportalcommon.shared.SearchingFilter;
|
|
import org.gcube.application.geoportalcommon.shared.SearchingFilter.LOGICAL_OP;
|
|
import org.gcube.application.geoportalcommon.shared.WhereClause;
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.ResultDocumentDV;
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.project.LifecycleInformationDV;
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.project.PhaseDV;
|
|
import org.gcube.application.geoportalcommon.shared.geoportal.project.ProjectDV;
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
|
public class Project_Tests {
|
|
|
|
private ProjectsCaller client = null;
|
|
// private static String CONTEXT = "/pred4s/preprod/preVRE";
|
|
// private static String TOKEN = ""; //preVRE
|
|
|
|
//private static String CONTEXT = "/gcube/devsec/devVRE";
|
|
//private static String TOKEN = ""; // devVRE
|
|
private static String PROFILE_ID = "profiledConcessioni";
|
|
private static String PROJECT_ID = "6365485fa8d67f4c82794cc4";
|
|
|
|
private static String CONTEXT = "/pred4s/preprod/preVRE";
|
|
private static String TOKEN = ""; //preVRE
|
|
|
|
@Before
|
|
public void getClient() {
|
|
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
|
|
ScopeProvider.instance.set(CONTEXT);
|
|
SecurityTokenProvider.instance.set(TOKEN);
|
|
client = GeoportalClientCaller.projects();
|
|
}
|
|
|
|
// @Test
|
|
public void getList() throws Exception {
|
|
List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
|
|
|
|
int i = 0;
|
|
for (Project project : listOfProjects) {
|
|
System.out.println(++i + ") " + project);
|
|
}
|
|
}
|
|
|
|
//@Test
|
|
public void getByID() throws Exception {
|
|
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
|
|
ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder);
|
|
System.out.println(projectDV);
|
|
|
|
LinkedHashMap<String, Object> theMap = projectDV.getTheDocument().getDocumentAsMap();
|
|
for (String key : theMap.keySet()) {
|
|
Object theValue = theMap.get(key);
|
|
System.out.println("The key: " + key + " has value: " + theValue);
|
|
}
|
|
|
|
System.out.println(projectDV.getSpatialReference());
|
|
}
|
|
|
|
//@Test
|
|
public void getListProjectsDV() throws Exception {
|
|
List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
|
|
ProjectDVBuilder projectBuilder = ProjectDVBuilder.newBuilder().fullDocumentMap(true);
|
|
int i = 0;
|
|
for (Project project : listOfProjects) {
|
|
ProjectDV projectDV = ConvertToDataValueObjectModel.toProjectDV(project, projectBuilder);
|
|
System.out.println(++i + ") " + projectDV);
|
|
}
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void getResultDocumentForID() {
|
|
try {
|
|
ProjectsCaller projects = GeoportalClientCaller.projects();
|
|
Project project = projects.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
ResultDocumentDV documentDV = ConvertToDataValueObjectModel.toResultDocumentDV(project);
|
|
System.out.println("returning: " + documentDV);
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
//@Test
|
|
public void getListProjectsDVFiltered() throws Exception {
|
|
// List<Project> listOfProjects = client.getListForProfileID(PROFILE_ID);
|
|
|
|
|
|
SearchingFilter filter = new SearchingFilter();
|
|
|
|
// Where Clause
|
|
List<WhereClause> conditions = new ArrayList<WhereClause>();
|
|
Map<String, Object> searchInto = new HashMap<String, Object>();
|
|
// searchInto.put("_id", "61f0299baf51592c36795f52");
|
|
// searchInto.put("_theDocument.nome", "Test progetto con clustering di
|
|
// fileset");
|
|
// searchInto.put("_theDocument.paroleChiaveLibere", "scavo");
|
|
// searchInto.put("_theDocument.editore", "scavo");
|
|
// searchInto.put("_theDocument.paroleChiaveICCD", "scavo");
|
|
// searchInto.put("_theDocument.responsabile", "scavo");
|
|
|
|
searchInto.put("_theDocument.dataInizioProgetto", "2019");
|
|
WhereClause whereClause = new WhereClause(LOGICAL_OP.OR, searchInto);
|
|
|
|
conditions.add(whereClause);
|
|
filter.setConditions(conditions);
|
|
|
|
/* ORDER BY */
|
|
// List<ItemFieldDV> orderByFields = new ArrayList<ItemFieldDV>();
|
|
// List<String> jsonFields = Arrays.asList("_theDocument.dataInizioProgetto");
|
|
// ItemFieldDV itemField = new ItemFieldDV("dataInizioProgetto", jsonFields, "$or", false, false, false);
|
|
// orderByFields.add(itemField);
|
|
// filter.setOrderByFields(orderByFields);
|
|
|
|
LinkedHashMap<String, Object> projection = new LinkedHashMap<String, Object>();
|
|
// default
|
|
//PROJECTION
|
|
projection.put(Project.ID, 1);
|
|
// projection.put("_theDocument.nome", 1);
|
|
// projection.put("_profileID", 1);
|
|
// projection.put("_profileVersion", 1);
|
|
// projection.put("_version", 1);
|
|
// projection.put("_theDocument", 1);
|
|
//
|
|
// projection.put("_theDocument.paroleChiaveLibere", 1);
|
|
// projection.put("_theDocument.editore", 1);
|
|
// projection.put("_theDocument.paroleChiaveICCD", 1);
|
|
// projection.put("_theDocument.responsabile", 1);
|
|
//
|
|
// projection.put("_theDocument.introduzione", 1);
|
|
// projection.put("_theDocument.authors", 1);
|
|
// projection.put("_theDocument.dataInizioProgetto", 1);
|
|
|
|
filter.setProjection(projection);
|
|
|
|
Integer totalDocs = client.getTotalDocument(PROFILE_ID);
|
|
Iterator<Project> projects = client.queryOnMongo(PROFILE_ID, totalDocs, 0, null, filter);
|
|
Iterable<Project> itP = () -> projects;
|
|
Stream<Project> targetStream = StreamSupport.stream(itP.spliterator(), false);
|
|
List<String> listProjectIDs = targetStream.map(Project::getId).collect(Collectors.toList());
|
|
|
|
|
|
List<ResultDocumentDV> results = ConvertToDataValueObjectModel.toListResultDocument(projects);
|
|
int i = 0;
|
|
for (ResultDocumentDV projectDV : results) {
|
|
System.out.println(++i + ") " + projectDV.getId() + " dataInizioProgetto: "+projectDV.getDocumentAsMap().get("dataInizioProgetto"));
|
|
}
|
|
|
|
|
|
// TEST TO PROJECT DV
|
|
/*
|
|
* ProjectDVBuilder projectBuilder =
|
|
* ProjectDVBuilder.newBuilder().fullDocumentMap(true);
|
|
*
|
|
* List<ProjectDV> listProjects =
|
|
* ConvertToDataValueObjectModel.toListProject(projects, projectBuilder); i = 0;
|
|
* for (ProjectDV projectDV : listProjects) { System.out.println(++i + ") " +
|
|
* projectDV); }
|
|
*/
|
|
|
|
/*int limit = 2;
|
|
LinkedHashMap<String, Object> documentAsMap = new LinkedHashMap<String, Object>(limit);
|
|
|
|
try {
|
|
// Project theProject =
|
|
// GeoportalClientCaller.projects().getProjectByID(profileID, projectID);
|
|
|
|
QueryRequest request = new QueryRequest();
|
|
request.setFilter(Document.parse("{\"" + Project.ID + "\" : " + "{\"$eq\" : \"" + PROJECT_ID + "\"}}"));
|
|
request.setProjection(Document.parse("{\"" + Project.ID + "\" : " + "1}"));
|
|
// should be one
|
|
projects(PROFILE_ID).build().f
|
|
projects(PROFILE_ID).build().query(request).forEachRemaining(p -> {
|
|
try {
|
|
System.out.println("p is: "+p);
|
|
Iterator<Entry<String, Object>> entrySetsIt = p.getTheDocument().entrySet().iterator();
|
|
int i = 0;
|
|
while (entrySetsIt.hasNext()) {
|
|
if (i > limit)
|
|
break;
|
|
|
|
Entry<String, Object> entry = entrySetsIt.next();
|
|
documentAsMap.put(entry.getKey(), entry.getValue());
|
|
i++;
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
System.out.println("getEntrySetsDocumentForProjectID returning map: "+documentAsMap);
|
|
|
|
|
|
} catch (Exception e) {
|
|
String erroMsg = "Error occurred on loading EntrySets document for profileID " + PROFILE_ID
|
|
+ " and projectID " + PROJECT_ID;
|
|
e.printStackTrace();
|
|
}*/
|
|
}
|
|
|
|
// @Test
|
|
public void getLifecycleForProjectId() throws Exception {
|
|
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
LifecycleInformation lci = project.getLifecycleInformation();
|
|
LifecycleInformationDV liDV = ConvertToDataValueObjectModel.toLifecycleInformationDV(lci);
|
|
System.out.println(liDV);
|
|
//
|
|
}
|
|
|
|
// @Test
|
|
public void getConfiguration() throws Exception {
|
|
Configuration config = client.getConfiguration(PROFILE_ID);
|
|
System.out.println(config);
|
|
}
|
|
|
|
// @Test
|
|
public void getTotalDocument() throws Exception {
|
|
|
|
System.out.println(client.getTotalDocument(PROFILE_ID));
|
|
}
|
|
|
|
// @Test
|
|
public void getListPhases() throws Exception {
|
|
|
|
List<String> idsPhases = client.getIDsPhases(PROFILE_ID);
|
|
System.out.println(idsPhases);
|
|
|
|
PhaseDV[] phases = client.getPhasesIntoDocumentStoreCollection(PROFILE_ID);
|
|
for (PhaseDV phaseDV : phases) {
|
|
System.out.println(phaseDV);
|
|
}
|
|
|
|
}
|
|
|
|
//@Test
|
|
public void getRelationshipsChain() throws Exception {
|
|
System.out.println("getRelationshipsChain test");
|
|
Project project = client.getProjectByID(PROFILE_ID, PROJECT_ID);
|
|
|
|
List<Relationship> relations = project.getRelationships();
|
|
|
|
for (Relationship relationship : relations) {
|
|
System.out.println("\n\ngetRelationshipsChain for "+relationship);
|
|
Iterator<RelationshipNavigationObject> iterator = client.getRelationshipChain(PROFILE_ID, PROJECT_ID,
|
|
relationship.getRelationshipName(), true);
|
|
|
|
while (iterator.hasNext()) {
|
|
RelationshipNavigationObject nav = (RelationshipNavigationObject) iterator.next();
|
|
visitRelationshipsChain(nav, relationship.getRelationshipName());
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void visitRelationshipsChain(RelationshipNavigationObject nav, String relationshipName) {
|
|
System.out.println("visitRelationshipsChain of target: "+nav.getTarget().getId() +", relationshipName: "+relationshipName+", doc: "+nav.getTarget().getTheDocument());
|
|
System.out.println("visitRelationshipsChain children "+nav.getChildren());
|
|
|
|
if (nav == null)
|
|
return;
|
|
|
|
printObject(nav.getTarget());
|
|
|
|
if (nav.getChildren() != null) {
|
|
|
|
for (RelationshipNavigationObject relationship : nav.getChildren()) {
|
|
visitRelationshipsChain(relationship, relationshipName);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void printObject(Project project) {
|
|
|
|
System.out.println("Project id: " + project.getId() + ", profile: " + project.getProfileID() + ", doc: "
|
|
+ project.getTheDocument().toJson());
|
|
}
|
|
|
|
// @Test
|
|
public void deserializeISOJSONObjectAsDate() throws Exception {
|
|
|
|
String jsonDate = "{hour=0, minute=0, " + "dayOfYear=259, " + "dayOfWeek=MONDAY, " + "dayOfMonth=16, "
|
|
+ "monthValue=9, " + "year=2019, " + "month=SEPTEMBER, " + "second=0, " + "nano=0, "
|
|
+ "chronology={calendarType=iso8601, id=ISO}" + "}";
|
|
|
|
try {
|
|
|
|
/*
|
|
* ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule
|
|
* = new JavaTimeModule(); javaTimeModule.addDeserializer(LocalDate.class, new
|
|
* MyLocalDateDeserializer()); javaTimeModule.addSerializer(LocalDate.class, new
|
|
* MyLocalDateSerializer()); objectMapper.registerModule(javaTimeModule);
|
|
*
|
|
* objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
* objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
|
|
* false); objectMapper.configure(SerializationFeature.
|
|
* WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); LocalDate date =
|
|
* objectMapper.readerFor(LocalDate.class).readValue(jsonDate);
|
|
* System.out.println(date);
|
|
*/
|
|
|
|
// checking if the jsonDate is a LocalDate
|
|
// LocalDateTime date =
|
|
// org.gcube.application.geoportal.client.utils.Serialization.read(jsonDate,
|
|
// LocalDateTime.class);
|
|
LocalDate date = org.gcube.application.geoportal.client.utils.Serialization.read(jsonDate, LocalDate.class);
|
|
System.out.println(date);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static final DateTimeFormatter FULL_FORMATTER = DateTimeFormatter.ofPattern("uuuuMMdd_HH-mm-ss");
|
|
|
|
public static class MyLocalDateSerializer extends JsonSerializer<LocalDate> {
|
|
@Override
|
|
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
gen.writeString(value.format(FULL_FORMATTER));
|
|
}
|
|
}
|
|
|
|
public static class MyLocalDateDeserializer extends JsonDeserializer<LocalDate> {
|
|
|
|
@Override
|
|
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
return LocalDate.parse(p.getValueAsString(), FULL_FORMATTER);
|
|
}
|
|
}
|
|
}
|