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; |
| 19 | import net.named_data.jndn.Interest; |
| 20 | import net.named_data.jndn.Name; |
| 21 | import net.named_data.jndn.OnData; |
| 22 | import net.named_data.jndn.OnInterest; |
| 23 | import net.named_data.jndn.encoding.EncodingException; |
| 24 | import net.named_data.jndn.transport.Transport; |
| 25 | import net.named_data.jndn.util.Blob; |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import static org.junit.Assert.*; |
| 28 | |
| 29 | /** |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 30 | * Test MockFace functionality |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 31 | * |
| 32 | * @author Andrew Brown <andrew.brown@intel.com> |
| 33 | */ |
| 34 | public class MockFaceTest { |
| 35 | |
| 36 | /** |
| 37 | * Setup logging |
| 38 | */ |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 39 | private static final Logger logger = Logger.getLogger(MockFaceTest.class.getName()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 42 | * Test setting responses for specific names |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 43 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 44 | * @throws java.io.IOException |
| 45 | * @throws net.named_data.jndn.encoding.EncodingException |
| 46 | */ |
| 47 | @Test |
| 48 | public void testWithResponses() throws IOException, EncodingException { |
| 49 | MockFace face = new MockFace(); |
| 50 | |
| 51 | // add response |
| 52 | Data response = new Data(new Name("/test/with/responses")); |
| 53 | response.setContent(new Blob("...")); |
| 54 | face.addResponse(new Name("/test/with/responses"), response); |
| 55 | |
| 56 | // make request |
| 57 | final Counter count = new Counter(); |
| 58 | logger.info("Express interest: /test/with/responses"); |
| 59 | face.expressInterest(new Interest(new Name("/test/with/responses")), new OnData() { |
| 60 | @Override |
| 61 | public void onData(Interest interest, Data data) { |
| 62 | count.inc(); |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 63 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 64 | assertEquals(data.getContent().buf(), new Blob("...").buf()); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | // process face until a response is received |
| 69 | int allowedLoops = 100; |
| 70 | while (count.get() == 0 && allowedLoops > 0) { |
| 71 | allowedLoops--; |
| 72 | face.processEvents(); |
| 73 | } |
| 74 | assertEquals(1, count.get()); |
| 75 | } |
| 76 | |
| 77 | /** |
Andrew Brown | 8aa0169 | 2015-01-19 13:44:03 -0800 | [diff] [blame] | 78 | * Test serving data dynamically with OnInterest handlers |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 79 | * |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 80 | * @throws net.named_data.jndn.encoding.EncodingException |
| 81 | * @throws java.io.IOException |
| 82 | * @throws net.named_data.jndn.security.SecurityException |
| 83 | */ |
| 84 | @Test |
| 85 | public void testWithHandlers() throws EncodingException, IOException, net.named_data.jndn.security.SecurityException { |
| 86 | MockFace face = new MockFace(); |
| 87 | |
| 88 | // add interest handler |
| 89 | logger.info("Register prefix: /test/with/responses"); |
| 90 | face.registerPrefix(new Name("/test/with/handlers"), new OnInterest() { |
| 91 | @Override |
| 92 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 93 | logger.fine("Received interest, responding: " + interest.getName().toUri()); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 94 | Data response = new Data(new Name("/test/with/handlers")); |
| 95 | response.setContent(new Blob("...")); |
| 96 | try { |
| 97 | transport.send(response.wireEncode().buf()); |
| 98 | } catch (IOException e) { |
| 99 | fail("Failed to send encoded data packet."); |
| 100 | } |
| 101 | } |
| 102 | }, null); |
| 103 | |
| 104 | // make request |
| 105 | final Counter count = new Counter(); |
| 106 | logger.info("Express interest: /test/with/responses"); |
| 107 | face.expressInterest(new Interest(new Name("/test/with/handlers")), new OnData() { |
| 108 | @Override |
| 109 | public void onData(Interest interest, Data data) { |
| 110 | count.inc(); |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 111 | logger.fine("Received data"); |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 112 | assertEquals(data.getContent().buf(), new Blob("...").buf()); |
| 113 | } |
| 114 | }); |
| 115 | |
| 116 | // process faces until a response is received |
| 117 | int allowedLoops = 100; |
| 118 | while (count.get() == 0 && allowedLoops > 0) { |
| 119 | allowedLoops--; |
| 120 | face.processEvents(); |
| 121 | } |
| 122 | assertEquals(1, count.get()); |
| 123 | } |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 124 | |
Andrew Brown | ec1b0d0 | 2015-02-21 13:11:42 -0800 | [diff] [blame] | 125 | // TODO add childInherit test |
Andrew Brown | 3831baf | 2015-01-19 13:38:52 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Count reference |
| 128 | */ |
| 129 | class Counter { |
| 130 | |
| 131 | int count = 0; |
| 132 | |
| 133 | public void inc() { |
| 134 | count++; |
| 135 | } |
| 136 | |
| 137 | public int get() { |
| 138 | return count; |
| 139 | } |
| 140 | } |
| 141 | } |