Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: ClientTest.java |
| 3 | * |
| 4 | * Purpose: Test Client.java |
| 5 | * |
| 6 | * © Copyright Intel Corporation. All rights reserved. |
| 7 | * Intel Corporation, 2200 Mission College Boulevard, |
| 8 | * Santa Clara, CA 95052-8119, USA |
| 9 | */ |
| 10 | package com.intel.jndn.utils; |
| 11 | |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 12 | import org.junit.Test; |
| 13 | import static org.junit.Assert.*; |
| 14 | import com.intel.jndn.mock.MockTransport; |
| 15 | import net.named_data.jndn.Data; |
| 16 | import net.named_data.jndn.Face; |
| 17 | import net.named_data.jndn.Name; |
| 18 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 19 | import org.apache.logging.log4j.LogManager; |
| 20 | import org.apache.logging.log4j.Logger; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * |
| 24 | * @author Andrew Brown <andrew.brown@intel.com> |
| 25 | */ |
| 26 | public class ClientTest { |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame^] | 27 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 28 | /** |
| 29 | * Setup logging |
| 30 | */ |
| 31 | private static final Logger logger = LogManager.getLogger(); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 32 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Test retrieving data synchronously |
| 35 | */ |
| 36 | @Test |
| 37 | public void testGetSync() { |
| 38 | // setup face |
| 39 | MockTransport transport = new MockTransport(); |
| 40 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 41 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 42 | // setup return data |
| 43 | Data response = new Data(new Name("/a/b/c")); |
| 44 | response.setContent(new Blob("...")); |
| 45 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 46 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 47 | // retrieve data |
| 48 | logger.info("Client expressing interest synchronously: /a/b/c"); |
| 49 | Client client = new Client(); |
| 50 | Data data = client.getSync(face, new Name("/a/b/c")); |
| 51 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 52 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 53 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Test retrieving data asynchronously |
Andrew Brown | ac28226 | 2015-01-20 16:14:43 -0800 | [diff] [blame^] | 56 | * |
| 57 | * @throws InterruptedException |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 58 | */ |
| 59 | @Test |
| 60 | public void testGetAsync() throws InterruptedException { |
| 61 | // setup face |
| 62 | MockTransport transport = new MockTransport(); |
| 63 | Face face = new Face(transport, null); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 64 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 65 | // setup return data |
| 66 | Data response = new Data(new Name("/a/b/c")); |
| 67 | response.setContent(new Blob("...")); |
| 68 | transport.respondWith(response); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 69 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 70 | // retrieve data |
| 71 | logger.info("Client expressing interest asynchronously: /a/b/c"); |
| 72 | Client client = new Client(); |
| 73 | ClientObserver observer = client.get(face, new Name("/a/b/c")); |
| 74 | |
| 75 | // wait |
| 76 | while (observer.responses() == 0) { |
| 77 | Thread.sleep(10); |
| 78 | } |
| 79 | Data data = (Data) observer.getFirst().getPacket(); |
| 80 | assertEquals(new Blob("...").buf(), data.getContent().buf()); |
| 81 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 82 | } |