blob: 6910059502e6487e7376f5fc39bd57f798462c4a [file] [log] [blame]
andrewsbrown4dddd472015-04-01 14:28:46 -07001/*
2 * jndn-utils
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.
13 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.utils.server.RespondWithData;
17import com.intel.jndn.mock.MockFace;
18import com.intel.jndn.utils.server.RespondWithBlob;
19import java.io.IOException;
20import net.named_data.jndn.Data;
21import net.named_data.jndn.Interest;
22import net.named_data.jndn.Name;
23import net.named_data.jndn.OnData;
24import net.named_data.jndn.encoding.EncodingException;
25import net.named_data.jndn.util.Blob;
26import static org.junit.Assert.*;
27import org.junit.Test;
28
29/**
30 * Test {@link SimpleServer}
31 *
32 * @author Andrew Brown <andrew.brown@intel.com>
33 */
34public class SimpleServerTest {
35
36 MockFace face = new MockFace();
37 SimpleServer instance = new SimpleServer(face, new Name("/test/prefix"));
38
39 @Test
40 public void testGetPrefix() {
41 assertNotNull(instance.getPrefix());
42 }
43
44 @Test
45 public void testAddPipelineStage() {
46 instance.addPipelineStage(null);
47 }
48
49 @Test
50 public void testProcessPipeline() throws Exception {
51 Data in = new Data(new Name("/test"));
52 Data out = instance.processPipeline(in);
53 assertEquals(out, in);
54 }
55
56 @Test
57 public void testRespond() throws IOException, EncodingException {
58 instance.respondUsing(new RespondWithData() {
59 @Override
60 public Data onInterest(Name prefix, Interest interest) throws Exception {
61 Data data = new Data(interest.getName());
62 data.setContent(new Blob("..."));
63 return data;
64 }
65 });
66
67 sendAndCheckOneInterest(new Name("/test/prefix/response"));
68 }
69
70 @Test
71 public void testRespondFromBlob() throws IOException, EncodingException {
72 instance.respondUsing(new RespondWithBlob() {
73 @Override
74 public Blob onInterest(Name prefix, Interest interest) throws Exception {
75 return new Blob("...");
76 }
77 });
78
79 sendAndCheckOneInterest(new Name("/test/prefix/response"));
80 }
81
82 private void sendAndCheckOneInterest(Name interestName) throws EncodingException, IOException {
83 Interest interest = new Interest(interestName);
84 face.getTransport().clear();
85 face.expressInterest(interest, new OnData() {
86 @Override
87 public void onData(Interest interest, Data data) {
88 assertEquals("/test/prefix/response", data.getName().toUri());
89 }
90 });
91
92 face.processEvents();
93 assertEquals(1, face.getTransport().getSentDataPackets().size());
94 assertEquals("...", face.getTransport().getSentDataPackets().get(0).getContent().toString());
95 }
96}