Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/systemtests/SessionTest.java

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.qpid.proton.systemtests;
20
21 import static java.util.EnumSet.of;
22 import static org.apache.qpid.proton.engine.EndpointState.ACTIVE;
23 import static org.apache.qpid.proton.engine.EndpointState.UNINITIALIZED;
24 import static org.apache.qpid.proton.systemtests.TestLoggingHelper.bold;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.logging.Logger;
33
34 import org.apache.qpid.proton.Proton;
35 import org.apache.qpid.proton.amqp.Symbol;
36 import org.junit.Test;
37
38 public class SessionTest extends EngineTestBase
39 {
40 private static final Logger LOGGER = Logger.getLogger(SessionTest.class.getName());
41
42 @Test
43 public void testCapabilities() throws Exception
44 {
45 final Symbol clientOfferedCap = Symbol.valueOf("clientOfferedCapability");
46 final Symbol clientDesiredCap = Symbol.valueOf("clientDesiredCapability");
47 final Symbol serverOfferedCap = Symbol.valueOf("serverOfferedCapability");
48 final Symbol serverDesiredCap = Symbol.valueOf("serverDesiredCapability");
49
50 Symbol[] clientOfferedCapabilities = new Symbol[] { clientOfferedCap };
51 Symbol[] clientDesiredCapabilities = new Symbol[] { clientDesiredCap };
52
53 Symbol[] serverOfferedCapabilities = new Symbol[] { serverOfferedCap };
54 Symbol[] serverDesiredCapabilities = new Symbol[] { serverDesiredCap };
55
56 LOGGER.fine(bold("======== About to create transports"));
57
58 getClient().transport = Proton.transport();
59 ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX);
60
61 getServer().transport = Proton.transport();
62 ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX);
63
64 doOutputInputCycle();
65
66 getClient().connection = Proton.connection();
67 getClient().transport.bind(getClient().connection);
68
69 getServer().connection = Proton.connection();
70 getServer().transport.bind(getServer().connection);
71
72 LOGGER.fine(bold("======== About to open connections"));
73 getClient().connection.open();
74 getServer().connection.open();
75
76 doOutputInputCycle();
77
78 LOGGER.fine(bold("======== About to open sessions"));
79 getClient().session = getClient().connection.session();
80
81 // Set the client session capabilities
82 getClient().session.setOfferedCapabilities(clientOfferedCapabilities);
83 getClient().session.setDesiredCapabilities(clientDesiredCapabilities);
84
85 getClient().session.open();
86
87 pumpClientToServer();
88
89 getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
90 assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE);
91
92 // Set the server session capabilities
93 getServer().session.setOfferedCapabilities(serverOfferedCapabilities);
94 getServer().session.setDesiredCapabilities(serverDesiredCapabilities);
95
96 getServer().session.open();
97 assertEndpointState(getServer().session, ACTIVE, ACTIVE);
98
99 pumpServerToClient();
100
101 // Verify server side got the clients session capabilities as expected
102 Symbol[] serverRemoteOfferedCapabilities = getServer().session.getRemoteOfferedCapabilities();
103 assertNotNull("Server had no remote offered capabilities", serverRemoteOfferedCapabilities);
104 assertEquals("Server remote offered capabilities not expected size", 1, serverRemoteOfferedCapabilities.length);
105 assertTrue("Server remote offered capabilities lack expected value: " + clientOfferedCap, Arrays.asList(serverRemoteOfferedCapabilities).contains(clientOfferedCap));
106
107 Symbol[] serverRemoteDesiredCapabilities = getServer().session.getRemoteDesiredCapabilities();
108 assertNotNull("Server had no remote desired capabilities", serverRemoteDesiredCapabilities);
109 assertEquals("Server remote desired capabilities not expected size", 1, serverRemoteDesiredCapabilities.length);
110 assertTrue("Server remote desired capabilities lack expected value: " + clientDesiredCap, Arrays.asList(serverRemoteDesiredCapabilities).contains(clientDesiredCap));
111
112 // Verify the client side got the servers session capabilities as expected
113 Symbol[] clientRemoteOfferedCapabilities = getClient().session.getRemoteOfferedCapabilities();
114 assertNotNull("Client had no remote offered capabilities", clientRemoteOfferedCapabilities);
115 assertEquals("Client remote offered capabilities not expected size", 1, clientRemoteOfferedCapabilities.length);
116 assertTrue("Client remote offered capabilities lack expected value: " + serverOfferedCap, Arrays.asList(clientRemoteOfferedCapabilities).contains(serverOfferedCap));
117
118 Symbol[] clientRemoteDesiredCapabilities = getClient().session.getRemoteDesiredCapabilities();
119 assertNotNull("Client had no remote desired capabilities", clientRemoteDesiredCapabilities);
120 assertEquals("Client remote desired capabilities not expected size", 1, clientRemoteDesiredCapabilities.length);
121 assertTrue("Client remote desired capabilities lack expected value: " + serverDesiredCap, Arrays.asList(clientRemoteDesiredCapabilities).contains(serverDesiredCap));
122 }
123
124 @Test
125 public void testProperties() throws Exception
126 {
127 final Symbol clientPropName = Symbol.valueOf("ClientPropName");
128 final Integer clientPropValue = 1234;
129 final Symbol serverPropName = Symbol.valueOf("ServerPropName");
130 final Integer serverPropValue = 5678;
131
132 Map<Symbol, Object> clientProps = new HashMap<>();
133 clientProps.put(clientPropName, clientPropValue);
134
135 Map<Symbol, Object> serverProps = new HashMap<>();
136 serverProps.put(serverPropName, serverPropValue);
137
138 LOGGER.fine(bold("======== About to create transports"));
139
140 getClient().transport = Proton.transport();
141 ProtocolTracerEnabler.setProtocolTracer(getClient().transport, TestLoggingHelper.CLIENT_PREFIX);
142
143 getServer().transport = Proton.transport();
144 ProtocolTracerEnabler.setProtocolTracer(getServer().transport, " " + TestLoggingHelper.SERVER_PREFIX);
145
146 doOutputInputCycle();
147
148 getClient().connection = Proton.connection();
149 getClient().transport.bind(getClient().connection);
150
151 getServer().connection = Proton.connection();
152 getServer().transport.bind(getServer().connection);
153
154 LOGGER.fine(bold("======== About to open connections"));
155 getClient().connection.open();
156 getServer().connection.open();
157
158 doOutputInputCycle();
159
160 LOGGER.fine(bold("======== About to open sessions"));
161 getClient().session = getClient().connection.session();
162
163 // Set the client session properties
164 getClient().session.setProperties(clientProps);
165
166 getClient().session.open();
167
168 pumpClientToServer();
169
170 getServer().session = getServer().connection.sessionHead(of(UNINITIALIZED), of(ACTIVE));
171 assertEndpointState(getServer().session, UNINITIALIZED, ACTIVE);
172
173 // Set the server session properties
174 getServer().session.setProperties(serverProps);
175
176 getServer().session.open();
177
178 assertEndpointState(getServer().session, ACTIVE, ACTIVE);
179
180 pumpServerToClient();
181
182 assertEndpointState(getClient().session, ACTIVE, ACTIVE);
183
184 // Verify server side got the clients session properties as expected
185 Map<Symbol, Object> serverRemoteProperties = getServer().session.getRemoteProperties();
186 assertNotNull("Server had no remote properties", serverRemoteProperties);
187 assertEquals("Server remote properties not expected size", 1, serverRemoteProperties.size());
188 assertTrue("Server remote properties lack expected key: " + clientPropName, serverRemoteProperties.containsKey(clientPropName));
189 assertEquals("Server remote properties contain unexpected value for key: " + clientPropName, clientPropValue, serverRemoteProperties.get(clientPropName));
190
191 // Verify the client side got the servers session properties as expected
192 Map<Symbol, Object> clientRemoteProperties = getClient().session.getRemoteProperties();
193 assertNotNull("Client had no remote properties", clientRemoteProperties);
194 assertEquals("Client remote properties not expected size", 1, clientRemoteProperties.size());
195 assertTrue("Client remote properties lack expected key: " + serverPropName, clientRemoteProperties.containsKey(serverPropName));
196 assertEquals("Client remote properties contain unexpected value for key: " + serverPropName, serverPropValue, clientRemoteProperties.get(serverPropName));
197 }
198 }