Copied: manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/NuxeoDocumentHelper.java (from r1828419, manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/DocumentManifold.java) URL: http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/NuxeoDocumentHelper.java?p2=manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/NuxeoDocumentHelper.java&p1=manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/DocumentManifold.java&r1=1828419&r2=1828420&rev=1828420&view=diff ============================================================================== --- manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/DocumentManifold.java (original) +++ manifoldcf/trunk/connectors/nuxeo/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/nuxeo/model/NuxeoDocumentHelper.java Thu Apr 5 11:28:22 2018 @@ -1,31 +1,29 @@ /** -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You under the Apache License, Version 2.0 -* (the "License"); you may not use this file except in compliance with -* the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.manifoldcf.crawler.connectors.nuxeo.model; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import org.nuxeo.client.NuxeoClient; import org.nuxeo.client.objects.Document; import org.nuxeo.client.objects.Documents; @@ -36,163 +34,218 @@ import org.nuxeo.client.objects.blob.Blo import com.google.common.collect.Maps; -public class DocumentManifold { +public class NuxeoDocumentHelper { + + private static final String URI_TAGGING = "SELECT * FROM Tagging"; + + private static final String DEFAULT_MIMETYPE = "text/html; charset=utf-8"; + private static final Set<String> AVOID_PROPERTIES = ImmutableSet.of("file:filename", + "thumb:thumbnail", "file:content", "files:files" ); + + private Document document; + private InputStream content; + private int size; + private String mimetype; + private String filename; + private Map<String, Object> properties = null; + + public static final String DELETED = "deleted"; + + private static final String DOC_UID = "uid"; + private static final String DOC_ENTITY_TYPE = "entity-type"; + private static final String DOC_LAST_MODIFIED = "last-modified"; + private static final String DOC_STATE = "state"; + private static final String NUXEO_MAJOR_VERSION_PROPERTY = "uid:major_version"; + private static final String NUXEO_MINOR_VERSION_PROPERTY = "uid:minor_version"; + private static final Set<String> NUXEO_FOLDER_TYPE = ImmutableSet.of("Folderish", "Collection"); + private static final String NUXEO_TAGS_PROPERTY = "nxtag:tags"; + + + // Constructor + public NuxeoDocumentHelper(Document document) { + this.document = document; + processDocument(); + } + + /** + * + * + * @return Map<String, Object> + */ + public Map<String, Object> getMetadata() { + Map<String, Object> docMetadata = Maps.newHashMap(); + + for (Entry<String, Object> property : this.getProperties().entrySet()) { + if (!AVOID_PROPERTIES.contains(property.getKey())) { + try { + if (property.getKey().equalsIgnoreCase(NUXEO_TAGS_PROPERTY)){ + List<Map> tags = (List) property.getValue(); + List<String> tagsLabels = Lists.newArrayList(); + for (Map tag: tags){ + if (tag.containsKey("label")) { + tagsLabels.add((String) tag.get("label")); + break; + } + } + if (!tagsLabels.isEmpty()) + docMetadata.put("tags", tagsLabels); + }else { + addIfNotEmpty(docMetadata, property.getKey(), property.getValue()); + } + } catch (Exception e){ + continue; + } + } + } + addIfNotEmpty(docMetadata, DOC_UID, this.document.getUid()); + addIfNotEmpty(docMetadata, DOC_ENTITY_TYPE, this.document.getEntityType()); + addIfNotEmpty(docMetadata, DOC_LAST_MODIFIED, this.document.getLastModified()); + addIfNotEmpty(docMetadata, DOC_STATE, this.document.getState()); + + return docMetadata; + } + + public boolean isFolder(){ + return !Collections.disjoint(document.getFacets(), NUXEO_FOLDER_TYPE); + } + + private void addIfNotEmpty(Map<String, Object> docMetadata, String key, Object obj) { + if (obj != null && ((obj instanceof String && !((String) obj).isEmpty()) || !(obj instanceof String))) { + docMetadata.put(key, obj); + } + } + + private void processDocument() { + try { + Blob blob = document.fetchBlob(); + this.content = blob.getStream(); + this.mimetype = blob.getMimeType(); + this.size = blob.getLength(); + this.filename = blob.getFilename(); + } catch (Exception ex) { + this.content = new ByteArrayInputStream("".getBytes()); + this.mimetype = DEFAULT_MIMETYPE; + this.size = 0; + this.filename = document.getTitle(); + } + } + + // GETTERS AND SETERS + public Document getDocument() { + return this.document; + } + + public String getMimeType() { + return this.mimetype; + } + + public int getLength() { + return this.size; + } + + public InputStream getContent() { + return this.content; + } + + public String getFilename(){ + return filename; + } + + private Map<String, Object> getProperties(){ + if (this.properties == null) + this.properties = document.getProperties(); + + return this.properties; + } + + public String getVersion(){ + StringBuilder builder = new StringBuilder(); + builder.append(this.document.getUid()); + builder.append('_'); + if (this.getProperties().containsKey(NUXEO_MAJOR_VERSION_PROPERTY) + && this.getProperties().containsKey(NUXEO_MINOR_VERSION_PROPERTY)){ + builder.append(this.getProperties().get(NUXEO_MAJOR_VERSION_PROPERTY)); + builder.append('.'); + builder.append(this.getProperties().get(NUXEO_MINOR_VERSION_PROPERTY)); + }else{ + builder.append("0.0"); + } + + return builder.toString(); + } + + public String[] getPermissions() { + + List<String> permissions = new ArrayList<>(); + try { + for (ACL acl : this.getDocument().fetchPermissions().getAcls()) { + for (ACE ace : acl.getAces()) { + if (ace.getStatus().equalsIgnoreCase("effective") && ace.getGranted().equalsIgnoreCase("true")) { + permissions.add(ace.getUsername()); + } + } + } + + return permissions.toArray(new String[permissions.size()]); + } catch (Exception e) { + return new String[] {}; + } + } - private static final String URI_TAGGING = "SELECT * FROM Tagging"; + public List<NuxeoAttachment> getAttachments(NuxeoClient nuxeoClient) { + List<NuxeoAttachment> attachments = new ArrayList<>(); + List<?> arrayList = this.document.getPropertyValue(NuxeoAttachment.ATT_KEY_FILES); + + for (Object object : arrayList) { + NuxeoAttachment attach = new NuxeoAttachment(); + LinkedHashMap<?, ?> file = (LinkedHashMap<?, ?>) ((LinkedHashMap<?, ?>) object) + .get(NuxeoAttachment.ATT_KEY_FILE); + + attach.name = (String) file.get("name"); + attach.encoding = (String) file.get("encoding"); + attach.mime_type = (String) file.get("mime-type"); + attach.digestAlgorithm = (String) file.get("digestAlgorithm"); + attach.digest = (String) file.get("digest"); + attach.length = Long.valueOf((String) file.get("length")); + attach.url = (String) file.get("data"); + + try { + Blob blob = nuxeoClient.repository().fetchBlobById(this.document.getUid(), + getAttachPath(attach.url)); + + attach.data = blob.getStream(); + + } catch (Exception ex) { + attach.data = new ByteArrayInputStream("".getBytes()); + } - private static final String DEFAULT_MIMETYPE = "text/html; charset=utf-8"; - private static final String[] avoid_properties = { "file:filename", "file:content", "files:files" }; + attachments.add(attach); - private Document document; - private InputStream content; - private String mimetype; - - public static final String DELETED = "deleted"; - - private static final String DOC_UID = "uid"; - private static final String DOC_ENTITY_TYPE = "entity-type"; - private static final String DOC_LAST_MODIFIED = "last-modified"; - private static final String DOC_STATE = "state"; - - // Constructor - public DocumentManifold(Document document) { - this.document = document; - processDocument(); - } - - - /** - * - * - * @return Map<String, Object> - */ - public Map<String, Object> getMetadata() { - Map<String, Object> docMetadata = Maps.newHashMap(); - - for (Entry<String, Object> property : this.document.getProperties().entrySet()) { - if (!Arrays.asList(avoid_properties).contains(property.getKey())) { - addIfNotEmpty(docMetadata, property.getKey(), property.getValue()); - } - } - addIfNotEmpty(docMetadata, DOC_UID, this.document.getUid()); - addIfNotEmpty(docMetadata, DOC_ENTITY_TYPE, this.document.getEntityType()); - addIfNotEmpty(docMetadata, DOC_LAST_MODIFIED, this.document.getLastModified()); - addIfNotEmpty(docMetadata, DOC_STATE, this.document.getState()); - - return docMetadata; - } - - private void addIfNotEmpty(Map<String, Object> docMetadata, String key, Object obj) { - if (obj != null && ((obj instanceof String && !((String) obj).isEmpty()) || !(obj instanceof String))) { - docMetadata.put(key, obj); - } - } - - private void processDocument() { - try { - this.content = document.fetchBlob().getStream(); - this.mimetype = (String) ((Map) this.document.getPropertyValue("file:content")).get("mime-type"); - } catch (Exception ex) { - this.content = new ByteArrayInputStream("".getBytes()); - this.mimetype = DEFAULT_MIMETYPE; - } - } - - // GETTERS AND SETERS - public Document getDocument() { - return this.document; - } - - public String getMimeType() { - return this.mimetype; - } - - public int getLength() { - int size; - try { - size = this.getContent().available(); - } catch (IOException ex) { - size = 0; - } - return size; - } - - public InputStream getContent() { - return this.content; - } - - public String[] getPermissions() { - - List<String> permissions = new ArrayList<>(); - try { - for (ACL acl : this.getDocument().fetchPermissions().getAcls()) { - for (ACE ace : acl.getAces()) { - if (ace.getStatus().equalsIgnoreCase("effective") && ace.getGranted().equalsIgnoreCase("true")) { - permissions.add(ace.getUsername()); - } - } - } - - return permissions.toArray(new String[permissions.size()]); - } catch (Exception e) { - return new String[] {}; - } - } - - public List<Attachment> getAttachments(NuxeoClient nuxeoClient) { - List<Attachment> attachments = new ArrayList<>(); - List<?> arrayList = this.document.getPropertyValue(Attachment.ATT_KEY_FILES); - - for (Object object : arrayList) { - Attachment attach = new Attachment(); - LinkedHashMap<?, ?> file = (LinkedHashMap<?, ?>) ((LinkedHashMap<?, ?>) object) - .get(Attachment.ATT_KEY_FILE); - - attach.name = (String) file.get("name"); - attach.encoding = (String) file.get("encoding"); - attach.mime_type = (String) file.get("mime-type"); - attach.digestAlgorithm = (String) file.get("digestAlgorithm"); - attach.digest = (String) file.get("digest"); - attach.length = Long.valueOf((String) file.get("length")); - attach.url = (String) file.get("data"); - - try { - Blob blob = nuxeoClient.repository().fetchBlobById(this.document.getUid(), - getAttachPath(attach.url)); - - attach.data = blob.getStream(); - - } catch (Exception ex) { - attach.data = new ByteArrayInputStream("".getBytes()); - } - - attachments.add(attach); - - } - - return attachments; - } - - private String getAttachPath(String absolutePath) { - String[] splitPath = absolutePath.split("/"); - int size = splitPath.length; - return String.join("/", splitPath[size - 4], splitPath[size - 3], splitPath[size - 2]); - } - - public String[] getTags(NuxeoClient nuxeoClient) { - try { - Operation op = nuxeoClient.operation("Repository.Query").param("query", - URI_TAGGING + " where relation:source='" + this.document.getUid() + "'"); - Documents tags = op.execute(); - List<String> ls = new ArrayList<>(); - - for (Document tag : tags.getDocuments()) { - ls.add(tag.getTitle()); - } - return ls.toArray(new String[tags.size()]); - } catch (Exception e) { - return new String[] {}; + } + + return attachments; + } + + private String getAttachPath(String absolutePath) { + String[] splitPath = absolutePath.split("/"); + int size = splitPath.length; + return String.join("/", splitPath[size - 4], splitPath[size - 3], splitPath[size - 2]); + } + + public String[] getTags(NuxeoClient nuxeoClient) { + try { + Operation op = nuxeoClient.operation("Repository.Query").param("query", + URI_TAGGING + " where relation:source='" + this.document.getUid() + "'"); + Documents tags = op.execute(); + List<String> ls = new ArrayList<>(); + + for (Document tag : tags.getDocuments()) { + ls.add(tag.getTitle()); + } + return ls.toArray(new String[tags.size()]); + } catch (Exception e) { + return new String[] {}; + } } - } }
Modified: manifoldcf/trunk/connectors/nuxeo/connector/src/test/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoConnectorTest.java URL: http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/nuxeo/connector/src/test/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoConnectorTest.java?rev=1828420&r1=1828419&r2=1828420&view=diff ============================================================================== --- manifoldcf/trunk/connectors/nuxeo/connector/src/test/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoConnectorTest.java (original) +++ manifoldcf/trunk/connectors/nuxeo/connector/src/test/java/org/apache/manifoldcf/crawler/connectors/nuxeo/NuxeoConnectorTest.java Thu Apr 5 11:28:22 2018 @@ -1,31 +1,30 @@ /** -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You under the Apache License, Version 2.0 -* (the "License"); you may not use this file except in compliance with -* the License. You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.manifoldcf.crawler.connectors.nuxeo; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -35,7 +34,7 @@ import java.util.List; import org.apache.manifoldcf.agents.interfaces.RepositoryDocument; import org.apache.manifoldcf.core.interfaces.Specification; import org.apache.manifoldcf.crawler.connectors.BaseRepositoryConnector; -import org.apache.manifoldcf.crawler.connectors.nuxeo.model.DocumentManifold; +import org.apache.manifoldcf.crawler.connectors.nuxeo.model.NuxeoDocumentHelper; import org.apache.manifoldcf.crawler.interfaces.IExistingVersions; import org.apache.manifoldcf.crawler.interfaces.IProcessActivity; import org.apache.manifoldcf.crawler.system.SeedingActivity; @@ -50,6 +49,7 @@ import org.nuxeo.client.NuxeoClient; import org.nuxeo.client.objects.Document; import org.nuxeo.client.objects.Documents; import org.nuxeo.client.objects.Repository; +import org.nuxeo.client.objects.blob.FileBlob; /** * @author David Arroyo Escobar <[email protected]> @@ -58,170 +58,197 @@ import org.nuxeo.client.objects.Reposito @RunWith(MockitoJUnitRunner.class) public class NuxeoConnectorTest { - @Mock - private NuxeoClient client; + @Mock + private NuxeoClient client; - private NuxeoRepositoryConnector repositoryConnector; + private NuxeoRepositoryConnector repositoryConnector; - @Before - public void setup(){ + @Before + public void setup(){ - repositoryConnector = new NuxeoRepositoryConnector(); - repositoryConnector.setNuxeoClient(client); - } - - @Test - public void checkMockInjection() throws Exception { - Document doc = mock(Document.class); - Repository repository = mock(Repository.class); - - when(client.repository()).thenReturn(repository); - when(client.repository().fetchDocumentRoot()).thenReturn(doc); - assertEquals(repositoryConnector.check(), "Connection working"); - } - - @Test - public void mockSeeding() throws Exception { - SeedingActivity activities = mock(SeedingActivity.class); - Specification spec = new Specification(); - Repository repository = mock(Repository.class); - Document document = mock(Document.class); - List<Document> documents = new ArrayList<>(); - documents.add(document); - documents.add(document); - Documents docs = mock(Documents.class); - long seedTime = 0; - - - when(client.repository()).thenReturn(repository); - when(client.repository().query(anyString(), anyString(), anyString(), anyString(), anyString(), - anyString(), anyString())).thenReturn(docs); - when(docs.isNextPageAvailable()).thenReturn(false); - when(docs.getDocuments()).thenReturn(documents); - - repositoryConnector.addSeedDocuments(activities, spec, "", seedTime, - BaseRepositoryConnector.JOBMODE_CONTINUOUS); - - verify(activities, times(2)).addSeedDocument(anyString()); - - } - - @Test - public void mockEmptySeeding() throws Exception { - SeedingActivity activities = mock(SeedingActivity.class); - Specification spec = new Specification(); - Repository repository = mock(Repository.class); - List<Document> documents = new ArrayList<>(); - Documents docs = mock(Documents.class); - long seedTime = 0; - - - when(client.repository()).thenReturn(repository); - when(client.repository().query(anyString(), anyString(), anyString(), anyString(), anyString(), - anyString(), anyString())).thenReturn(docs); - when(docs.isNextPageAvailable()).thenReturn(false); - when(docs.getDocuments()).thenReturn(documents); - - repositoryConnector.addSeedDocuments(activities, spec, "", seedTime, - BaseRepositoryConnector.JOBMODE_CONTINUOUS); - - verify(activities, times(0)).addSeedDocument(anyString()); - } - - @Test - public void mockDeleteDocument() throws Exception { - DocumentManifold docMa = mock(DocumentManifold.class); - Repository repository = mock(Repository.class); - Document doc = mock(Document.class); - Specification spec = new Specification(); - IProcessActivity activities = mock(IProcessActivity.class); - IExistingVersions statuses = mock(IExistingVersions.class); - - when(client.repository()).thenReturn(repository); - when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); - when(docMa.getDocument()).thenReturn(doc); - when(doc.getState()).thenReturn("deleted"); - - repositoryConnector.processDocuments(new String[] { anyString() }, statuses, spec, activities, - BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); - - verify(activities, times(1)).deleteDocument(anyString()); - } - - @Test - public void mockSimpleIngestion() throws Exception { - - Document doc = mock(Document.class); - DocumentManifold docMa = mock(DocumentManifold.class); - Date date = new Date(); - DateFormat df = DateFormat.getDateTimeInstance(); - String version = df.format(date); - Long size = 0L; - String id; - String uri = "http://localhost:8080/nuxeo/site/api/v1/id/7995ff6d-1eda-41db-b9de-3ea4037fdb81"; - Map<String, Object> metadata = new HashMap<>(); - IProcessActivity activities = mock(IProcessActivity.class); - IExistingVersions statuses = mock(IExistingVersions.class); - Specification spec = new Specification(); - Repository repository = mock(Repository.class); - - - metadata.put("key", "value"); - id = df.format(date); - - when(doc.getPropertyValue("lenght")).thenReturn(size); - when(doc.getLastModified()).thenReturn(version); - when(docMa.getMetadata()).thenReturn(metadata); - when(doc.getUid()).thenReturn(uri); - when(client.repository()).thenReturn(repository); - when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); - - when(activities.checkDocumentNeedsReindexing(anyString(), anyString())).thenReturn(true); - when(statuses.getIndexedVersionString(id)).thenReturn(null); - - - repositoryConnector.processDocuments(new String[] { id }, statuses, spec, activities, - BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); - ArgumentCaptor<RepositoryDocument> ac = ArgumentCaptor.forClass(RepositoryDocument.class); - - verify(activities, times(1)).ingestDocumentWithException(eq(id), eq(df.format(date)), eq(uri), ac.capture()); - verify(activities, times(1)).recordActivity(anyLong(), eq("read document"), eq(size), eq(id), eq("OK"), - anyString(), Mockito.isNull(String[].class)); - - RepositoryDocument rd = ac.getValue(); - Long rdSize = rd.getBinaryLength(); - - String[] keyValue = rd.getFieldAsStrings("uid"); - - assertEquals(size, rdSize); - assertEquals(keyValue.length, 1); - } - - @Test - public void mockNotNeedReindexing() throws Exception { - Document doc = mock(Document.class); - Date date = new Date(); - Repository repository = mock(Repository.class); - DateFormat df = DateFormat.getDateTimeInstance(); - String version = df.format(date); - String uid = "297529bf-191a-4c87-8259-28b692394229"; - - IProcessActivity activities = mock(IProcessActivity.class); - IExistingVersions statuses = mock(IExistingVersions.class); - Specification spec = new Specification(); - - when(doc.getLastModified()).thenReturn(version); - when(statuses.getIndexedVersionString(uid)).thenReturn(version); - when(client.repository()).thenReturn(repository); - when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); - - repositoryConnector.processDocuments(new String[] { uid }, statuses, spec, activities, - BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); - ArgumentCaptor<RepositoryDocument> ac = ArgumentCaptor.forClass(RepositoryDocument.class); + repositoryConnector = new NuxeoRepositoryConnector(); + repositoryConnector.setNuxeoClient(client); + repositoryConnector.protocol = "http"; + repositoryConnector.host = "localhost"; + repositoryConnector.port = "8080"; + repositoryConnector.path = "/nuxeo"; + } + + @Test + public void checkMockInjection() throws Exception { + Document doc = mock(Document.class); + Repository repository = mock(Repository.class); + + when(client.repository()).thenReturn(repository); + when(client.repository().fetchDocumentRoot()).thenReturn(doc); + assertEquals(repositoryConnector.check(), "Connection working"); + } + + @Test + public void mockSeeding() throws Exception { + SeedingActivity activities = mock(SeedingActivity.class); + Specification spec = new Specification(); + Repository repository = mock(Repository.class); + Document document = mock(Document.class); + List<Document> documents = new ArrayList<>(); + documents.add(document); + documents.add(document); + Documents docs = mock(Documents.class); + long seedTime = 0; + + + when(client.repository()).thenReturn(repository); + when(client.repository().query(anyString(), anyString(), anyString(), anyString(), anyString(), + anyString(), anyString())).thenReturn(docs); + when(docs.isNextPageAvailable()).thenReturn(false); + when(docs.getDocuments()).thenReturn(documents); + + repositoryConnector.addSeedDocuments(activities, spec, "", seedTime, + BaseRepositoryConnector.JOBMODE_CONTINUOUS); + + verify(activities, times(2)).addSeedDocument(anyString()); + + } + + @Test + public void mockEmptySeeding() throws Exception { + SeedingActivity activities = mock(SeedingActivity.class); + Specification spec = new Specification(); + Repository repository = mock(Repository.class); + List<Document> documents = new ArrayList<>(); + Documents docs = mock(Documents.class); + long seedTime = 0; + + + when(client.repository()).thenReturn(repository); + when(client.repository().query(anyString(), anyString(), anyString(), anyString(), anyString(), + anyString(), anyString())).thenReturn(docs); + when(docs.isNextPageAvailable()).thenReturn(false); + when(docs.getDocuments()).thenReturn(documents); + + repositoryConnector.addSeedDocuments(activities, spec, "", seedTime, + BaseRepositoryConnector.JOBMODE_CONTINUOUS); + + verify(activities, times(0)).addSeedDocument(anyString()); + } + + @Test + public void mockDeleteDocument() throws Exception { + NuxeoDocumentHelper docMa = mock(NuxeoDocumentHelper.class); + Repository repository = mock(Repository.class); + Document doc = mock(Document.class); + Specification spec = new Specification(); + IProcessActivity activities = mock(IProcessActivity.class); + IExistingVersions statuses = mock(IExistingVersions.class); + + when(client.repository()).thenReturn(repository); + when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); + when(docMa.getDocument()).thenReturn(doc); + when(doc.getType()).thenReturn("Document"); + when(doc.getState()).thenReturn("deleted"); + + repositoryConnector.processDocuments(new String[] { anyString() }, statuses, spec, activities, + BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); + + verify(activities, times(1)).deleteDocument(anyString()); + } + + @Test + public void mockSimpleIngestion() throws Exception { + + Document doc = mock(Document.class); + NuxeoDocumentHelper docMa = mock(NuxeoDocumentHelper.class); + FileBlob blob = mock(FileBlob.class); + Date date = new Date(); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + final String lastModified = df.format(date); + final String id = "7995ff6d-1eda-41db-b9de-3ea4037fdb81"; + final String path = "/workspaces/folder/001001-0001-000000010690"; + final String repository_name = "default-domain"; + + Map<String, Object> metadata = new HashMap<>(); + IProcessActivity activities = mock(IProcessActivity.class); + IExistingVersions statuses = mock(IExistingVersions.class); + Specification spec = new Specification(); + Repository repository = mock(Repository.class); + final Long size = 1000000L; + + when(docMa.getMetadata()).thenReturn(metadata); + + metadata.put("key", "value"); + + when(blob.getLength()).thenReturn((int)size.intValue()); + when(doc.fetchBlob()).thenReturn(blob); + + when(doc.getLastModified()).thenReturn(lastModified); + when(doc.getUid()).thenReturn(id); + when(doc.getPath()).thenReturn(path); + when(doc.getType()).thenReturn("Document"); + when(doc.getRepositoryName()).thenReturn(repository_name); + when(client.repository()).thenReturn(repository); + when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); + + when(activities.checkDocumentNeedsReindexing(anyString(), anyString())).thenReturn(true); + when(activities.checkLengthIndexable(anyLong())).thenReturn(true); + when(activities.checkDateIndexable(anyObject())).thenReturn(true); + when(activities.checkURLIndexable(anyString())).thenReturn(true); + when(activities.checkMimeTypeIndexable(anyString())).thenReturn(true); + when(statuses.getIndexedVersionString(id)).thenReturn(null); + + + repositoryConnector.processDocuments(new String[] { id }, statuses, spec, activities, + BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); + ArgumentCaptor<RepositoryDocument> ac = ArgumentCaptor.forClass(RepositoryDocument.class); + + String expectedUri = repositoryConnector.getUrl() + "/nxpath/" + + doc.getRepositoryName() + doc.getPath() + "@view_documents"; + + verify(activities, times(1)).ingestDocumentWithException(eq(id), eq(id+"_0.0"), eq(expectedUri), ac.capture()); + verify(activities, times(1)).recordActivity(anyLong(), eq("read document"), eq(size), eq(id), eq("OK"), + anyString(), Mockito.isNull(String[].class)); + + RepositoryDocument rd = ac.getValue(); + Long rdSize = rd.getBinaryLength(); + + String[] keyValue = rd.getFieldAsStrings("uid"); + + assertEquals(size, rdSize); + assertEquals(keyValue.length, 1); + } + + @Test + public void mockNotNeedReindexing() throws Exception { + Document doc = mock(Document.class); + Repository repository = mock(Repository.class); + Date date = new Date(); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + final String lastModified = df.format(date); + String uid = "297529bf-191a-4c87-8259-28b692394229"; + final String path = "/workspaces/folder/001001-0001-000000010690"; + final String repository_name = "default-domain"; + final String version = uid + "_" + "0.0"; + + IProcessActivity activities = mock(IProcessActivity.class); + IExistingVersions statuses = mock(IExistingVersions.class); + Specification spec = new Specification(); + + when(doc.getLastModified()).thenReturn(lastModified); + when(doc.getUid()).thenReturn(uid); + when(doc.getPath()).thenReturn(path); + when(doc.getType()).thenReturn("Document"); + when(doc.getRepositoryName()).thenReturn(repository_name); + when(statuses.getIndexedVersionString(uid)).thenReturn(version); + when(client.repository()).thenReturn(repository); + when(client.repository().fetchDocumentById(anyString())).thenReturn(doc); + when(doc.getType()).thenReturn("Document"); + + repositoryConnector.processDocuments(new String[] { uid }, statuses, spec, activities, + BaseRepositoryConnector.JOBMODE_CONTINUOUS, true); + ArgumentCaptor<RepositoryDocument> ac = ArgumentCaptor.forClass(RepositoryDocument.class); - verify(activities, times(1)).checkDocumentNeedsReindexing(uid, version); - verify(activities, times(0)).ingestDocumentWithException(anyString(), anyString(), anyString(), ac.capture()); + verify(activities, times(1)).checkDocumentNeedsReindexing(uid, version); + verify(activities, times(0)).ingestDocumentWithException(anyString(), anyString(), anyString(), ac.capture()); - } + } }
