blob: 787a39515f20774aaa75b55392aa14f8a6d60bc6 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
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 */
10package com.intel.jndn.utils;
11
Andrew Brown3f2521a2015-01-17 22:10:15 -080012import org.junit.Test;
13import static org.junit.Assert.*;
14import com.intel.jndn.mock.MockTransport;
15import net.named_data.jndn.Data;
16import net.named_data.jndn.Face;
17import net.named_data.jndn.Name;
18import net.named_data.jndn.util.Blob;
Andrew Brown7b1daf32015-01-19 16:36:01 -080019import org.apache.logging.log4j.LogManager;
20import org.apache.logging.log4j.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080021
22/**
23 *
24 * @author Andrew Brown <andrew.brown@intel.com>
25 */
26public class ClientTest {
Andrew Brownac282262015-01-20 16:14:43 -080027
Andrew Brown7b1daf32015-01-19 16:36:01 -080028 /**
29 * Setup logging
30 */
31 private static final Logger logger = LogManager.getLogger();
Andrew Brown3f2521a2015-01-17 22:10:15 -080032
Andrew Brown7b1daf32015-01-19 16:36:01 -080033 /**
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 Brown3f2521a2015-01-17 22:10:15 -080041
Andrew Brown7b1daf32015-01-19 16:36:01 -080042 // setup return data
43 Data response = new Data(new Name("/a/b/c"));
44 response.setContent(new Blob("..."));
45 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080046
Andrew Brown7b1daf32015-01-19 16:36:01 -080047 // 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 Brown3f2521a2015-01-17 22:10:15 -080053
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 /**
55 * Test retrieving data asynchronously
Andrew Brownac282262015-01-20 16:14:43 -080056 *
57 * @throws InterruptedException
Andrew Brown7b1daf32015-01-19 16:36:01 -080058 */
59 @Test
60 public void testGetAsync() throws InterruptedException {
61 // setup face
62 MockTransport transport = new MockTransport();
63 Face face = new Face(transport, null);
Andrew Brown3f2521a2015-01-17 22:10:15 -080064
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 // setup return data
66 Data response = new Data(new Name("/a/b/c"));
67 response.setContent(new Blob("..."));
68 transport.respondWith(response);
Andrew Brown3f2521a2015-01-17 22:10:15 -080069
Andrew Brown7b1daf32015-01-19 16:36:01 -080070 // 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 Brown3f2521a2015-01-17 22:10:15 -080082}