Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 6 | package com.intel.jndn.mock; |
| 7 | |
| 8 | import java.io.IOException; |
| 9 | import net.named_data.jndn.Data; |
| 10 | import net.named_data.jndn.Interest; |
| 11 | import net.named_data.jndn.Name; |
| 12 | import net.named_data.jndn.OnData; |
| 13 | import net.named_data.jndn.OnInterest; |
| 14 | import net.named_data.jndn.encoding.EncodingException; |
| 15 | import net.named_data.jndn.transport.Transport; |
| 16 | import net.named_data.jndn.util.Blob; |
| 17 | import org.apache.logging.log4j.LogManager; |
| 18 | import org.apache.logging.log4j.Logger; |
| 19 | import org.junit.Test; |
| 20 | import static org.junit.Assert.*; |
| 21 | |
| 22 | /** |
| 23 | * |
| 24 | * @author Andrew Brown <andrew.brown@intel.com> |
| 25 | */ |
| 26 | public class MockFaceTest { |
| 27 | |
| 28 | /** |
| 29 | * Setup logging |
| 30 | */ |
| 31 | private static final Logger logger = LogManager.getLogger(); |
| 32 | |
| 33 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame^] | 34 | * Test setting responses for specific names |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 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 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame^] | 69 | * Test serving data dynamically with OnInterest handlers |
| 70 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 71 | * @throws net.named_data.jndn.encoding.EncodingException |
| 72 | * @throws java.io.IOException |
| 73 | * @throws net.named_data.jndn.security.SecurityException |
| 74 | */ |
| 75 | @Test |
| 76 | public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException { |
| 77 | MockFace face = new MockFace(); |
| 78 | |
| 79 | // add interest handler |
| 80 | logger.info("Register prefix: /test/with/responses"); |
| 81 | face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() { |
| 82 | @Override |
| 83 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 84 | logger.debug("Received interest, responding: " + interest.getName().toUri()); |
| 85 | Data response = new Data(new Name("/test/with/handlers")); |
| 86 | response.setContent(new Blob("...")); |
| 87 | try { |
| 88 | transport.send(response.wireEncode().buf()); |
| 89 | } catch (IOException e) { |
| 90 | fail("Failed to send encoded data packet."); |
| 91 | } |
| 92 | } |
| 93 | }, null); |
| 94 | |
| 95 | // make request |
| 96 | final Counter count = new Counter(); |
| 97 | logger.info("Express interest: /test/with/responses"); |
| 98 | face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() { |
| 99 | @Override |
| 100 | public void onData(Interest interest, Data data) { |
| 101 | count.inc(); |
| 102 | logger.debug("Received data"); |
| 103 | assertEquals(data.getContent().buf(), new Blob("...").buf()); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | // process faces until a response is received |
| 108 | int allowedLoops = 100; |
| 109 | while (count.get() == 0 && allowedLoops > 0) { |
| 110 | allowedLoops--; |
| 111 | face.processEvents(); |
| 112 | } |
| 113 | assertEquals(1, count.get()); |
| 114 | } |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame^] | 115 | |
| 116 | // TODO add childInherit test |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Count reference |
| 120 | */ |
| 121 | class Counter { |
| 122 | |
| 123 | int count = 0; |
| 124 | |
| 125 | public void inc() { |
| 126 | count++; |
| 127 | } |
| 128 | |
| 129 | public int get() { |
| 130 | return count; |
| 131 | } |
| 132 | } |
| 133 | } |