blob: 8c51a6e2d5fd87bac6b059ca34c253c8e1b283bd [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;
24import net.named_data.jndn.transport.Transport;
25import net.named_data.jndn.util.Blob;
Andrew Brown3831baf2015-01-19 13:38:52 -080026import org.junit.Test;
27import static org.junit.Assert.*;
28
29/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080030 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080031 *
32 * @author Andrew Brown <andrew.brown@intel.com>
33 */
34public class MockFaceTest {
35
36 /**
37 * Setup logging
38 */
Andrew Brownec1b0d02015-02-21 13:11:42 -080039 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080040
41 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080042 * Test setting responses for specific names
Andrew Brownec1b0d02015-02-21 13:11:42 -080043 *
Andrew Brown3831baf2015-01-19 13:38:52 -080044 * @throws java.io.IOException
45 * @throws net.named_data.jndn.encoding.EncodingException
46 */
47 @Test
48 public void testWithResponses() throws IOException, EncodingException {
49 MockFace face = new MockFace();
50
51 // add response
52 Data response = new Data(new Name("/test/with/responses"));
53 response.setContent(new Blob("..."));
54 face.addResponse(new Name("/test/with/responses"), response);
55
56 // make request
57 final Counter count = new Counter();
58 logger.info("Express interest: /test/with/responses");
59 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
60 @Override
61 public void onData(Interest interest, Data data) {
62 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -080063 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -080064 assertEquals(data.getContent().buf(), new Blob("...").buf());
65 }
66 });
67
68 // process face until a response is received
69 int allowedLoops = 100;
70 while (count.get() == 0 && allowedLoops > 0) {
71 allowedLoops--;
72 face.processEvents();
73 }
74 assertEquals(1, count.get());
75 }
76
77 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080078 * Test serving data dynamically with OnInterest handlers
Andrew Brownec1b0d02015-02-21 13:11:42 -080079 *
Andrew Brown3831baf2015-01-19 13:38:52 -080080 * @throws net.named_data.jndn.encoding.EncodingException
81 * @throws java.io.IOException
82 * @throws net.named_data.jndn.security.SecurityException
83 */
84 @Test
85 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
86 MockFace face = new MockFace();
87
88 // add interest handler
89 logger.info("Register prefix: /test/with/responses");
90 face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
91 @Override
92 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080093 logger.fine("Received interest, responding: " + interest.getName().toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080094 Data response = new Data(new Name("/test/with/handlers"));
95 response.setContent(new Blob("..."));
96 try {
97 transport.send(response.wireEncode().buf());
98 } catch (IOException e) {
99 fail("Failed to send encoded data packet.");
100 }
101 }
102 }, null);
103
104 // make request
105 final Counter count = new Counter();
106 logger.info("Express interest: /test/with/responses");
107 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
108 @Override
109 public void onData(Interest interest, Data data) {
110 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -0800111 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -0800112 assertEquals(data.getContent().buf(), new Blob("...").buf());
113 }
114 });
115
116 // process faces until a response is received
117 int allowedLoops = 100;
118 while (count.get() == 0 && allowedLoops > 0) {
119 allowedLoops--;
120 face.processEvents();
121 }
122 assertEquals(1, count.get());
123 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800124
Andrew Brownec1b0d02015-02-21 13:11:42 -0800125 // TODO add childInherit test
Andrew Brown3831baf2015-01-19 13:38:52 -0800126 /**
127 * Count reference
128 */
129 class Counter {
130
131 int count = 0;
132
133 public void inc() {
134 count++;
135 }
136
137 public int get() {
138 return count;
139 }
140 }
141}