Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 1 | /* |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 2 | * 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 9 | */ |
| 10 | package com.intel.jndn.mock; |
| 11 | |
| 12 | import java.io.IOException; |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 13 | import java.util.logging.Logger; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 14 | import net.named_data.jndn.Data; |
| 15 | import net.named_data.jndn.Interest; |
| 16 | import net.named_data.jndn.Name; |
| 17 | import net.named_data.jndn.OnData; |
| 18 | import net.named_data.jndn.OnInterest; |
| 19 | import net.named_data.jndn.encoding.EncodingException; |
| 20 | import net.named_data.jndn.transport.Transport; |
| 21 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import static org.junit.Assert.*; |
| 24 | |
| 25 | /** |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 26 | * Test MockFace functionality |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 27 | * |
| 28 | * @author Andrew Brown <andrew.brown@intel.com> |
| 29 | */ |
| 30 | public class MockFaceTest { |
| 31 | |
| 32 | /** |
| 33 | * Setup logging |
| 34 | */ |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 35 | private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 38 | * Test setting responses for specific names |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 39 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 40 | * @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 Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 59 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 60 | 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 Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 74 | * Test serving data dynamically with OnInterest handlers |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 75 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 76 | * @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 Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 89 | logger.fine("Received interest, responding: " + interest.getName().toUri()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 90 | 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 Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 107 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 108 | 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 120 | |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 121 | // TODO add childInherit test |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 122 | /** |
| 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 | } |