View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  package org.fourthline.cling.mock;
16  
17  import org.fourthline.cling.UpnpServiceConfiguration;
18  import org.fourthline.cling.model.NetworkAddress;
19  import org.fourthline.cling.model.message.IncomingDatagramMessage;
20  import org.fourthline.cling.model.message.OutgoingDatagramMessage;
21  import org.fourthline.cling.model.message.StreamRequestMessage;
22  import org.fourthline.cling.model.message.StreamResponseMessage;
23  import org.fourthline.cling.protocol.ProtocolFactory;
24  import org.fourthline.cling.transport.Router;
25  import org.fourthline.cling.transport.RouterException;
26  import org.fourthline.cling.transport.impl.NetworkAddressFactoryImpl;
27  import org.fourthline.cling.transport.spi.InitializationException;
28  import org.fourthline.cling.transport.spi.NetworkAddressFactory;
29  import org.fourthline.cling.transport.spi.UpnpStream;
30  
31  import javax.enterprise.inject.Alternative;
32  import java.net.InetAddress;
33  import java.net.UnknownHostException;
34  import java.util.ArrayList;
35  import java.util.Arrays;
36  import java.util.List;
37  
38  /**
39   * <p>
40   * This is not a real network transport layer, it collects all messages instead and makes
41   * them available for testing with {@link #getOutgoingDatagramMessages()},
42   * {@link #getSentStreamRequestMessages()}, etc. Mock responses for TCP (HTTP) stream requests
43   * can be returned by overriding {@link #getStreamResponseMessage(org.fourthline.cling.model.message.StreamRequestMessage)}
44   * or {@link #getStreamResponseMessages()} if you know the order of requests.
45   * </p>
46   *
47   * @author Christian Bauer
48   */
49  @Alternative
50  public class MockRouter implements Router {
51  
52      public int counter = -1;
53      public List<IncomingDatagramMessage> incomingDatagramMessages = new ArrayList<>();
54      public List<OutgoingDatagramMessage> outgoingDatagramMessages = new ArrayList<>();
55      public List<UpnpStream> receivedUpnpStreams = new ArrayList<>();
56      public List<StreamRequestMessage> sentStreamRequestMessages = new ArrayList<>();
57      public List<byte[]> broadcastedBytes = new ArrayList<>();
58  
59      protected UpnpServiceConfiguration configuration;
60      protected ProtocolFactory protocolFactory;
61  
62      public MockRouter(UpnpServiceConfiguration configuration,
63                        ProtocolFactory protocolFactory) {
64          this.configuration = configuration;
65          this.protocolFactory = protocolFactory;
66      }
67  
68      @Override
69      public UpnpServiceConfiguration getConfiguration() {
70          return configuration;
71      }
72  
73      @Override
74      public ProtocolFactory getProtocolFactory() {
75          return protocolFactory;
76      }
77  
78      @Override
79      public boolean enable() throws RouterException {
80          return false;
81      }
82  
83      @Override
84      public boolean disable() throws RouterException {
85          return false;
86      }
87  
88      @Override
89      public void shutdown() throws RouterException {
90      }
91  
92      @Override
93      public boolean isEnabled() throws RouterException {
94          return false;
95      }
96  
97      @Override
98      public void handleStartFailure(InitializationException ex) throws InitializationException {
99      }
100 
101     @Override
102     public List<NetworkAddress> getActiveStreamServers(InetAddress preferredAddress) throws RouterException {
103         // Simulate an active stream server, otherwise the notification/search response
104         // protocols won't even run
105         try {
106             return Arrays.asList(
107                 new NetworkAddress(
108                     InetAddress.getByName("127.0.0.1"),
109                     NetworkAddressFactoryImpl.DEFAULT_TCP_HTTP_LISTEN_PORT
110                 )
111             );
112         } catch (UnknownHostException ex) {
113             throw new RuntimeException(ex);
114         }
115     }
116 
117     public void received(IncomingDatagramMessage msg) {
118         incomingDatagramMessages.add(msg);
119     }
120 
121     public void received(UpnpStream stream) {
122         receivedUpnpStreams.add(stream);
123     }
124 
125     public void send(OutgoingDatagramMessage msg) throws RouterException {
126         outgoingDatagramMessages.add(msg);
127     }
128 
129     public StreamResponseMessage send(StreamRequestMessage msg) throws RouterException {
130         sentStreamRequestMessages.add(msg);
131         counter++;
132         return getStreamResponseMessages() != null
133             ? getStreamResponseMessages()[counter]
134             : getStreamResponseMessage(msg);
135     }
136 
137     public void broadcast(byte[] bytes) {
138         broadcastedBytes.add(bytes);
139     }
140 
141     public void resetStreamRequestMessageCounter() {
142         counter = -1;
143     }
144 
145     public List<IncomingDatagramMessage> getIncomingDatagramMessages() {
146         return incomingDatagramMessages;
147     }
148 
149     public List<OutgoingDatagramMessage> getOutgoingDatagramMessages() {
150         return outgoingDatagramMessages;
151     }
152 
153     public List<UpnpStream> getReceivedUpnpStreams() {
154         return receivedUpnpStreams;
155     }
156 
157     public List<StreamRequestMessage> getSentStreamRequestMessages() {
158         return sentStreamRequestMessages;
159     }
160 
161     public List<byte[]> getBroadcastedBytes() {
162         return broadcastedBytes;
163     }
164 
165     public StreamResponseMessage[] getStreamResponseMessages() {
166         return null;
167     }
168 
169     public StreamResponseMessage getStreamResponseMessage(StreamRequestMessage request) {
170         return null;
171     }
172 
173 }