blob: b60eed838a8388a208326f72c17249414a06c086 [file] [log] [blame]
andrewsbrown8d5ae292015-07-01 17:38:22 -07001/*
2 * 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.
13 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.mock.MockKeyChain;
17import java.io.IOException;
18import java.util.concurrent.CompletableFuture;
19import java.util.concurrent.Executors;
20import java.util.concurrent.ScheduledExecutorService;
21import java.util.concurrent.TimeUnit;
22import java.util.logging.Level;
23import java.util.logging.Logger;
24import net.named_data.jndn.Data;
25import net.named_data.jndn.Face;
26import net.named_data.jndn.Interest;
27import net.named_data.jndn.InterestFilter;
28import net.named_data.jndn.Name;
29import net.named_data.jndn.OnInterestCallback;
30import net.named_data.jndn.encoding.EncodingException;
31import net.named_data.jndn.security.KeyChain;
32import net.named_data.jndn.security.SecurityException;
33import net.named_data.jndn.util.Blob;
34import org.junit.Assert;
35import org.junit.Test;
36
37/**
38 * Test SimpleClient.java; requires a hostname to an NFD accepting a generated
39 * key to register prefixes, e.g. mvn test -Dnfd.ip=10.10.10.1
40 *
41 * @author Andrew Brown <andrew.brown@intel.com>
42 */
43public class SimpleClientTestIT {
44
45 private static final Logger logger = Logger.getLogger(SegmentedServerTestIT.class.getName());
46 private static final Name PREFIX = new Name("/test/for/simple-client");
47
48 Face face;
49 SimpleClient instance;
50 String ip;
51 ScheduledExecutorService pool;
52
53 public SimpleClientTestIT() throws SecurityException {
54 this.ip = System.getProperty("nfd.ip");
55 this.face = new Face(ip);
56 this.instance = SimpleClient.getDefault();
57 this.pool = Executors.newScheduledThreadPool(2);
58
59 KeyChain mockKeyChain = MockKeyChain.configure(new Name("/test/server"));
60 face.setCommandSigningInfo(mockKeyChain, mockKeyChain.getDefaultCertificateName());
61 pool.scheduleAtFixedRate(new EventProcessor(face), 0, 10, TimeUnit.MILLISECONDS);
62 }
63
64 @Test
65 public void testCompletableFuture() throws Exception {
66 Data servedData = new Data();
67 servedData.setContent(new Blob("....."));
68 face.registerPrefix(PREFIX, new OnInterestCallback() {
69 @Override
70 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
71 servedData.setName(interest.getName());
72 try {
73 face.putData(servedData);
74 } catch (IOException ex) {
75 logger.log(Level.SEVERE, "Failed to put data.", ex);
76 }
77 }
78 }, null);
79
80 CompletableFuture<Data> future = instance.getAsync(face, PREFIX);
81 Data retrievedData = future.get(200, TimeUnit.MILLISECONDS);
82
83 Assert.assertEquals(servedData.getContent().toString(), retrievedData.getContent().toString());
84 }
85
86 private class EventProcessor implements Runnable {
87
88 private final Face face;
89
90 public EventProcessor(Face face) {
91 this.face = face;
92 }
93
94 @Override
95 public void run() {
96 try {
97 face.processEvents();
98 } catch (IOException | EncodingException ex) {
99 logger.log(Level.SEVERE, null, ex);
100 }
101 }
102 }
103}