diff --git a/entity-management-common/src/main/java/eu/europeana/entitymanagement/common/config/DataSource.java b/entity-management-common/src/main/java/eu/europeana/entitymanagement/common/config/DataSource.java index bb859e6b..776cfb7d 100644 --- a/entity-management-common/src/main/java/eu/europeana/entitymanagement/common/config/DataSource.java +++ b/entity-management-common/src/main/java/eu/europeana/entitymanagement/common/config/DataSource.java @@ -9,7 +9,7 @@ public class DataSource { public static final String FREQ_STATIC = "static"; public static final String EUROPEANA_ID = "europeana"; public static final String ZOHO_ID = "zoho-crm"; - public static final String ZOHO_HOST = "crm.zoho.com"; + public static final String ZOHO_HOST = "crm.zoho.eu"; @JacksonXmlProperty(isAttribute = true) private String url; diff --git a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Entity.java b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Entity.java index c9db854a..79177bba 100644 --- a/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Entity.java +++ b/entity-management-definitions/src/main/java/eu/europeana/entitymanagement/definitions/model/Entity.java @@ -239,6 +239,10 @@ public Object getFieldValue(Field field) throws IllegalAccessException { public void setFieldValue(Field field, Object value) throws IllegalAccessException { + if(TYPE.equals(field.getName())) { + //type is immutable must not be overwritten + return; + } field.set(this, value); } diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/service/DereferenceServiceIT.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/service/DereferenceServiceIT.java index 1582c9d6..cc363b6f 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/service/DereferenceServiceIT.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/service/DereferenceServiceIT.java @@ -28,7 +28,7 @@ // enable test config to use zoho mocking @Import(TestConfig.class) // enable tests only on local machine -// @Disabled +//@Disabled @SpringBootTest public class DereferenceServiceIT extends AbstractIntegrationTest { @@ -65,7 +65,7 @@ public void dereferenceConceptById() throws Exception { assertEquals(8, entity.getNote().size()); } - // @Test + @Test public void zohoOrganizationDereferenceTest() throws Exception { String organizationId = IntegrationTestUtils.ORGANIZATION_BNF_URI_ZOHO; Dereferencer dereferencer = diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java index 11c682fc..9ed6e400 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/IntegrationTestUtils.java @@ -169,7 +169,7 @@ public class IntegrationTestUtils { public static final String INVALID_MIGRATION_ID = "http://www.testing.org/entity/testing"; public static final String ORGANIZATION_BNF_URI_ZOHO = - "https://crm.zoho.com/crm/org51823723/tab/Accounts/1482250000002112001"; + "https://crm.zoho.eu/crm/org20085137532/tab/Accounts/486281000000938399"; public static final String ORGANIZATION_NATURALIS_URI_ZOHO = "https://crm.zoho.com/crm/org51823723/tab/Accounts/1482250000000370517"; public static final String ORGANIZATION_PCCE_URI_ZOHO = diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/TestConfig.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/TestConfig.java index 145a45df..4de3f5e6 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/TestConfig.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/TestConfig.java @@ -4,6 +4,7 @@ import org.mockito.ArgumentMatchers; import org.mockito.Mockito; import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; @@ -18,6 +19,9 @@ public class TestConfig { public static final String MOCK_ZOHO_BASE_URL = "https://crm.zoho.com/crm/org51823723/tab/Accounts/"; public static final String MOCK_ZOHO_COUNTRY_MAPPING_FILE = "/zoho_country_mapping_test.json"; + @Autowired + ZohoConfiguration zohoConfiguration; + /** * Since requests to Zoho are done via its SDK, and require authentication first, we mock out the * entire flow with Mockito. @@ -28,7 +32,7 @@ public ZohoConfiguration configureZoho() throws Exception { ZohoConfiguration zohoConfiguration = Mockito.mock(ZohoConfiguration.class); ZohoAccessClient zohoClient = Mockito.mock(ZohoAccessClient.class); Mockito.when(zohoConfiguration.getZohoAccessClient()).thenReturn(zohoClient); - Mockito.when(zohoConfiguration.getZohoBaseUrl()).thenReturn(MOCK_ZOHO_BASE_URL); + Mockito.when(zohoConfiguration.getZohoBaseUrl()).thenReturn(zohoConfiguration.getZohoBaseUrl()); // find matching JSON file based on zohoId argument, then create a Record object for it Mockito.doAnswer( diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/ZohoRecordTestDeserializer.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/ZohoRecordTestDeserializer.java index ff901829..d5c0975f 100644 --- a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/ZohoRecordTestDeserializer.java +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/testutils/ZohoRecordTestDeserializer.java @@ -99,7 +99,13 @@ public Record deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx List> values = new ArrayList>(); currentNode.elements().forEachRemaining(v -> values.add(new Choice(v.asText()))); record.addKeyValue(key, values); - } + }else if (currentNode.isContainerNode()){ + System.out.println("container node: " + key); + }else if(currentNode.isPojo()) { + System.out.println("pojo node: " + key); + }else if(currentNode.isObject()) { + System.out.println("object node: " + key); + } } // add fields with numeric suffixes diff --git a/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/zoho/organization/ZohoTestDataGenerator.java b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/zoho/organization/ZohoTestDataGenerator.java new file mode 100644 index 00000000..cbccaa05 --- /dev/null +++ b/entity-management-tests/src/integration-test/java/eu/europeana/entitymanagement/zoho/organization/ZohoTestDataGenerator.java @@ -0,0 +1,59 @@ +package eu.europeana.entitymanagement.zoho.organization; + +import static org.junit.Assert.assertNotNull; +import java.util.Optional; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.zoho.crm.api.record.Record; +import eu.europeana.entitymanagement.common.config.EntityManagementConfiguration; +import eu.europeana.entitymanagement.common.vocabulary.AppConfigConstants; +import eu.europeana.entitymanagement.config.SerializationConfig; +import eu.europeana.entitymanagement.testutils.IntegrationTestUtils; +import eu.europeana.entitymanagement.testutils.ZohoRecordTestDeserializer; + +/** JUnit test to check if DataSources are properly deserialized from XML */ +@SpringBootTest(classes = {SerializationConfig.class, ZohoConfiguration.class, EntityManagementConfiguration.class}) +public class ZohoTestDataGenerator { + + @Autowired + @Qualifier(AppConfigConstants.BEAN_JSON_MAPPER) + private ObjectMapper jsonMapper; + + @Autowired + ZohoConfiguration zohoConfiguration; + EntityManagementConfiguration emConfig; + + @Bean + ZohoDereferenceService getZohoDereferenceService() { + return new ZohoDereferenceService(zohoConfiguration, emConfig); + } + +// @Test + //manually enable the test when data needs to be generated + public void generateBNFJson() throws Exception { + //get original zoho record + Optional zohoOrganization = + zohoConfiguration.getZohoAccessClient().getZohoRecordOrganizationById(IntegrationTestUtils.ORGANIZATION_BNF_URI_ZOHO); + String zohoRecord = getZohoDereferenceService().serialize(zohoOrganization.get()); + //if you need to see original enable : System.out.println(zohoRecord); + System.out.println(zohoRecord); + + //deserialize data with the test deserializer + ZohoRecordTestDeserializer zohoRecordDeserializer = new ZohoRecordTestDeserializer(); + //TODO: many fields are missing or need to be renamed in the deserializer, need to include all fields from ZohoMaping except for the sensitive ones (like email addresses) + Record zohoTestRecord = zohoRecordDeserializer.deserialize(jsonMapper.getFactory().createParser(zohoRecord), jsonMapper.getDeserializationContext()); + + //serialize test data + String zohoTestRecordJson = getZohoDereferenceService().serialize(zohoTestRecord); + System.out.println(zohoTestRecordJson); + + Record zohoTestRecordFiltered = zohoRecordDeserializer.deserialize(jsonMapper.getFactory().createParser(zohoTestRecordJson), jsonMapper.getDeserializationContext()); + assertNotNull(zohoTestRecordFiltered.getId()); + assertNotNull(zohoTestRecordFiltered.getKeyValue("Account_Name")); + + } +} \ No newline at end of file diff --git a/entity-management-web/.gitignore b/entity-management-web/.gitignore index 154abc92..658853ec 100644 --- a/entity-management-web/.gitignore +++ b/entity-management-web/.gitignore @@ -1,2 +1,3 @@ /SDKLogs.log* **/log4j2.xml +/sdk_tokens.txt diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java index 5b53035b..b1c87586 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/EMController.java @@ -72,6 +72,7 @@ import eu.europeana.entitymanagement.vocabulary.WebEntityConstants; import eu.europeana.entitymanagement.web.service.DereferenceServiceLocator; import eu.europeana.entitymanagement.web.service.EntityRecordService; +import eu.europeana.entitymanagement.zoho.utils.ZohoUtils; import io.swagger.annotations.ApiOperation; @RestController @@ -521,8 +522,7 @@ public ResponseEntity registerEntity(@RequestBody Entity europeanaProxyE // in case of Organization it must be the zoho Organization String creationRequestType = europeanaProxyEntity.getType(); - if (EntityTypes.isOrganization(creationRequestType) - && !creationRequestId.contains(DataSource.ZOHO_HOST)) { + if (!ZohoUtils.isZohoOrganization(creationRequestId, creationRequestType)) { throw new HttpBadRequestException(String.format( "The Organization entity should come from Zoho and have the corresponding id format containing: %s", DataSource.ZOHO_HOST)); diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/DereferenceServiceLocator.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/DereferenceServiceLocator.java index 0a310620..08689236 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/DereferenceServiceLocator.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/DereferenceServiceLocator.java @@ -35,6 +35,8 @@ public Dereferencer getDereferencer(String id, String entityType) { if (ZohoUtils.isZohoOrganization(id, entityType)) { return zohoDereferenceService; } + + System.out.println(); return metisDereferenceService; } diff --git a/entity-management-web/src/main/resources/datasources.xml b/entity-management-web/src/main/resources/datasources.xml index c100b6c1..5df2515b 100644 --- a/entity-management-web/src/main/resources/datasources.xml +++ b/entity-management-web/src/main/resources/datasources.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/entity-management-zoho/pom.xml b/entity-management-zoho/pom.xml index 08058a1b..68c211e0 100644 --- a/entity-management-zoho/pom.xml +++ b/entity-management-zoho/pom.xml @@ -23,6 +23,8 @@ com.zoho.crm java-sdk ${zoho-sdk.version} + + org.springframework diff --git a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java index f45d7a1a..c5291e9a 100644 --- a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java +++ b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/ZohoAccessClient.java @@ -23,6 +23,7 @@ import com.zoho.crm.api.SDKConfig; import com.zoho.crm.api.UserSignature; import com.zoho.crm.api.dc.DataCenter.Environment; +import com.zoho.crm.api.dc.EUDataCenter; import com.zoho.crm.api.dc.USDataCenter; import com.zoho.crm.api.exception.SDKException; import com.zoho.crm.api.record.APIException; @@ -84,7 +85,8 @@ public ZohoAccessClient( new OAuthToken(clientId, clientSecret, refreshToken, TokenType.REFRESH, redirectUrl); SDKConfig sdkConfig = new SDKConfig.Builder().setAutoRefreshFields(false).setPickListValidation(true).build(); - Environment environment = USDataCenter.PRODUCTION; + //Environment environment = USDataCenter.PRODUCTION; + Environment environment = EUDataCenter.PRODUCTION; String resourcePath = SystemUtils.getUserHome().getAbsolutePath(); // Does not generate any tokens, we'll need to execute a command to do so Initializer.initialize( diff --git a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java index 122dd9d9..9cb680f0 100644 --- a/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java +++ b/entity-management-zoho/src/main/java/eu/europeana/entitymanagement/zoho/organization/ZohoDereferenceService.java @@ -1,7 +1,6 @@ package eu.europeana.entitymanagement.zoho.organization; import java.text.SimpleDateFormat; -import java.util.Map; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; @@ -9,11 +8,12 @@ import org.springframework.stereotype.Service; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; import com.zoho.crm.api.record.Record; import eu.europeana.entitymanagement.common.config.EntityManagementConfiguration; -import eu.europeana.entitymanagement.definitions.model.CountryMapping; import eu.europeana.entitymanagement.definitions.model.Entity; import eu.europeana.entitymanagement.dereference.Dereferencer; @@ -34,8 +34,9 @@ public Optional dereferenceEntityById(@NonNull String id) throws Excepti Optional zohoOrganization = zohoConfiguration.getZohoAccessClient().getZohoRecordOrganizationById(id); - // Gson resp = new Gson(); - // System.out.println(resp.toJson(zohoOrganization.get().getKeyValues())); + + + System.out.println(serialize(zohoOrganization.get())); if(zohoOrganization.isPresent()) { return Optional.of( @@ -57,7 +58,7 @@ public String serialize(Record zohoRecord) throws JsonProcessingException { .serializationInclusion(JsonInclude.Include.NON_NULL) .build(); mapper.findAndRegisterModules(); - return mapper.writeValueAsString(zohoRecord.getKeyValues()); + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(zohoRecord.getKeyValues()); } }