Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 533c6ef | 2015-03-03 16:08:41 -0800 | [diff] [blame] | 2 | * 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 Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.mock; |
| 15 | |
| 16 | import java.io.IOException; |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 17 | import java.util.logging.Logger; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 18 | import net.named_data.jndn.Data; |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 19 | import net.named_data.jndn.Face; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 20 | import net.named_data.jndn.Interest; |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 21 | import net.named_data.jndn.InterestFilter; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 22 | import net.named_data.jndn.Name; |
| 23 | import net.named_data.jndn.OnData; |
| 24 | import net.named_data.jndn.OnInterest; |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 25 | import net.named_data.jndn.OnInterestCallback; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.encoding.EncodingException; |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 27 | import net.named_data.jndn.security.SecurityException; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 28 | import net.named_data.jndn.transport.Transport; |
| 29 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import static org.junit.Assert.*; |
| 32 | |
| 33 | /** |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 34 | * Test MockFace functionality |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 35 | * |
| 36 | * @author Andrew Brown <andrew.brown@intel.com> |
| 37 | */ |
| 38 | public class MockFaceTest { |
| 39 | |
| 40 | /** |
| 41 | * Setup logging |
| 42 | */ |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 43 | private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 46 | * Test setting responses for specific names |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 47 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 48 | * @throws java.io.IOException |
| 49 | * @throws net.named_data.jndn.encoding.EncodingException |
| 50 | */ |
| 51 | @Test |
| 52 | public void testWithResponses() throws IOException, EncodingException { |
| 53 | MockFace face = new MockFace(); |
| 54 | |
| 55 | // add response |
| 56 | Data response = new Data(new Name("/test/with/responses")); |
| 57 | response.setContent(new Blob("...")); |
| 58 | face.addResponse(new Name("/test/with/responses"), response); |
| 59 | |
| 60 | // make request |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 61 | final TestCounter count = new TestCounter(); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 62 | logger.info("Express interest: /test/with/responses"); |
| 63 | face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() { |
| 64 | @Override |
| 65 | public void onData(Interest interest, Data data) { |
| 66 | count.inc(); |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 67 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 68 | assertEquals(data.getContent().buf(), new Blob("...").buf()); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | // process face until a response is received |
| 73 | int allowedLoops = 100; |
| 74 | while (count.get() == 0 && allowedLoops > 0) { |
| 75 | allowedLoops--; |
| 76 | face.processEvents(); |
| 77 | } |
| 78 | assertEquals(1, count.get()); |
| 79 | } |
| 80 | |
| 81 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 82 | * Test serving data dynamically with OnInterest handlers |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 83 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 84 | * @throws net.named_data.jndn.encoding.EncodingException |
| 85 | * @throws java.io.IOException |
| 86 | * @throws net.named_data.jndn.security.SecurityException |
| 87 | */ |
| 88 | @Test |
| 89 | public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException { |
| 90 | MockFace face = new MockFace(); |
| 91 | |
| 92 | // add interest handler |
| 93 | logger.info("Register prefix: /test/with/responses"); |
| 94 | face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() { |
| 95 | @Override |
| 96 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 97 | logger.fine("Received interest, responding: " + interest.getName().toUri()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 98 | Data response = new Data(new Name("/test/with/handlers")); |
| 99 | response.setContent(new Blob("...")); |
| 100 | try { |
| 101 | transport.send(response.wireEncode().buf()); |
| 102 | } catch (IOException e) { |
| 103 | fail("Failed to send encoded data packet."); |
| 104 | } |
| 105 | } |
| 106 | }, null); |
| 107 | |
| 108 | // make request |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 109 | final TestCounter count = new TestCounter(); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 110 | logger.info("Express interest: /test/with/responses"); |
| 111 | face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() { |
| 112 | @Override |
| 113 | public void onData(Interest interest, Data data) { |
| 114 | count.inc(); |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 115 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 116 | assertEquals(data.getContent().buf(), new Blob("...").buf()); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | // process faces until a response is received |
| 121 | int allowedLoops = 100; |
| 122 | while (count.get() == 0 && allowedLoops > 0) { |
| 123 | allowedLoops--; |
| 124 | face.processEvents(); |
| 125 | } |
| 126 | assertEquals(1, count.get()); |
| 127 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 128 | |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Ensure registering a prefix connects the underlying transport |
| 131 | * |
| 132 | * @throws IOException |
| 133 | * @throws SecurityException |
| 134 | */ |
| 135 | @Test |
| 136 | public void testRegistrationConnectsTransport() throws IOException, SecurityException { |
| 137 | MockFace face = new MockFace(); |
| 138 | assertFalse(face.getTransport().getIsConnected()); |
andrewsbrown | 1f28bcf | 2015-04-20 13:29:20 -0700 | [diff] [blame] | 139 | face.registerPrefix(new Name("/fake/prefix"), (OnInterest) null, null); |
andrewsbrown | a52bf7d | 2015-04-06 13:51:53 -0700 | [diff] [blame] | 140 | assertTrue(face.getTransport().getIsConnected()); |
| 141 | } |
andrewsbrown | 0f36eee | 2015-05-07 01:37:48 +0100 | [diff] [blame^] | 142 | |
| 143 | /** |
| 144 | * Test that interest filters work as expected |
| 145 | */ |
| 146 | @Test |
| 147 | public void testInterestFilters() throws IOException, SecurityException, EncodingException { |
| 148 | MockFace face = new MockFace(); |
| 149 | |
| 150 | final TestCounter count = new TestCounter(); |
| 151 | face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() { |
| 152 | @Override |
| 153 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { |
| 154 | count.inc(); |
| 155 | } |
| 156 | }); |
| 157 | |
| 158 | face.expressInterest(new Interest(new Name("/a/b")).setInterestLifetimeMilliseconds(100), null); |
| 159 | face.processEvents(); |
| 160 | |
| 161 | assertEquals(1, count.get()); |
| 162 | } |
| 163 | |
| 164 | @Test |
| 165 | public void testResponseFromInsideElementReader() throws IOException, SecurityException, EncodingException{ |
| 166 | MockFace face = new MockFace(); |
| 167 | face.setInterestFilter(new InterestFilter("/a/b"), new OnInterestCallback() { |
| 168 | @Override |
| 169 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { |
| 170 | try { |
| 171 | face.putData(new Data(interest.getName()).setContent(new Blob("......"))); |
| 172 | } catch (IOException ex) { |
| 173 | fail("Failed to put data."); |
| 174 | } |
| 175 | } |
| 176 | }); |
| 177 | |
| 178 | final TestCounter count = new TestCounter(); |
| 179 | face.expressInterest(new Interest(new Name("/a/b/c")), new OnData() { |
| 180 | @Override |
| 181 | public void onData(Interest interest, Data data) { |
| 182 | logger.info("Data returned: " + data.getContent().toString()); |
| 183 | count.inc(); |
| 184 | } |
| 185 | }); |
| 186 | assertEquals(0, count.get()); |
| 187 | |
| 188 | face.processEvents(); |
| 189 | face.processEvents(); // the second processEvents() is required because the InterestFilter sends data from within the first processEvents loop |
| 190 | assertEquals(1, count.get()); |
| 191 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 192 | } |