blob: 3c780ef9d543555f1304267c614fa15517962b41 [file] [log] [blame]
Andrew Brown7b1daf32015-01-19 16:36:01 -08001/*
2 * File name: PublisherTest.java
3 *
4 * Purpose:
5 *
6 * © Copyright Intel Corporation. All rights reserved.
7 * Intel Corporation, 2200 Mission College Boulevard,
8 * Santa Clara, CA 95052-8119, USA
9 */
10package com.intel.jndn.utils;
11
12import com.intel.jndn.mock.MockTransport;
13import java.io.IOException;
14import net.named_data.jndn.Data;
15import net.named_data.jndn.Face;
16import net.named_data.jndn.Name;
17import net.named_data.jndn.encoding.EncodingException;
18import net.named_data.jndn.encoding.tlv.Tlv;
19import net.named_data.jndn.encoding.tlv.TlvEncoder;
20import net.named_data.jndn.security.SecurityException;
21import net.named_data.jndn.util.Blob;
22import org.junit.Test;
23import static org.junit.Assert.*;
24
25/**
26 *
27 * @author Andrew Brown <andrew.brown@intel.com>
28 */
29public class PublisherTest {
30
31 /**
32 * Test of publish method, of class Publisher.
33 */
34 @Test
35 public void testPublishSubscribe() throws IOException, EncodingException, InterruptedException, SecurityException {
36 MockTransport transport = new MockTransport();
37 Face face = new Face(transport, null);
38 final Counter counter = new Counter();
39
40 // setup subscriber
41 Subscriber subscriber = new Subscriber(face, new Name("/test/channel"));
42 subscriber.addType("example", TestExample.class);
43 subscriber.on(TestExample.class, new Subscriber.OnPublish<TestExample>() {
44 @Override
45 public void onPublish(TestExample publishedObject) {
46 counter.inc();
47 assertEquals(1, publishedObject.a);
48 assertEquals(true, publishedObject.b);
49 }
50 });
51
52 // setup publisher
53 Publisher publisher = new Publisher(face, new Name("/test/channel"), false);
54 publisher.addType("example", TestExample.class);
55 publisher.publish(new TestExample(1, true));
56
57 // process events
58 while(counter.get() == 0){
59 Thread.sleep(10);
60 }
61
62 // check
63 assertEquals(1, counter.get());
64 }
65
66 public Data fakeManagementSuccess(Name forName, int statusCode, String statusText){
67 TlvEncoder encoder = new TlvEncoder(1500);
68 int saveLength = encoder.getLength();
69
70 // encode backwards
71 encoder.writeBlobTlv(Tlv.NfdCommand_StatusText, new Blob(statusText).buf());
72 encoder.writeNonNegativeIntegerTlv(Tlv.NfdCommand_StatusCode, statusCode);
73 encoder.writeTypeAndLength(Tlv.NfdCommand_ControlResponse, encoder.getLength() - saveLength);
74 Blob content = new Blob(encoder.getOutput(), false);
75
76 // now create data packet
77 Data data = new Data(forName);
78 data.setContent(content);
79 return data;
80 }
81
82 class TestExample {
83
84 int a;
85 boolean b;
86
87 public TestExample(int a, boolean b) {
88 this.a = a;
89 this.b = b;
90 }
91 }
92
93 /**
94 * Count reference
95 */
96 class Counter {
97
98 int count = 0;
99
100 public void inc() {
101 count++;
102 }
103
104 public int get() {
105 return count;
106 }
107 }
108
109// /**
110// * Test of clearOldPublished method, of class Publisher.
111// */
112// @Test
113// public void testClearOldPublished() {
114// System.out.println("clearOldPublished");
115// Publisher instance = null;
116// instance.clearOldPublished();
117// // TODO review the generated test code and remove the default call to fail.
118// fail("The test case is a prototype.");
119// }
120}