DocumentRetriever { public void retrieveDocuments() throws DocumentRetrieverException { boolean first = true; String route = params.cluster.isEmpty() ? params.route : resolveClusterRoute(params.cluster); LoadType loadType = params.loadTypeName.isEmpty() ? null : resolveLoadType(params.loadTypeName); MessageBusParams messageBusParams = createMessageBusParams(params.configId, params.timeout, route); documentAccess = documentAccessFactory.createDocumentAccess(messageBusParams); session = documentAccess.createSyncSession(new SyncParameters.Builder().build()); int trace = params.traceLevel; if (trace > 0) { session.setTraceLevel(trace); } Iterator<String> iter = params.documentIds; if (params.jsonOutput && !params.printIdsOnly) { System.out.println('['); } while (iter.hasNext()) { if (params.jsonOutput && !params.printIdsOnly) { if (!first) { System.out.println(','); } else { first = false; } } String docid = iter.next(); Message msg = createDocumentRequest(docid, loadType); Reply reply = session.syncSend(msg); printReply(reply); } if (params.jsonOutput && !params.printIdsOnly) { System.out.println(']'); } } DocumentRetriever(ClusterList clusterList,
                             DocumentAccessFactory documentAccessFactory,
                             LoadTypeSet loadTypeSet,
                             ClientParameters params); void shutdown(); void retrieveDocuments();  }
@Test public void testSendSingleMessage() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .setPriority(DocumentProtocol.Priority.HIGH_1) .setNoRetry(true) .setLoadTypeName("loadtype") .build(); when(mockedSession.syncSend(any())).thenReturn(createDocumentReply(DOC_ID_1)); LoadTypeSet loadTypeSet = new LoadTypeSet(); loadTypeSet.addLoadType(1, "loadtype", DocumentProtocol.Priority.HIGH_1); DocumentRetriever documentRetriever = new DocumentRetriever( new ClusterList(), mockedFactory, loadTypeSet, params); documentRetriever.retrieveDocuments(); verify(mockedSession, times(1)).syncSend(argThat((ArgumentMatcher<GetDocumentMessage>) o -> o.getPriority().equals(DocumentProtocol.Priority.HIGH_1) && !o.getRetryEnabled() && o.getLoadType().equals(new LoadType(1, "loadtype", DocumentProtocol.Priority.HIGH_1)))); assertContainsDocument(DOC_ID_1); }
@Test public void testMultipleMessages() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1, DOC_ID_2, DOC_ID_3)) .build(); when(mockedSession.syncSend(any())).thenReturn( createDocumentReply(DOC_ID_1), createDocumentReply(DOC_ID_2), createDocumentReply(DOC_ID_3)); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); verify(mockedSession, times(3)).syncSend(any()); assertContainsDocument(DOC_ID_1); assertContainsDocument(DOC_ID_2); assertContainsDocument(DOC_ID_3); }
@Test public void testJsonOutput() throws DocumentRetrieverException, IOException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1, DOC_ID_2, DOC_ID_3)) .setJsonOutput(true) .build(); when(mockedSession.syncSend(any())).thenReturn( createDocumentReply(DOC_ID_1), createDocumentReply(DOC_ID_2), createDocumentReply(DOC_ID_3)); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); verify(mockedSession, times(3)).syncSend(any()); ObjectMapper m = new ObjectMapper(); @SuppressWarnings("unchecked") List<Map<String, Object>> feed = m.readValue(outContent.toByteArray(), List.class); assertEquals(DOC_ID_1, feed.get(0).get("id")); assertEquals(DOC_ID_2, feed.get(1).get("id")); assertEquals(DOC_ID_3, feed.get(2).get("id")); }
@Test public void testInvalidLoadType() throws DocumentRetrieverException { exception.expect(DocumentRetrieverException.class); exception.expectMessage("Loadtype with name 'undefinedloadtype' does not exist.\n"); ClientParameters params = createParameters() .setLoadTypeName("undefinedloadtype") .build(); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); }
@Test public void testClusterLookup() throws DocumentRetrieverException { final String cluster = "storage", configId = "content/cluster.foo/storage", expectedRoute = "[Storage:cluster=storage;clusterconfigid=content/cluster.foo/storage]"; ClientParameters params = createParameters() .setCluster(cluster) .build(); ClusterList clusterList = new ClusterList(Collections.singletonList(new ClusterDef(cluster, configId))); DocumentRetriever documentRetriever = createDocumentRetriever(params, clusterList); documentRetriever.retrieveDocuments(); verify(mockedFactory).createDocumentAccess(argThat(o -> o.getRoute().equals(expectedRoute))); }
@Test public void testInvalidClusterName() throws DocumentRetrieverException { exception.expect(DocumentRetrieverException.class); exception.expectMessage("The Vespa cluster contains the content clusters storage, not invalidclustername. Please select a valid vespa cluster."); ClientParameters params = createParameters() .setCluster("invalidclustername") .build(); ClusterList clusterList = new ClusterList(Collections.singletonList(new ClusterDef("storage", "content/cluster.foo/storage"))); DocumentRetriever documentRetriever = createDocumentRetriever(params, clusterList); documentRetriever.retrieveDocuments(); }
@Test public void testEmtpyClusterList() throws DocumentRetrieverException { exception.expect(DocumentRetrieverException.class); exception.expectMessage("The Vespa cluster does not have any content clusters declared."); ClientParameters params = createParameters() .setCluster("invalidclustername") .build(); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); }
@Test public void testHandlingErrorFromMessageBus() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .build(); Reply r = new GetDocumentReply(null); r.addError(new Error(0, "Error message")); when(mockedSession.syncSend(any())).thenReturn(r); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); assertTrue(errContent.toString().contains("Request failed")); }
@Test public void testShowDocSize() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .setShowDocSize(true) .build(); Document document = new Document(DataType.DOCUMENT, new DocumentId(DOC_ID_1)); when(mockedSession.syncSend(any())).thenReturn(new GetDocumentReply(document)); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); assertTrue(outContent.toString().contains(String.format("Document size: %d bytes", document.getSerializedSize()))); }
@Test public void testPrintIdOnly() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .setPrintIdsOnly(true) .build(); when(mockedSession.syncSend(any())).thenReturn(createDocumentReply(DOC_ID_1)); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); assertEquals(DOC_ID_1 + "\n", outContent.toString()); }
@Test public void testDocumentNotFound() throws DocumentRetrieverException { ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .setPrintIdsOnly(true) .build(); when(mockedSession.syncSend(any())).thenReturn(new GetDocumentReply(null)); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); verify(mockedSession, times(1)).syncSend(any()); assertEquals(outContent.toString(), "Document not found.\n"); }
@Test public void testTrace() throws DocumentRetrieverException { final int traceLevel = 9; ClientParameters params = createParameters() .setDocumentIds(asIterator(DOC_ID_1)) .setTraceLevel(traceLevel) .build(); GetDocumentReply reply = new GetDocumentReply(new Document(DataType.DOCUMENT, new DocumentId(DOC_ID_1))); reply.getTrace().getRoot().addChild("childnode"); when(mockedSession.syncSend(any())).thenReturn(reply); DocumentRetriever documentRetriever = createDocumentRetriever(params); documentRetriever.retrieveDocuments(); verify(mockedSession, times(1)).setTraceLevel(traceLevel); assertTrue(outContent.toString().contains("<trace>")); }