Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Add tests #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@
<artifactId>logback-classic</artifactId>
<version>1.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.22</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/twilio/Configuration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.twilio;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.lang.StringUtils.*;

public class Configuration {

private String accountSid;
private String apiKey;
private String apiSecret;
private String notificationServiceSid;
private String chatServiceSid;
private String syncServiceSid;

public Configuration() {
accountSid = System.getenv("TWILIO_ACCOUNT_SID");
apiKey = System.getenv("TWILIO_API_KEY");
apiSecret = System.getenv("TWILIO_API_SECRET");
notificationServiceSid = System.getenv("TWILIO_NOTIFICATION_SERVICE_SID");
chatServiceSid = System.getenv("TWILIO_CHAT_SERVICE_SID");
syncServiceSid = System.getenv("TWILIO_SYNC_SERVICE_SID");
}

public String getAccountSid() {
return accountSid;
}

public String getApiKey() {
return apiKey;
}

public String getApiSecret() {
return apiSecret;
}

public String getNotificationServiceSid() {
return notificationServiceSid;
}

public String getChatServiceSid() {
return chatServiceSid;
}

public String getSyncServiceSid() {
return syncServiceSid;
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put("TWILIO_ACCOUNT_SID", getAccountSid());
map.put("TWILIO_API_KEY", getApiKey());
map.put("TWILIO_API_SECRET", !isEmpty(getApiKey()));
map.put("TWILIO_NOTIFICATION_SERVICE_SID", getNotificationServiceSid());
map.put("TWILIO_CHAT_SERVICE_SID", getChatServiceSid());
map.put("TWILIO_SYNC_SERVICE_SID", getSyncServiceSid());
return map;
}
}
194 changes: 9 additions & 185 deletions src/main/java/com/twilio/ServerApp.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
package com.twilio;

import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.staticFileLocation;
import static spark.Spark.afterAfter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.javafaker.Faker;
import com.google.gson.Gson;

import com.twilio.jwt.accesstoken.*;
import com.twilio.rest.notify.v1.service.BindingCreator;
import com.twilio.rest.notify.v1.service.Binding;
import com.twilio.rest.notify.v1.service.Notification;

import com.twilio.controller.TwilioController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static spark.Spark.*;


public class ServerApp {

final static Logger logger = LoggerFactory.getLogger(ServerApp.class);

private class BindingRequest {
String endpoint;
String BindingType;
String Address;
}

private static class BindingResponse {
String message;
String error;
}
private TwilioController twilioController;

private static class SendNotificationResponse {
String message;
String error;
}


public static void main(String[] args) {
Expand All @@ -51,168 +24,19 @@ public static void main(String[] args) {
// Create a Faker instance to generate a random username for the connecting user
Faker faker = new Faker();


// Set up configuration from environment variables
Map<String, String> configuration = new HashMap<>();
configuration.put("TWILIO_ACCOUNT_SID", System.getenv("TWILIO_ACCOUNT_SID"));
configuration.put("TWILIO_API_KEY", System.getenv("TWILIO_API_KEY"));
configuration.put("TWILIO_API_SECRET", System.getenv("TWILIO_API_SECRET"));
configuration.put("TWILIO_NOTIFICATION_SERVICE_SID", System.getenv("TWILIO_NOTIFICATION_SERVICE_SID"));
configuration.put("TWILIO_CHAT_SERVICE_SID",System.getenv("TWILIO_CHAT_SERVICE_SID"));
configuration.put("TWILIO_SYNC_SERVICE_SID",System.getenv("TWILIO_SYNC_SERVICE_SID"));
TwilioController twilioController = new TwilioController();

// Log all requests and responses
afterAfter(new LoggingFilter());

// Get the configuration for variables for the health check
get("/config", (request, response) -> {

Map<String, Object> json = new HashMap<>();
String apiSecret = configuration.get("TWILIO_API_SECRET");
boolean apiSecretConfigured = (apiSecret != null) && !apiSecret.isEmpty();

json.putAll(configuration);
json.put("TWILIO_API_SECRET", apiSecretConfigured);


// Render JSON response
Gson gson = new Gson();
response.type("application/json");
return gson.toJson(json);
});
get("/config", twilioController.configRoute);

// Create an access token using our Twilio credentials
get("/token", "application/json", (request, response) -> {
// Generate a random username for the connecting client
String identity = faker.firstName() + faker.lastName() + faker.zipCode();
get("/token", "application/json", twilioController.tokenRoute);

// Create an endpoint ID which uniquely identifies the user on their current device
String appName = "TwilioAppDemo";
post("/register", twilioController.registerRoute);

// Create access token builder
AccessToken.Builder builder = new AccessToken.Builder(
configuration.get("TWILIO_ACCOUNT_SID"),
configuration.get("TWILIO_API_KEY"),
configuration.get("TWILIO_API_SECRET")
).identity(identity);

List<Grant> grants = new ArrayList<>();

// Add Sync grant if configured
if (configuration.containsKey("TWILIO_SYNC_SERVICE_SID")) {
SyncGrant grant = new SyncGrant();
grant.setServiceSid(configuration.get("TWILIO_SYNC_SERVICE_SID"));
grants.add(grant);
}

// Add Chat grant if configured
if (configuration.containsKey("TWILIO_CHAT_SERVICE_SID")) {
IpMessagingGrant grant = new IpMessagingGrant();
grant.setServiceSid(configuration.get("TWILIO_CHAT_SERVICE_SID"));
grants.add(grant);
}

// Add Video grant
VideoGrant grant = new VideoGrant();
grants.add(grant);

builder.grants(grants);

AccessToken token = builder.build();


// create JSON response payload
HashMap<String, String> json = new HashMap<String, String>();
json.put("identity", identity);
json.put("token", token.toJwt());

// Render JSON response
Gson gson = new Gson();
response.type("application/json");
return gson.toJson(json);
});


post("/register", (request, response) -> {

// Authenticate with Twilio
Twilio.init(configuration.get("TWILIO_API_KEY"),configuration.get("TWILIO_API_SECRET"),configuration.get("TWILIO_ACCOUNT_SID"));

logger.debug(request.body());

// Decode the JSON Body
Gson gson = new Gson();
BindingRequest bindingRequest = gson.fromJson(request.body(), BindingRequest.class);


// Create a binding
Binding.BindingType bindingType = Binding.BindingType.forValue(bindingRequest.BindingType);
BindingCreator creator = Binding.creator(configuration.get("TWILIO_NOTIFICATION_SERVICE_SID"),
bindingRequest.endpoint,
bindingType,
bindingRequest.Address);

try {
Binding binding = creator.create();
logger.info("Binding successfully created");
logger.debug(binding.toString());

// Send a JSON response indicating success
BindingResponse bindingResponse = new BindingResponse();
bindingResponse.message = "Binding Created";
response.type("application/json");
return gson.toJson(bindingResponse);

} catch (Exception ex) {
logger.error("Exception creating binding: " + ex.getMessage(), ex);

// Send a JSON response indicating an error
BindingResponse bindingResponse = new BindingResponse();
bindingResponse.message = "Failed to create binding: " + ex.getMessage();
bindingResponse.error = ex.getMessage();
response.type("application/json");
response.status(500);
return gson.toJson(bindingResponse);
}
});

post("/send-notification", (request, response) -> {

// Authenticate with Twilio
Twilio.init(configuration.get("TWILIO_API_KEY"),configuration.get("TWILIO_API_SECRET"),configuration.get("TWILIO_ACCOUNT_SID"));

try {
// Get the identity
String identity = request.raw().getParameter("identity");
logger.info("Identity: " + identity);

// Create the notification
String serviceSid = configuration.get("TWILIO_NOTIFICATION_SERVICE_SID");
Notification notification = Notification
.creator(serviceSid)
.setBody("Hello " + identity)
.setIdentity(identity)
.create();
logger.info("Notification successfully created");
logger.debug(notification.toString());

// Send a JSON response indicating success
SendNotificationResponse sendNotificationResponse = new SendNotificationResponse();
sendNotificationResponse.message = "Notification Created";
response.type("application/json");
return new Gson().toJson(sendNotificationResponse);

} catch (Exception ex) {
logger.error("Exception creating notification: " + ex.getMessage(), ex);

// Send a JSON response indicating an error
SendNotificationResponse sendNotificationResponse = new SendNotificationResponse();
sendNotificationResponse.message = "Failed to create notification: " + ex.getMessage();
sendNotificationResponse.error = ex.getMessage();
response.type("application/json");
response.status(500);
return new Gson().toJson(sendNotificationResponse);
}
});
post("/send-notification", twilioController.sendNotificationRoute);
}
}
Loading