blob: e6773eca7db427a07cb45475aee12ae86bd6efe7 [file] [log] [blame]
Andrew Browndb457052015-02-21 15:41:58 -08001/*
andrewsbrown4feb2da2015-03-03 16:05:29 -08002 * jndn-utils
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
Andrew Browndb457052015-02-21 15:41:58 -080013 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.mock.MockFace;
andrewsbrown69d53292015-03-17 19:37:34 +010017import com.intel.jndn.mock.MockTransport;
Andrew Browndb457052015-02-21 15:41:58 -080018import java.io.IOException;
andrewsbrown69d53292015-03-17 19:37:34 +010019import java.util.List;
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.Future;
22import java.util.logging.Logger;
Andrew Browndb457052015-02-21 15:41:58 -080023import net.named_data.jndn.Data;
andrewsbrown69d53292015-03-17 19:37:34 +010024import net.named_data.jndn.Face;
Andrew Browndb457052015-02-21 15:41:58 -080025import net.named_data.jndn.Interest;
26import net.named_data.jndn.Name;
27import net.named_data.jndn.Name.Component;
28import net.named_data.jndn.OnInterest;
29import net.named_data.jndn.transport.Transport;
30import net.named_data.jndn.util.Blob;
31import org.junit.Test;
32import static org.junit.Assert.*;
33
34/**
35 * Test SegmentedClient functionality.
36 *
37 * @author Andrew Brown <andrew.brown@intel.com>
38 */
39public class SegmentedClientTest {
40
andrewsbrown69d53292015-03-17 19:37:34 +010041 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
42
Andrew Browndb457052015-02-21 15:41:58 -080043 /**
44 * Test of getSync method, of class SegmentedClient.
andrewsbrown69d53292015-03-17 19:37:34 +010045 *
46 * @throws java.lang.Exception
Andrew Browndb457052015-02-21 15:41:58 -080047 */
48 @Test
49 public void testGetSync() throws Exception {
50 MockFace face = new MockFace();
51 face.registerPrefix(new Name("/segmented/data"), new OnInterest() {
52 private int count = 0;
53 private int max = 9;
54
55 @Override
56 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
57 Data data = new Data(interest.getName());
58 if (!SegmentedClient.hasSegment(data.getName())) {
59 data.getName().appendSegment(0);
60 }
61 data.getMetaInfo().setFinalBlockId(Component.fromNumberWithMarker(max, 0x00));
62 data.setContent(new Blob("."));
63 try {
64 transport.send(data.wireEncode().buf());
65 } catch (IOException e) {
66 fail(e.getMessage());
67 }
68 }
69 }, null);
70
71 Data data = SegmentedClient.getDefault().getSync(face, new Name("/segmented/data").appendSegment(0));
72 assertEquals(10, data.getContent().size());
73 }
andrewsbrown69d53292015-03-17 19:37:34 +010074
75 /**
76 * Test that a failed request fails with an exception.
77 *
78 * @throws java.lang.Exception
79 */
80 @Test(expected = ExecutionException.class)
81 public void testFailureToRetrieve() throws Exception {
82 // setup face
83 MockTransport transport = new MockTransport();
84 Face face = new Face(transport, null);
85
86 // retrieve non-existent data, should timeout
87 logger.info("Client expressing interest asynchronously: /test/no-data");
88 List<Future<Data>> futureSegments = SegmentedClient.getDefault().getAsyncList(face, new Name("/test/no-data"));
89
90 // the list of future packets should be initialized
91 assertEquals(1, futureSegments.size());
92 assertTrue(futureSegments.get(0).isDone());
93
94 // should throw error
95 futureSegments.get(0).get();
96 }
Andrew Browndb457052015-02-21 15:41:58 -080097}