-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(x2): add test that X2 could be received by the Simulator.
To test that we added a REST trigger in the Simulator ("/sip") that receives SIP. It is then encoded to X2 and sent with an x2x3Client to its own X2 interface. To check if the correct X2 has been received we store the received X2 in then X2X3Memory component, which we then query with an also new REST call "/x2/last". Co-authored-by: Sven Uhlig <[email protected]>
- Loading branch information
1 parent
50e45f8
commit 02f06b7
Showing
8 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/sipgate/li/simulator/controller/SipController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.sipgate.li.simulator.controller; | ||
|
||
import static com.sipgate.li.lib.x2x3.PduObject.MANDATORY_HEADER_LENGTH; | ||
|
||
import com.sipgate.li.lib.x2x3.PayloadDirection; | ||
import com.sipgate.li.lib.x2x3.PayloadFormat; | ||
import com.sipgate.li.lib.x2x3.PduObject; | ||
import com.sipgate.li.lib.x2x3.PduType; | ||
import com.sipgate.li.lib.x2x3.X2X3Client; | ||
import com.sipgate.li.simulator.x2x3.X2X3Server; | ||
import com.sipgate.util.SSLContextBuilder; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.security.KeyManagementException; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.NoSuchProviderException; | ||
import java.security.UnrecoverableKeyException; | ||
import java.security.cert.CertificateException; | ||
import java.util.UUID; | ||
import javax.net.ssl.SSLContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class SipController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SipController.class); | ||
|
||
private final X2X3Server localX2X3Server; | ||
|
||
public SipController(final X2X3Server localX2X3Server) { | ||
this.localX2X3Server = localX2X3Server; | ||
} | ||
|
||
@PostMapping("/sip") | ||
public ResponseEntity<String> interceptSip(@RequestBody final String sip) throws Exception { | ||
LOGGER.debug("interceptSip: {}", sip); | ||
try (final var x2X3Client = makeX2X3Client()) { | ||
final byte[] bytes = sip.getBytes(); | ||
final var request = new PduObject( | ||
(short) 0, | ||
(short) 5, | ||
PduType.X2_PDU, | ||
MANDATORY_HEADER_LENGTH, | ||
bytes.length, | ||
PayloadFormat.SIP, | ||
PayloadDirection.SENT_FROM_TARGET, | ||
UUID.randomUUID(), | ||
new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, | ||
new byte[] {}, | ||
bytes | ||
); | ||
x2X3Client.send(request); | ||
return ResponseEntity.ok("null"); | ||
} | ||
} | ||
|
||
private SSLContext makeFlippedS1X2X3SslContext() | ||
throws UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, NoSuchProviderException, KeyManagementException { | ||
return SSLContextBuilder.newBuilder() | ||
.withKeyStore(Path.of("/mutual-tls-stores/network-element-keystore.p12"), "changeit") | ||
.withTrustStore(Path.of("/mutual-tls-stores/network-element-truststore.jks"), "changeit") | ||
.build(); | ||
} | ||
|
||
private X2X3Client makeX2X3Client() | ||
throws IOException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, KeyManagementException { | ||
LOGGER.info("Attempting to create local connection, port:{}", localX2X3Server.getPort()); | ||
final SSLContext sslContext = makeFlippedS1X2X3SslContext(); | ||
return new X2X3Client(sslContext.getSocketFactory(), "localhost", localX2X3Server.getPort()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/sipgate/li/simulator/controller/X2Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sipgate.li.simulator.controller; | ||
|
||
import com.sipgate.li.lib.x2x3.PduObject; | ||
import com.sipgate.li.simulator.x2x3.X2X3Memory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/x2") | ||
public class X2Controller { | ||
|
||
private final X2X3Memory x2X3Memory; | ||
|
||
public X2Controller(final X2X3Memory x2X3Memory) { | ||
this.x2X3Memory = x2X3Memory; | ||
} | ||
|
||
@GetMapping("/last") | ||
public ResponseEntity<PduObject> getLast() { | ||
if (x2X3Memory.getLast() == null) { | ||
return ResponseEntity.notFound().build(); | ||
} | ||
return ResponseEntity.ok(x2X3Memory.getLast()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/sipgate/li/simulator/x2x3/X2X3Memory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sipgate.li.simulator.x2x3; | ||
|
||
import com.sipgate.li.lib.x2x3.PduObject; | ||
import com.sipgate.li.simulator.event.X2X3ReceivedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class X2X3Memory { | ||
|
||
private PduObject lastMessage = null; | ||
|
||
public PduObject getLast() { | ||
return lastMessage; | ||
} | ||
|
||
@EventListener(X2X3ReceivedEvent.class) | ||
public void onX2X3Received(final X2X3ReceivedEvent event) { | ||
lastMessage = event.pduObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/test/java/com/sipgate/li/simulator/e2e/InterceptsX2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sipgate.li.simulator.e2e; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.sipgate.li.lib.x2x3.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(SimulatorClientExtension.class) | ||
class InterceptsX2Test { | ||
|
||
@Test | ||
void simulator_receives_x2(final SimulatorClient simulatorClient) throws Exception { | ||
// GIVEN | ||
final String body = "INVITE sip:[email protected]\n"; | ||
|
||
// WHEN | ||
simulatorClient.post("/sip", body, Void.class); | ||
|
||
// THEN | ||
final PduObject result = simulatorClient.get("/x2/last", PduObject.class); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.payload()).isEqualTo(body.getBytes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters