Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To review initial code commit #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.rakshith</groupId>
<artifactId>usermanagement</artifactId>
<version>1.0-SNAPSHOT</version>
<name>usermanagement</name>
<packaging>pom</packaging>
<modules>
<module>user-service</module>
<module>user-actors</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
141 changes: 141 additions & 0 deletions user-actors/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>user-actors</artifactId>
<version>1.0-SNAPSHOT</version>
<name>user-actors</name>
<parent>
<artifactId>usermanagement</artifactId>
<groupId>org.rakshith</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<play2.version>2.7.2</play2.version>
<play2.plugin.version>1.0.0-rc5</play2.plugin.version>
<scala.major.version>2.11</scala.major.version>
</properties>
<dependencies>
<dependency>
<groupId>net.debasishg</groupId>
<artifactId>redisclient_2.11</artifactId>
<version>3.30</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play_${scala.major.version}</artifactId>
<version>${play2.version}</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.typesafe.play</groupId>
<artifactId>play-guice_${scala.major.version}</artifactId>
<version>${play2.version}</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.12</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.major.version}</artifactId>
<version>3.0.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalamock</groupId>
<artifactId>scalamock_${scala.major.version}</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_${scala.major.version}</artifactId>
<version>2.5.22</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.4.0</version>
<configuration>
<scalaVersion>2.11.12</scalaVersion>
<checkMultipleScalaVersions>false</checkMultipleScalaVersions>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>1.4.1</version>
<configuration>
<scalaVersion>2.11.12</scalaVersion>
<aggregate>true</aggregate>
<highlighting>true</highlighting>
</configuration>
</plugin>
</plugins>
</build>
</project>
46 changes: 46 additions & 0 deletions user-actors/src/main/org/rakshith/actors/TimeActor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.rakshith.actors

import akka.actor.Actor
import play.api.libs.json.{Json, OWrites}
import java.text.SimpleDateFormat
import java.util.{Calendar, TimeZone}

case class getTimeZone(timeZone: String)
case class currentTime(time: String,timezone: String)

class TimeActor extends Actor{

implicit val writes: OWrites[currentTime] = Json.writes[currentTime]

override def receive(): Receive ={
case getTimeZone(timeZone) if timeZone=="IST" =>
val time = Json.stringify(Json.toJson(currentTime(getTimeFromTimeZone("IST"),timeZone)))
sender() ! jsonConversion(time)
case getTimeZone(timeZone) if timeZone=="EST" =>
val time = Json.stringify(Json.toJson(currentTime(getTimeFromTimeZone("EST"),timeZone)))
sender() ! jsonConversion(time)
case getTimeZone(timeZone) if timeZone=="GMT" =>
val time = Json.stringify(Json.toJson(currentTime(getTimeFromTimeZone("GMT"),timeZone)))
sender() ! jsonConversion(time)
case getTimeZone(timeZone) if timeZone=="ALL" =>
val timeZones = Array("IST","EST","GMT")
val allTime = timeZones.map(a => Json.stringify(Json.toJson(currentTime(getTimeFromTimeZone(a),a))))
sender() ! jsonConversion(allTime.mkString(","))
case _ => sender() ! "Invalid Timezone"
}

def getTimeFromTimeZone(timeZone: String): String = {
val cal = Calendar.getInstance()
TimeZone.setDefault(TimeZone.getTimeZone(timeZone))
cal.setTimeZone(TimeZone.getTimeZone(timeZone))
val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
val currentTime = cal.getTime
val dateFormat = formatter.format(currentTime)
dateFormat
}

def jsonConversion(time: String) : String ={
val jsonString: String = "{"+"\""+"Current Time"+"\""+":"+s"$time"+"}"
jsonString
}
}
102 changes: 102 additions & 0 deletions user-actors/src/main/org/rakshith/actors/UserActor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.rakshith.actors

import akka.actor.Actor
import com.redis.RedisClient
import com.typesafe.config.{Config, ConfigFactory}
import play.api.libs.json.{JsValue, Json, OWrites}

case class user(action : String,body :JsValue)
case class createUser(phoneNumber : String,name : String,age : Int,location : String)

class UserActor extends Actor{

implicit val writes: OWrites[createUser] = Json.writes[createUser]

override def receive() : Receive = {
case user(action,body) if action == "create" =>
sender() ! create(body)
case user(action,body) if action == "delete" =>
sender() ! delete(body)
case user(action,body) if action == "read" =>
sender() ! read(body)
case user(action,body) if action == "update" =>
sender() ! update(body)
}

val conf: Config = ConfigFactory.load
val hostName : String = conf.getString("redis-host")
val port :Int = conf.getInt("redis-port")
val redis = new RedisClient(hostName,port)

private def create(body: JsValue) : String ={
val phoneNumber : String = (body \ "phoneNumber").as[String]
if(!(redis.exists(phoneNumber))){
val name : String = (body \ "name").as[String]
val age : Int = (body \ "age").as[Int]
val location : String = (body \ "location").as[String]
val userDetails = Json.stringify(Json.toJson(createUser(phoneNumber,name,age,location)))
redis.set(phoneNumber,userDetails)
"User created successfully"
}
else
{
"User already exist"
}
}

private def delete(body:JsValue): String ={
val phoneNumber : String = (body \ "phoneNumber").as[String]
if(redis.exists(phoneNumber)){
redis.del(phoneNumber)
"Deleted user details successfully"
}
else
{
"User do not exist"
}
}

private def read(body:JsValue) : String ={
val phoneNumber : String = (body \ "phoneNumber").as[String]
if(redis.exists(phoneNumber)){
val userDetails = redis.get(phoneNumber)
show(userDetails)
}
else
{
"User do not exist"
}
}

private def update(body:JsValue) : String ={
val phoneNumber : String = (body \ "phoneNumber").as[String]
val newName : String = (body \ "name").as[String]
val newAge : Int = (body \ "age").as[Int]
val newLocation : String = (body \ "location").as[String]

if(redis.exists(phoneNumber)){
val details = redis.get(phoneNumber)
val exactDetails = show(details)
val parsedDetails = Json.parse(exactDetails)
val oldName : String = (parsedDetails \ "name").as[String]
val oldAge : Int = (parsedDetails \ "age").as[Int]
val oldLocation : String = (parsedDetails \ "location").as[String]
val userDetails = Json.stringify(Json.toJson(createUser(phoneNumber,newName,newAge,newLocation)))
redis.set(phoneNumber,userDetails)
s"Update user details from $oldName, $oldAge, $oldLocation to $newName, $newAge, $newLocation"
}
else
{
"User do not exist"
}

}

def show(x: Option[String]): String = x match {
case Some(s) => s
case None => "?"
}

}


3 changes: 3 additions & 0 deletions user-actors/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#redis DB configuration details
redis-host="127.0.0.1"
redis-port=6379
50 changes: 50 additions & 0 deletions user-actors/src/test/org/rakshith/actors/TestTimeActor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.rakshith.actors

import akka.actor.ActorSystem
import akka.testkit.{DefaultTimeout, ImplicitSender, TestActorRef, TestKit}
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}
import java.text.SimpleDateFormat
import java.util.{Calendar, TimeZone}

class TestTimeActor extends TestKit(ActorSystem("UserActorTests")) with ImplicitSender with DefaultTimeout with WordSpecLike with BeforeAndAfterAll with Matchers{
def getTime(timezone : String): String ={
val cal = Calendar.getInstance()
TimeZone.setDefault(TimeZone.getTimeZone(timezone))
cal.setTimeZone(TimeZone.getTimeZone(timezone))
val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
val currentTime = cal.getTime
val dateFormat = formatter.format(currentTime)
dateFormat
}
"Sending a timezone message to TimeActor" should {
"display the IST time when timezone is IST" in {
val testActor = TestActorRef[TimeActor]
testActor ! getTimeZone("IST")
val time = getTime("IST")
expectMsg("{"+ "\""+"Current Time"+"\"" +":"+ "{"+ "\""+"time"+"\""+":"+"\""+s"$time"+"\""+"," +"\""+"timezone"+"\""+":"+ "\""+"IST"+"\""+"}}"
)
}

"display the EST time when timezone is EST" in {
val testActor = TestActorRef[TimeActor]
testActor ! getTimeZone("EST")
val time = getTime("EST")
expectMsg("{"+ "\""+"Current Time"+"\"" +":"+ "{"+ "\""+"time"+"\""+":"+"\""+s"$time"+"\""+"," +"\""+"timezone"+"\""+":"+ "\""+"EST"+"\""+"}}"
)
}

"display the GMT time when timezone is GMT" in {
val testActor = TestActorRef[TimeActor]
testActor ! getTimeZone("GMT")
val time = getTime("GMT")
expectMsg("{"+ "\""+"Current Time"+"\"" +":"+ "{"+ "\""+"time"+"\""+":"+"\""+s"$time"+"\""+"," +"\""+"timezone"+"\""+":"+ "\""+"GMT"+"\""+"}}"
)
}

"display the invalid timezone when timezone is not IST or EST or GMT" in {
val testActor = TestActorRef[TimeActor]
testActor ! getTimeZone("UTC")
expectMsg("Invalid Timezone")
}
}
}
Loading