blob: 77ce5a30168a3ed464cea077309a9bfa11a2aab6 [file] [log] [blame]
Andrew Brown3831baf2015-01-19 13:38:52 -08001/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package com.intel.jndn.mock;
7
8import java.io.IOException;
9import net.named_data.jndn.Data;
10import net.named_data.jndn.Interest;
11import net.named_data.jndn.Name;
12import net.named_data.jndn.OnData;
13import net.named_data.jndn.OnInterest;
14import net.named_data.jndn.encoding.EncodingException;
15import net.named_data.jndn.transport.Transport;
16import net.named_data.jndn.util.Blob;
17import org.apache.logging.log4j.LogManager;
18import org.apache.logging.log4j.Logger;
19import org.junit.Test;
20import static org.junit.Assert.*;
21
22/**
23 *
24 * @author Andrew Brown <andrew.brown@intel.com>
25 */
26public class MockFaceTest {
27
28 /**
29 * Setup logging
30 */
31 private static final Logger logger = LogManager.getLogger();
32
33 /**
34 * Test of addResponse method, of class MockFace.
35 * @throws java.io.IOException
36 * @throws net.named_data.jndn.encoding.EncodingException
37 */
38 @Test
39 public void testWithResponses() throws IOException, EncodingException {
40 MockFace face = new MockFace();
41
42 // add response
43 Data response = new Data(new Name("/test/with/responses"));
44 response.setContent(new Blob("..."));
45 face.addResponse(new Name("/test/with/responses"), response);
46
47 // make request
48 final Counter count = new Counter();
49 logger.info("Express interest: /test/with/responses");
50 face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() {
51 @Override
52 public void onData(Interest interest, Data data) {
53 count.inc();
54 logger.debug("Received data");
55 assertEquals(data.getContent().buf(), new Blob("...").buf());
56 }
57 });
58
59 // process face until a response is received
60 int allowedLoops = 100;
61 while (count.get() == 0 && allowedLoops > 0) {
62 allowedLoops--;
63 face.processEvents();
64 }
65 assertEquals(1, count.get());
66 }
67
68 /**
69 * Test of removeResponse method, of class MockFace.
70 * @throws net.named_data.jndn.encoding.EncodingException
71 * @throws java.io.IOException
72 * @throws net.named_data.jndn.security.SecurityException
73 */
74 @Test
75 public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException {
76 MockFace face = new MockFace();
77
78 // add interest handler
79 logger.info("Register prefix: /test/with/responses");
80 face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() {
81 @Override
82 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
83 logger.debug("Received interest, responding: " + interest.getName().toUri());
84 Data response = new Data(new Name("/test/with/handlers"));
85 response.setContent(new Blob("..."));
86 try {
87 transport.send(response.wireEncode().buf());
88 } catch (IOException e) {
89 fail("Failed to send encoded data packet.");
90 }
91 }
92 }, null);
93
94 // make request
95 final Counter count = new Counter();
96 logger.info("Express interest: /test/with/responses");
97 face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() {
98 @Override
99 public void onData(Interest interest, Data data) {
100 count.inc();
101 logger.debug("Received data");
102 assertEquals(data.getContent().buf(), new Blob("...").buf());
103 }
104 });
105
106 // process faces until a response is received
107 int allowedLoops = 100;
108 while (count.get() == 0 && allowedLoops > 0) {
109 allowedLoops--;
110 face.processEvents();
111 }
112 assertEquals(1, count.get());
113 }
114
115 /**
116 * Count reference
117 */
118 class Counter {
119
120 int count = 0;
121
122 public void inc() {
123 count++;
124 }
125
126 public int get() {
127 return count;
128 }
129 }
130}