Skip to content

Commit

Permalink
feature(faker-class): added faker class, config file and test cases f…
Browse files Browse the repository at this point in the history
…or generating sip data.
  • Loading branch information
TomerFi committed Aug 2, 2020
1 parent 59d661e commit 0bc4a58
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 123,7 @@ Fakers
* Robin
* RockBand
* Shakespeare
* Sip
* SlackEmoji
* Space
* StarTrek
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/github/javafaker/Faker.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 102,7 @@ public class Faker {
private final Disease disease;
private final Basketball basketball;
private final Barcode barcode;
private final Sip sip;

public Faker() {
this(Locale.ENGLISH);
Expand Down Expand Up @@ -213,6 214,7 @@ public Faker(FakeValuesService fakeValuesService, RandomService random) {
this.disease = new Disease(this);
this.basketball = new Basketball(this);
this.barcode = new Barcode(this);
this.sip = new Sip(this);
}

/**
Expand Down Expand Up @@ -667,6 669,8 @@ public Kaamelott kaamelott() {

public Barcode barcode() { return barcode; }

public Sip sip() { return sip; }

public String resolve(String key) {
return this.fakeValuesService.resolve(key, this, this);
}
Expand All @@ -690,4 694,4 @@ public String resolve(String key) {
public String expression(String expression) {
return this.fakeValuesService.expression(expression, this);
}
}
}
215 changes: 215 additions & 0 deletions src/main/java/com/github/javafaker/Sip.java
Original file line number Diff line number Diff line change
@@ -0,0 1,215 @@
package com.github.javafaker;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.util.ArrayList;

/**
* Faker class for generating Session Initiation Protocol (SIP) related data.
*
* @author TomerFi
*/
public final class Sip {
private final Faker faker;
private final ArrayList<Integer> portPool;

protected Sip(final Faker faker) {
this.faker = faker;
int port = 40000;
portPool = new ArrayList<Integer>();
while (port <= 50000) {
portPool.add(port);
port = port 2;
}
}

/**
* The various SIP methods are listed in https://en.wikipedia.org/wiki/Session_Initiation_Protocol.
*
* @return a SIP method String, e.g. {@code INVITE}.
*/
public String method() {
return faker.resolve("sip.methods");
}

/**
* Content types are based on https://tools.ietf.org/html/rfc5621 and
* https://tools.ietf.org/html/rfc3261.
*
* @return a SIP content-type declaration String, e.g. {@code application/sdp}
*/
public String contentType() {
return faker.resolve("sip.content.types");
}

/**
* Get a 4 digit random port for SIP messaging.
*
* @return a SIP messaging port int, e.g. 5060.
*/
public int messagingPort() {
return faker.random().nextInt(1000, 9999);
}

/**
* Get a 5 digit positive even port for rtp udp communication.
*
* @return an RTP UDP 5 digit port int, e.g. 40002.
*/
public int rtpPort() {
return portPool.get(faker.random().nextInt(0, portPool.size()));
}

/**
* Proviosional code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP provisioan response code between 100 and 199 int, e.g. {@code 180}.
*/
public int provisionalResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.provisional"));
}

/**
* Success code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP success response code between 200 and 299 int, e.g. {@code 200}.
*/
public int successResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.success"));
}

/**
* Redirection code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP redirection response code between 300 and 399 int, e.g. {@code 301}.
*/
public int redirectResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.redirection"));
}

/**
* Client error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP client error response code between 400 and 499 int, e.g. {@code 486}.
*/
public int clientErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.clientError"));
}

/**
* Server error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP server error response code between 500 and 599 int, e.g. {@code 503}.
*/
public int serverErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.serverError"));
}

/**
* Global error code, the various response codes are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a 3 digit SIP global error response code between 600 and 699 int, e.g. {@code 608}.
*/
public int globalErrorResponseCode() {
return Integer.parseInt(faker.resolve("sip.response.codes.globalError"));
}

/**
* Proviosional phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP provisional response phrase String, e.g. {@code Ringing}.
*/
public String provisionalResponsePhrase() {
return faker.resolve("sip.response.phrases.provisional");
}

/**
* Success phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP success response phrase String, e.g. {@code OK}.
*/
public String successResponsePhrase() {
return faker.resolve("sip.response.phrases.success");
}

/**
* Redirection phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP redirection response phrase String, e.g. {@code Moved Permanently}.
*/
public String redirectResponsePhrase() {
return faker.resolve("sip.response.phrases.redirection");
}

/**
* Client error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP client error response phrase String, e.g. {@code Busy Here}.
*/
public String clientErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.clientError");
}

/**
* Server error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP server erro response phrase String, e.g. {@code Service Unavailable}.
*/
public String serverErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.serverError");
}

/**
* Server error phrase, the various response phrases are listed in
* https://en.wikipedia.org/wiki/List_of_SIP_response_codes.
*
* @return a SIP global error response phrase String, e.g. {@code Rejected}.
*/
public String globalErrorResponsePhrase() {
return faker.resolve("sip.response.phrases.globalError");
}

/**
* Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621.
*
* @return a fake SDP type SIP body String.
*/
public String bodyString() {
return "v=0\n"
"o=" faker.name().firstName() " " faker.internet().uuid() " IN IP4 " faker.internet().domainName() "\n"
"s=-\n"
"c=IN IP4 " faker.internet().ipV4Address() "\n"
"t=0 0\n"
"m=audio " rtpPort() " RTP/AVP 0\n"
"a=rtpmap:0 PCMU/8000";
}

/**
* Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621.
*
* @return a fake SDP type SIP body byte array.
*/
public byte[] bodyBytes() {
return bodyString().getBytes(UTF_8);
}

/**
* Return a valid name address to use with {@code to/from} headers.
*
* @return a valid name address String, e.g. {@code <sip:[email protected]:5060>}.
*/
public String nameAddress() {
return "<sip:" faker.name().firstName() "@" faker.internet().ipV4Address() ":" messagingPort() ">";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 145,7 @@ public String getPath() {
"silicon_valley.yml",
"simpsons.yml",
"singular_siegler.yml",
"sip.yml",
"slack_emoji.yml",
"sonic_the_hedgehog.yml",
"source.yml",
Expand Down
101 changes: 101 additions & 0 deletions src/main/resources/en/sip.yml
Original file line number Diff line number Diff line change
@@ -0,0 1,101 @@
---
en:
faker:
sip:
methods: ["INVITE", "ACK", "BYE", "CANCEL", "REGISTER", "OPTIONS", "PRACK", "SUBSCRIBE", "NOTIFY", "PUBLISH", "INFO", "REFER", "MESSAGE", "UPDATE"]
content:
types: [
"application/sdp",
"application/resource-lists",
"application/pkcs7-mime",
"application/pkcs7-signature",
"application/x-private",
"application/xml",
"multipart/mixed",
"multipart/alternative",
"multipart/related",
"text/html"]
response:
codes:
provisional: ["100", "180", "181", "182", "183", "199"]
success: ["200", "202"]
redirection: ["300", "301", "302", "305", "380"]
clientError: [
"400",
"401",
"402",
"403",
"404",
"405",
"406",
"407",
"408",
"409",
"410",
"413",
"414",
"415",
"416",
"420",
"421",
"422",
"423",
"480",
"481",
"482",
"483",
"484",
"485",
"486",
"487",
"488",
"491",
"493"]
serverError: ["500", "501", "502", "503", "504", "505", "513", "555", "580"]
globalError: ["600", "603", "604", "606", "607"]
phrases:
provisional: ["Trying", "Ringing", "Call Is Being Forwarded", "Queued", "Session Progress", "Early Dialog Terminated"]
success: ["OK", "accepted"]
redirection: ["Multiple Choices", "Moved Permanently", "Moved Temporarily", "Use Proxy", "Alternative Service"]
clientError: [
"Bad Request",
"Unauthorized",
"Payment Required",
"Forbidden",
"Not Found",
"Method Not Allowed",
"Not Acceptable",
"Proxy Authentication Required",
"Request Timeout",
"Conflict",
"Gone",
"Request Entity Too Large",
"Request-URI Too Long",
"Unsupported Media Type",
"Unsupported URI Scheme",
"Bad Extension",
"Extension Required",
"Session Interval Too Small",
"Interval Too Brief",
"Temporarily Unavailable",
"Call/Transaction Does Not Exist",
"Loop Detected",
"Too Many Hops",
"Address Incomplete",
"Ambiguous",
"Busy Here",
"Request Terminated",
"Not Acceptable Here",
"Request Pending",
"Undecipherable"]
serverError: [
"Server Internal Error",
"Not Implemented",
"Bad Gateway",
"Service Unavailable",
"Server Time-out",
"Version Not Supported",
"Message Too Large",
"Push Notification Service Not Supported",
"Precondition Failure"]
globalError: ["Busy Everywhere", "Decline", "Does Not Exist Anywhere", "Not Acceptable", "Unwanted"]
Loading

0 comments on commit 0bc4a58

Please sign in to comment.