blob: 0646f94065dc34680454e87ed8f55f41aeb4311a [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
Andrew Brownec1b0d02015-02-21 13:11:42 -08002 * File name: MockFaceTest.java
3 *
4 * Purpose: Test MockFace functionality.
5 *
6 * © Copyright Intel Corporation. All rights reserved.
7 * Intel Corporation, 2200 Mission College Boulevard,
8 * Santa Clara, CA 95052-8119, USA
Andrew Brown3831baf2015-01-19 13:38:52 -08009 */
10package com.intel.jndn.mock;
11
12import java.io.IOException;
Andrew Brownec1b0d02015-02-21 13:11:42 -080013import java.util.logging.Logger;
Andrew Brown3831baf2015-01-19 13:38:52 -080014import net.named_data.jndn.Data;
15import net.named_data.jndn.Interest;
16import net.named_data.jndn.Name;
17import net.named_data.jndn.OnData;
18import net.named_data.jndn.OnInterest;
19import net.named_data.jndn.encoding.EncodingException;
20import net.named_data.jndn.transport.Transport;
21import net.named_data.jndn.util.Blob;
Andrew Brown3831baf2015-01-19 13:38:52 -080022import org.junit.Test;
23import static org.junit.Assert.*;
24
25/**
Andrew Brownec1b0d02015-02-21 13:11:42 -080026 * Test MockFace functionality
Andrew Brown3831baf2015-01-19 13:38:52 -080027 *
28 * @author Andrew Brown <andrew.brown@intel.com>
29 */
30public class MockFaceTest {
31
32 /**
33 * Setup logging
34 */
Andrew Brownec1b0d02015-02-21 13:11:42 -080035 private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName());
Andrew Brown3831baf2015-01-19 13:38:52 -080036
37 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080038 * Test setting responses for specific names
Andrew Brownec1b0d02015-02-21 13:11:42 -080039 *
Andrew Brown3831baf2015-01-19 13:38:52 -080040 * @throws java.io.IOException
41 * @throws net.named_data.jndn.encoding.EncodingException
42 */
43 @Test
44 public void testWithResponses() throws IOException, EncodingException {
45 MockFace face = new MockFace();
46
47 // add response
48 Data response = new Data(new Name("/test/with/responses"));
49 response.setContent(new Blob("..."));
50 face.addResponse(new Name("/test/with/responses"), response);
51
52 // make request
53 final Counter count = new Counter();
54 logger.info("Express interest: /test/with/responses");
55 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
56 @Override
57 public void onData(Interest interest, Data data) {
58 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -080059 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -080060 assertEquals(data.getContent().buf(), new Blob("...").buf());
61 }
62 });
63
64 // process face until a response is received
65 int allowedLoops = 100;
66 while (count.get() == 0 && allowedLoops > 0) {
67 allowedLoops--;
68 face.processEvents();
69 }
70 assertEquals(1, count.get());
71 }
72
73 /**
Andrew Brown8aa01692015-01-19 13:44:03 -080074 * Test serving data dynamically with OnInterest handlers
Andrew Brownec1b0d02015-02-21 13:11:42 -080075 *
Andrew Brown3831baf2015-01-19 13:38:52 -080076 * @throws net.named_data.jndn.encoding.EncodingException
77 * @throws java.io.IOException
78 * @throws net.named_data.jndn.security.SecurityException
79 */
80 @Test
81 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
82 MockFace face = new MockFace();
83
84 // add interest handler
85 logger.info("Register prefix: /test/with/responses");
86 face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
87 @Override
88 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
Andrew Brownec1b0d02015-02-21 13:11:42 -080089 logger.fine("Received interest, responding: " + interest.getName().toUri());
Andrew Brown3831baf2015-01-19 13:38:52 -080090 Data response = new Data(new Name("/test/with/handlers"));
91 response.setContent(new Blob("..."));
92 try {
93 transport.send(response.wireEncode().buf());
94 } catch (IOException e) {
95 fail("Failed to send encoded data packet.");
96 }
97 }
98 }, null);
99
100 // make request
101 final Counter count = new Counter();
102 logger.info("Express interest: /test/with/responses");
103 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
104 @Override
105 public void onData(Interest interest, Data data) {
106 count.inc();
Andrew Brownec1b0d02015-02-21 13:11:42 -0800107 logger.fine("Received data");
Andrew Brown3831baf2015-01-19 13:38:52 -0800108 assertEquals(data.getContent().buf(), new Blob("...").buf());
109 }
110 });
111
112 // process faces until a response is received
113 int allowedLoops = 100;
114 while (count.get() == 0 && allowedLoops > 0) {
115 allowedLoops--;
116 face.processEvents();
117 }
118 assertEquals(1, count.get());
119 }
Andrew Brown3831baf2015-01-19 13:38:52 -0800120
Andrew Brownec1b0d02015-02-21 13:11:42 -0800121 // TODO add childInherit test
Andrew Brown3831baf2015-01-19 13:38:52 -0800122 /**
123 * Count reference
124 */
125 class Counter {
126
127 int count = 0;
128
129 public void inc() {
130 count++;
131 }
132
133 public int get() {
134 return count;
135 }
136 }
137}