blob: 3cae974b8a35cf12fa5523183ebe83b9ab454e55 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -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 Brown3f2521a2015-01-17 22:10:15 -080013 */
14package com.intel.jndn.utils;
15
Andrew Brown3f2521a2015-01-17 22:10:15 -080016import org.junit.Test;
17import static org.junit.Assert.*;
18import com.intel.jndn.mock.MockTransport;
andrewsbrown4dddd472015-04-01 14:28:46 -070019import com.intel.jndn.utils.client.FutureData;
andrewsbrown69d53292015-03-17 19:37:34 +010020import java.io.IOException;
Andrew Browndb457052015-02-21 15:41:58 -080021import java.util.concurrent.ExecutionException;
andrewsbrown69d53292015-03-17 19:37:34 +010022import java.util.concurrent.Future;
Andrew Browndb457052015-02-21 15:41:58 -080023import java.util.concurrent.TimeUnit;
24import java.util.concurrent.TimeoutException;
25import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080026import net.named_data.jndn.Data;
27import net.named_data.jndn.Face;
28import net.named_data.jndn.Name;
29import net.named_data.jndn.util.Blob;
Andrew Browndb457052015-02-21 15:41:58 -080030import org.junit.rules.ExpectedException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080031
32/**
Andrew Brown070dc892015-01-21 09:55:12 -080033 * Test Client.java
andrewsbrown69d53292015-03-17 19:37:34 +010034 *
Andrew Brown3f2521a2015-01-17 22:10:15 -080035 * @author Andrew Brown <andrew.brown@intel.com>
36 */
andrewsbrown90712cb2015-03-31 14:44:12 -070037public class SimpleClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080038
andrewsbrown69d53292015-03-17 19:37:34 +010039 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
40 public ExpectedException thrown = ExpectedException.none();
Andrew Brown3f2521a2015-01-17 22:10:15 -080041
Andrew Brown7b1daf32015-01-19 16:36:01 -080042 /**
43 * Test retrieving data synchronously
andrewsbrown69d53292015-03-17 19:37:34 +010044 *
45 * @throws java.io.IOException
Andrew Brown7b1daf32015-01-19 16:36:01 -080046 */
47 @Test
andrewsbrown69d53292015-03-17 19:37:34 +010048 public void testGetSync() throws IOException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080049 // setup face
50 MockTransport transport = new MockTransport();
51 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080052
Andrew Brown7b1daf32015-01-19 16:36:01 -080053 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080054 Data response = new Data(new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080055 response.setContent(new Blob("..."));
56 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080057
Andrew Brown7b1daf32015-01-19 16:36:01 -080058 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080059 logger.info("Client expressing interest synchronously: /test/sync");
andrewsbrown69d53292015-03-17 19:37:34 +010060 SimpleClient client = new SimpleClient();
Andrew Brown070dc892015-01-21 09:55:12 -080061 Data data = client.getSync(face, new Name("/test/sync"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080062 assertEquals(new Blob("...").buf(), data.getContent().buf());
63 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080064
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 /**
66 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080067 *
68 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080069 */
70 @Test
Andrew Browndb457052015-02-21 15:41:58 -080071 public void testGetAsync() throws InterruptedException, ExecutionException {
Andrew Brown7b1daf32015-01-19 16:36:01 -080072 // setup face
73 MockTransport transport = new MockTransport();
74 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080075
Andrew Brown7b1daf32015-01-19 16:36:01 -080076 // setup return data
Andrew Brown070dc892015-01-21 09:55:12 -080077 Data response = new Data(new Name("/test/async"));
Andrew Brown7b1daf32015-01-19 16:36:01 -080078 response.setContent(new Blob("..."));
79 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080080
Andrew Brown7b1daf32015-01-19 16:36:01 -080081 // retrieve data
Andrew Brown070dc892015-01-21 09:55:12 -080082 logger.info("Client expressing interest asynchronously: /test/async");
andrewsbrown69d53292015-03-17 19:37:34 +010083 SimpleClient client = new SimpleClient();
84 Future<Data> futureData = client.getAsync(face, new Name("/test/async"));
85
Andrew Browndb457052015-02-21 15:41:58 -080086 assertTrue(!futureData.isDone());
87 futureData.get();
88 assertTrue(futureData.isDone());
89 assertEquals(new Blob("...").toString(), futureData.get().getContent().toString());
Andrew Brown7b1daf32015-01-19 16:36:01 -080090 }
Andrew Brown070dc892015-01-21 09:55:12 -080091
92 /**
93 * Test that asynchronous client times out correctly
andrewsbrown69d53292015-03-17 19:37:34 +010094 *
95 * @throws InterruptedException
Andrew Brown070dc892015-01-21 09:55:12 -080096 */
andrewsbrown69d53292015-03-17 19:37:34 +010097 @Test(expected = TimeoutException.class)
Andrew Browndb457052015-02-21 15:41:58 -080098 public void testTimeout() throws InterruptedException, ExecutionException, TimeoutException {
Andrew Brown070dc892015-01-21 09:55:12 -080099 // setup face
100 MockTransport transport = new MockTransport();
101 Face face = new Face(transport, null);
102
103 // retrieve non-existent data, should timeout
104 logger.info("Client expressing interest asynchronously: /test/timeout");
andrewsbrown69d53292015-03-17 19:37:34 +0100105 Future<Data> futureData = SimpleClient.getDefault().getAsync(face, new Name("/test/timeout"));
106
107 // expect an exception
Andrew Browndb457052015-02-21 15:41:58 -0800108 futureData.get(50, TimeUnit.MILLISECONDS);
Andrew Brown070dc892015-01-21 09:55:12 -0800109 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700110
111 /**
112 * Test that a sync failed request fails with an exception.
113 */
114 @Test(expected = ExecutionException.class)
115 public void testAsyncFailureToRetrieve() throws InterruptedException, ExecutionException {
116 Future future = SimpleClient.getDefault().getAsync(new Face(), new Name("/test/no-data"));
andrewsbrown4dddd472015-04-01 14:28:46 -0700117 future.get();
Andrew Brown2659ec32015-05-07 11:05:50 -0700118 assertTrue(future.isDone());
andrewsbrown4dddd472015-04-01 14:28:46 -0700119 }
120
121 /**
122 * Test that a sync failed request fails with an exception.
123 */
124 @Test(expected = IOException.class)
125 public void testSyncFailureToRetrieve() throws IOException {
126 SimpleClient.getDefault().getSync(new Face(), new Name("/test/no-data"));
127 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800128}