|
| 1 | +/* |
| 2 | + * TeleStax, Open Source Cloud Communications |
| 3 | + * Copyright 2011-2014, Telestax Inc and individual contributors |
| 4 | + * by the @authors tag. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation; either version 3 of |
| 9 | + * the License, or (at your option) any later version. |
| 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. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 18 | + * |
| 19 | + */ |
| 20 | +package org.restcomm.connect.mgcp; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertTrue; |
| 23 | + |
| 24 | +import org.junit.AfterClass; |
| 25 | +import org.junit.BeforeClass; |
| 26 | +import org.junit.Test; |
| 27 | +import org.mobicents.protocols.mgcp.jain.pkg.AUMgcpEvent; |
| 28 | +import org.mobicents.protocols.mgcp.jain.pkg.AUPackage; |
| 29 | +import org.restcomm.connect.commons.patterns.Observe; |
| 30 | +import org.restcomm.connect.commons.patterns.Observing; |
| 31 | +import org.restcomm.connect.commons.patterns.StopObserving; |
| 32 | + |
| 33 | +import akka.actor.ActorRef; |
| 34 | +import akka.actor.ActorSystem; |
| 35 | +import akka.actor.Props; |
| 36 | +import akka.testkit.JavaTestKit; |
| 37 | +import jain.protocol.ip.mgcp.JainMgcpEvent; |
| 38 | +import jain.protocol.ip.mgcp.JainMgcpResponseEvent; |
| 39 | +import jain.protocol.ip.mgcp.message.DeleteConnection; |
| 40 | +import jain.protocol.ip.mgcp.message.DeleteConnectionResponse; |
| 41 | +import jain.protocol.ip.mgcp.message.NotificationRequest; |
| 42 | +import jain.protocol.ip.mgcp.message.NotificationRequestResponse; |
| 43 | +import jain.protocol.ip.mgcp.message.Notify; |
| 44 | +import jain.protocol.ip.mgcp.message.parms.EventName; |
| 45 | +import jain.protocol.ip.mgcp.message.parms.ReturnCode; |
| 46 | +import jain.protocol.ip.mgcp.pkg.MgcpEvent; |
| 47 | + |
| 48 | +/** |
| 49 | + * @author maria.farooq@telestax.com (Maria Farooq) |
| 50 | + */ |
| 51 | +public class EndpointTest { |
| 52 | + private static ActorSystem system; |
| 53 | + |
| 54 | + public EndpointTest() { |
| 55 | + super(); |
| 56 | + } |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void before() throws Exception { |
| 60 | + system = ActorSystem.create(); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterClass |
| 64 | + public static void after() throws Exception { |
| 65 | + system.shutdown(); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * https://github.com/RestComm/Restcomm-Connect/issues/2310 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + public void testDeleteEnpointSuccessScenario() { |
| 74 | + new JavaTestKit(system) { |
| 75 | + { |
| 76 | + final ActorRef observer = getRef(); |
| 77 | + // Create a new mock media gateway to simulate the real thing. |
| 78 | + final ActorRef gateway = system.actorOf(new Props(MockMediaGateway.class)); |
| 79 | + // Create a media session. This is just an identifier that groups |
| 80 | + // a set of end points, connections, and lists in to one call. |
| 81 | + gateway.tell(new CreateMediaSession(), observer); |
| 82 | + final MediaGatewayResponse<MediaSession> mediaSessionResponse = expectMsgClass(MediaGatewayResponse.class); |
| 83 | + assertTrue(mediaSessionResponse.succeeded()); |
| 84 | + final MediaSession session = mediaSessionResponse.get(); |
| 85 | + // Create an IVR end point. |
| 86 | + gateway.tell(new CreateIvrEndpoint(session, "mobicents/ivr/1"), observer); |
| 87 | + final MediaGatewayResponse<ActorRef> endpointResponse = expectMsgClass(MediaGatewayResponse.class); |
| 88 | + assertTrue(endpointResponse.succeeded()); |
| 89 | + final ActorRef endpoint = endpointResponse.get(); |
| 90 | + // Start observing events from the IVR end point. |
| 91 | + endpoint.tell(new Observe(observer), observer); |
| 92 | + final Observing observingResponse = expectMsgClass(Observing.class); |
| 93 | + assertTrue(observingResponse.succeeded()); |
| 94 | + |
| 95 | + // Destroy Endpoint |
| 96 | + endpoint.tell(new DestroyEndpoint(), observer); |
| 97 | + |
| 98 | + final EndpointStateChanged endpointStateChanged = expectMsgClass(EndpointStateChanged.class); |
| 99 | + assertTrue(endpointStateChanged.getState().equals(EndpointState.DESTROYED)); |
| 100 | + // Stop observing events from the IVR end point. |
| 101 | + endpoint.tell(new StopObserving(observer), observer); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /** |
| 108 | + * https://github.com/RestComm/Restcomm-Connect/issues/2310 |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void testDeleteEnpointFailureScenario() { |
| 112 | + new JavaTestKit(system) { |
| 113 | + { |
| 114 | + final ActorRef observer = getRef(); |
| 115 | + // Create a new mock media gateway to simulate the real thing. |
| 116 | + final ActorRef gateway = system.actorOf(new Props(FailingMockMediaGateway.class)); |
| 117 | + // Create a media session. This is just an identifier that groups |
| 118 | + // a set of end points, connections, and lists in to one call. |
| 119 | + gateway.tell(new CreateMediaSession(), observer); |
| 120 | + final MediaGatewayResponse<MediaSession> mediaSessionResponse = expectMsgClass(MediaGatewayResponse.class); |
| 121 | + assertTrue(mediaSessionResponse.succeeded()); |
| 122 | + final MediaSession session = mediaSessionResponse.get(); |
| 123 | + // Create an IVR end point. |
| 124 | + gateway.tell(new CreateIvrEndpoint(session, "mobicents/ivr/1"), observer); |
| 125 | + final MediaGatewayResponse<ActorRef> endpointResponse = expectMsgClass(MediaGatewayResponse.class); |
| 126 | + assertTrue(endpointResponse.succeeded()); |
| 127 | + final ActorRef endpoint = endpointResponse.get(); |
| 128 | + // Start observing events from the IVR end point. |
| 129 | + endpoint.tell(new Observe(observer), observer); |
| 130 | + final Observing observingResponse = expectMsgClass(Observing.class); |
| 131 | + assertTrue(observingResponse.succeeded()); |
| 132 | + |
| 133 | + // Destroy Endpoint |
| 134 | + endpoint.tell(new DestroyEndpoint(), observer); |
| 135 | + |
| 136 | + final EndpointStateChanged endpointStateChanged = expectMsgClass(EndpointStateChanged.class); |
| 137 | + assertTrue(endpointStateChanged.getState().equals(EndpointState.FAILED)); |
| 138 | + // Stop observing events from the IVR end point. |
| 139 | + endpoint.tell(new StopObserving(observer), observer); |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + private static final class MockMediaGateway extends AbstractMockMediaGateway { |
| 145 | + @SuppressWarnings("unused") |
| 146 | + public MockMediaGateway() { |
| 147 | + super(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + protected void event(final Object message, final ActorRef sender) { |
| 152 | + final ActorRef self = self(); |
| 153 | + if (message instanceof JainMgcpEvent) { |
| 154 | + System.out.println(message.toString()); |
| 155 | + } |
| 156 | + final Class<?> klass = message.getClass(); |
| 157 | + if (NotificationRequest.class.equals(klass)) { |
| 158 | + // Send a successful response for this request. |
| 159 | + final NotificationRequest request = (NotificationRequest) message; |
| 160 | + final JainMgcpResponseEvent response = new NotificationRequestResponse(this, |
| 161 | + ReturnCode.Transaction_Executed_Normally); |
| 162 | + sender.tell(response, self); |
| 163 | + System.out.println(response.toString()); |
| 164 | + // Send the notification. |
| 165 | + final MgcpEvent event = AUMgcpEvent.auoc.withParm("rc=100 dc=1"); |
| 166 | + final EventName[] events = { new EventName(AUPackage.AU, event) }; |
| 167 | + final Notify notify = new Notify(this, request.getEndpointIdentifier(), request.getRequestIdentifier(), events); |
| 168 | + notify.setTransactionHandle((int) transactionIdPool.get()); |
| 169 | + sender.tell(notify, self); |
| 170 | + System.out.println(notify.toString()); |
| 171 | + }else if(DeleteConnection.class.equals(klass)){ |
| 172 | + final JainMgcpResponseEvent response = new DeleteConnectionResponse(this, |
| 173 | + ReturnCode.Transaction_Executed_Normally); |
| 174 | + sender.tell(response, self); |
| 175 | + }else{ |
| 176 | + System.out.println("inside else"); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static final class FailingMockMediaGateway extends AbstractMockMediaGateway { |
| 182 | + @SuppressWarnings("unused") |
| 183 | + public FailingMockMediaGateway() { |
| 184 | + super(); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected void event(final Object message, final ActorRef sender) { |
| 189 | + final ActorRef self = self(); |
| 190 | + if (message instanceof JainMgcpEvent) { |
| 191 | + System.out.println("event received: "+message.toString()); |
| 192 | + } |
| 193 | + final Class<?> klass = message.getClass(); |
| 194 | + if (NotificationRequest.class.equals(klass)) { |
| 195 | + // Send a successful response for this request. |
| 196 | + final NotificationRequest request = (NotificationRequest) message; |
| 197 | + final JainMgcpResponseEvent response = new NotificationRequestResponse(this, |
| 198 | + ReturnCode.Transaction_Executed_Normally); |
| 199 | + response.setTransactionHandle(request.getTransactionHandle()); |
| 200 | + sender.tell(response, self); |
| 201 | + System.out.println(response.toString()); |
| 202 | + // Send the notification. |
| 203 | + final MgcpEvent event = AUMgcpEvent.auoc.withParm("rc=300"); |
| 204 | + final EventName[] events = { new EventName(AUPackage.AU, event) }; |
| 205 | + final Notify notify = new Notify(this, request.getEndpointIdentifier(), request.getRequestIdentifier(), events); |
| 206 | + notify.setTransactionHandle((int) transactionIdPool.get()); |
| 207 | + sender.tell(notify, self); |
| 208 | + System.out.println(notify.toString()); |
| 209 | + } else if(DeleteConnection.class.equals(klass)){ |
| 210 | + final JainMgcpResponseEvent response = new DeleteConnectionResponse(this, |
| 211 | + ReturnCode.Endpoint_Unknown); |
| 212 | + sender.tell(response, self); |
| 213 | + }else{ |
| 214 | + System.out.println("inside else"); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments