blob: 6bbbdda6f11e8e2a543e0f420be233a4b1801c63 [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
andrewsbrown533c6ef2015-03-03 16:08:41 -08002 * jndn-mock
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 Brown3831baf2015-01-19 13:38:52 -080013 */
14package com.intel.jndn.mock;
15
16import java.io.IOException;
Andrew Brownec1b0d02015-02-21 13:11:42 -080017import java.util.logging.Logger;
Andrew Brown3831baf2015-01-19 13:38:52 -080018import net.named_data.jndn.Data;
19import net.named_data.jndn.Interest;
20import net.named_data.jndn.Name;
21import net.named_data.jndn.OnData;
22import net.named_data.jndn.OnInterest;
23import net.named_data.jndn.encoding.EncodingException;
andrewsbrowna52bf7d2015-04-06 13:51:53 -070024import net.named_data.jndn.security.SecurityException;
Andrew Brown3831baf2015-01-19 13:38:52 -080025import net.named_data.jndn.transport.Transport;
26import net.named_data.jndn.util.Blob;
Andrew Brown3831baf2015-01-19 13:38:52 -080027import org.junit.Test;
28import static org.junit.Assert.*;
29
30/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080031 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080032 *
33 * @author Andrew Brown <andrew.brown@intel.com>
34 */
35public class MockFaceTest {
36
37 /**
38 * Setup logging
39 */
Andrew Brownec1b0d02015-02-21 13:11:42 -080040 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080041
42 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080043 * Test setting responses for specific names
Andrew Brownec1b0d02015-02-21 13:11:42 -080044 *
Andrew Brown3831baf2015-01-19 13:38:52 -080045 * @throws java.io.IOException
46 * @throws net.named_data.jndn.encoding.EncodingException
47 */
48 @Test
49 public void testWithResponses() throws IOException, EncodingException {
50 MockFace face = new MockFace();
51
52 // add response
53 Data response = new Data(new Name("/test/with/responses"));
54 response.setContent(new Blob("..."));
55 face.addResponse(new Name("/test/with/responses"), response);
56
57 // make request
58 final Counter count = new Counter();
59 logger.info("Express interest: /test/with/responses");
60 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
61 @Override
62 public void onData(Interest interest, Data data) {
63 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -080064 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -080065 assertEquals(data.getContent().buf(), new Blob("...").buf());
66 }
67 });
68
69 // process face until a response is received
70 int allowedLoops = 100;
71 while (count.get() == 0 && allowedLoops > 0) {
72 allowedLoops--;
73 face.processEvents();
74 }
75 assertEquals(1, count.get());
76 }
77
78 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080079 * Test serving data dynamically with OnInterest handlers
Andrew Brownec1b0d02015-02-21 13:11:42 -080080 *
Andrew Brown3831baf2015-01-19 13:38:52 -080081 * @throws net.named_data.jndn.encoding.EncodingException
82 * @throws java.io.IOException
83 * @throws net.named_data.jndn.security.SecurityException
84 */
85 @Test
86 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
87 MockFace face = new MockFace();
88
89 // add interest handler
90 logger.info("Register prefix: /test/with/responses");
91 face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
92 @Override
93 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080094 logger.fine("Received interest, responding: " + interest.getName().toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080095 Data response = new Data(new Name("/test/with/handlers"));
96 response.setContent(new Blob("..."));
97 try {
98 transport.send(response.wireEncode().buf());
99 } catch (IOException e) {
100 fail("Failed to send encoded data packet.");
101 }
102 }
103 }, null);
104
105 // make request
106 final Counter count = new Counter();
107 logger.info("Express interest: /test/with/responses");
108 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
109 @Override
110 public void onData(Interest interest, Data data) {
111 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -0800112 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -0800113 assertEquals(data.getContent().buf(), new Blob("...").buf());
114 }
115 });
116
117 // process faces until a response is received
118 int allowedLoops = 100;
119 while (count.get() == 0 && allowedLoops > 0) {
120 allowedLoops--;
121 face.processEvents();
122 }
123 assertEquals(1, count.get());
124 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800125
Andrew Brownec1b0d02015-02-21 13:11:42 -0800126 // TODO add childInherit test
Andrew Brown3831baf2015-01-19 13:38:52 -0800127 /**
128 * Count reference
129 */
130 class Counter {
131
132 int count = 0;
133
134 public void inc() {
135 count++;
136 }
137
138 public int get() {
139 return count;
140 }
141 }
andrewsbrowna52bf7d2015-04-06 13:51:53 -0700142
143 /**
144 * Ensure registering a prefix connects the underlying transport
145 *
146 * @throws IOException
147 * @throws SecurityException
148 */
149 @Test
150 public void testRegistrationConnectsTransport() throws IOException, SecurityException {
151 MockFace face = new MockFace();
152 assertFalse(face.getTransport().getIsConnected());
153 face.registerPrefix(new Name("/fake/prefix"), null, null);
154 assertTrue(face.getTransport().getIsConnected());
155 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800156}