Skip to content

Commit

Permalink
CAMEL-21319: Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Oct 6, 2024
1 parent 5d58b07 commit 9955251
Showing 1 changed file with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.smpp.integration;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.component.smpp.SmppConstants;
import org.apache.camel.component.smpp.SmppMessageType;
import org.apache.camel.spi.SupervisingRouteController;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.jsmpp.examples.SMPPServerSimulator;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

@Isolated
class SmppTRXProducerSupervisingRouteControllerIT extends CamelTestSupport {
private static int PORT = 8100; // use private port to avoid reusing from previous tests
private static final CountDownLatch LATCH = new CountDownLatch(1);
private static final Thread SMPP_SERVER_SIMULATOR = new Thread(() -> {
try {
// timeout after 3 sec
LATCH.await(3, TimeUnit.SECONDS);
} catch (Exception e) {
// ignore
}
System.setProperty("jsmpp.simulator.port", "" + PORT);
SMPPServerSimulator.main(null);
});

@EndpointInject("mock:result")
private MockEndpoint result;

@EndpointInject("direct:start")
private Endpoint start;

@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();

SupervisingRouteController src = context.getRouteController().supervising();
src.setBackOffDelay(500);
src.setBackOffMaxAttempts(20);
src.setInitialDelay(100);
src.setThreadPoolSize(2);

return context;
}

@BeforeAll
public static void runSMPPServerSimulator() {
PORT = AvailablePortFinder.getNextAvailable(8100, 9999);
SMPP_SERVER_SIMULATOR.start();
}

@AfterAll
public static void stopSMPPServerSimulator() {
SMPP_SERVER_SIMULATOR.interrupt();
}

@Test
void sendSubmitSMInOnly() throws Exception {
result.expectedMessageCount(1);

Exchange exchange = start.createExchange(ExchangePattern.InOnly);
exchange.getIn().setBody("Hello SMPP World!");

template.send(start, exchange);

MockEndpoint.assertIsSatisfied(context);
Exchange resultExchange = result.getExchanges().get(0);
assertEquals(SmppMessageType.DeliveryReceipt.toString(), resultExchange.getIn().getHeader(SmppConstants.MESSAGE_TYPE));
assertEquals("Hello SMPP World!", resultExchange.getIn().getBody());
assertNotNull(resultExchange.getIn().getHeader(SmppConstants.ID));
assertEquals(1, resultExchange.getIn().getHeader(SmppConstants.SUBMITTED));
assertEquals(1, resultExchange.getIn().getHeader(SmppConstants.DELIVERED));
assertNotNull(resultExchange.getIn().getHeader(SmppConstants.DONE_DATE));
assertNotNull(resultExchange.getIn().getHeader(SmppConstants.SUBMIT_DATE));
assertNull(resultExchange.getIn().getHeader(SmppConstants.ERROR));

assertNotNull(exchange.getIn().getHeader(SmppConstants.ID));
assertEquals(1, exchange.getIn().getHeader(SmppConstants.SENT_MESSAGE_COUNT));
}

@Override
protected RoutesBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("direct:start")
.to("smpp://j@localhost:" + PORT + "?password=jpwd&systemType=producer" +
"&messageReceiverRouteId=testMessageReceiverRouteId");

from("direct:messageReceiver").id("testMessageReceiverRouteId")
.choice()
.when(simple("${header.CamelSmppSourceAddr} == '555'"))
.to("mock:garbage") // SMPPServerSimulator.run send a test message, ignore it
.otherwise()
.to("mock:result");
}
};
}
}

0 comments on commit 9955251

Please sign in to comment.