1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api
21 import akka.actor.typed.ActorSystem
22 import akka.serialization.{Serialization, SerializationExtension}
23 
24 import java.nio.ByteOrder
25 import stamina.ByteString
26 
27 object StaminaTestUtils {
28 
29 //  copied from org.apache.logging.log4j.core.config.plugins.convert.HexConverter
30   @SuppressWarnings(Array("org.wartremover.warts.While"))
31   private def parseHexBinary(s: String): Array[Byte] = {
32     val len = s.length
33     val data = new Array[Byte](len / 2)
34     var i = 0
35     while ({ i < len }) {
36       data(i / 2) = ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16)).toByte
37       i += 2
38     }
39     data
40   }
41 
42   private val initialCharactersToSkip: Int = 2
43 
44   @SuppressWarnings(Array("org.wartremover.warts.TryPartial"))
45   private def makeEventSerializer(implicit system: ActorSystem[_]) =
46     SerializationExtension(system)
47       .serializerOf(new Serialization.Settings(system.settings.config).Serializers("make-serializer"))
48       .get
49 
50   @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
51   def deserializeEventFromJson[A](eventKey: String, eventAsJsonString: String, version: Int = 1)(
52     implicit system: ActorSystem[_]
53   ): A = {
54     implicit val byteOrder: ByteOrder = java.nio.ByteOrder.LITTLE_ENDIAN
55     val bytes: Array[Byte] = ByteString.newBuilder
56       .putInt(eventKey.length)
57       .putBytes(eventKey.getBytes("UTF-8"))
58       .putInt(version)
59       .append(ByteString(eventAsJsonString))
60       .result()
61       .toArray
62 
63     makeEventSerializer.fromBinary(bytes).asInstanceOf[A]
64   }
65 
66   @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf"))
67   def deserializeEventFromHexa[A](serialized: String)(implicit system: ActorSystem[_]): A = {
68     val bytes: Array[Byte] = parseHexBinary(serialized.substring(initialCharactersToSkip))
69 
70     makeEventSerializer.fromBinary(bytes).asInstanceOf[A]
71   }
72 
73   def getVersionFromHexa(serialized: String): Int = {
74     val bytes: Array[Byte] = parseHexBinary(serialized.substring(initialCharactersToSkip))
75     val start = bytes.take(4)
76     val skip = start.map(_.toInt).sum
77 
78     bytes.slice(skip + 4, skip + 8).map(_.toInt).sum
79   }
80 
81   def getEventNameFromHexa(serialized: String): String = {
82     val bytes: Array[Byte] = parseHexBinary(serialized.substring(initialCharactersToSkip))
83     val start = bytes.take(4)
84     val skip = start.map(_.toInt).sum
85     new String(bytes.slice(4, skip + 4), "UTF-8")
86   }
87 
88 }
Line Stmt Id Pos Tree Symbol Tests Code
32 51567 1160 - 1168 Apply java.lang.String.length org.make.api.userhistory.userhistorytest s.length()
33 51695 1184 - 1208 Apply scala.Array.<init> org.make.api.userhistory.userhistorytest new Array[Byte](len./(2))
33 51401 1200 - 1207 Apply scala.Int./ org.make.api.userhistory.userhistorytest len./(2)
34 51530 1221 - 1222 Literal <nosymbol> org.make.api.userhistory.userhistorytest 0
35 51824 1247 - 1247 Apply org.make.api.StaminaTestUtils.while$1 org.make.api.userhistory.userhistorytest while$1()
35 51569 1227 - 1227 Literal <nosymbol> org.make.api.userhistory.userhistorytest ()
35 51661 1247 - 1375 Block <nosymbol> org.make.api.userhistory.userhistorytest { { data.update(i./(2), java.lang.Character.digit(s.charAt(i), 16).<<(4).+(java.lang.Character.digit(s.charAt(i.+(1)), 16)).toByte); i = i.+(2) }; while$1() }
35 51395 1227 - 1227 Block <nosymbol> org.make.api.userhistory.userhistorytest ()
35 51807 1236 - 1243 Apply scala.Int.< org.make.api.userhistory.userhistorytest i.<(len)
36 51808 1270 - 1356 Select scala.Int.toByte org.make.api.userhistory.userhistorytest java.lang.Character.digit(s.charAt(i), 16).<<(4).+(java.lang.Character.digit(s.charAt(i.+(1)), 16)).toByte
36 51718 1260 - 1265 Apply scala.Int./ org.make.api.userhistory.userhistorytest i./(2)
36 51568 1337 - 1342 Apply scala.Int.+ org.make.api.userhistory.userhistorytest i.+(1)
36 51544 1287 - 1298 Apply java.lang.String.charAt org.make.api.userhistory.userhistorytest s.charAt(i)
36 51831 1300 - 1302 Literal <nosymbol> org.make.api.userhistory.userhistorytest 16
36 51520 1312 - 1348 Apply java.lang.Character.digit org.make.api.userhistory.userhistorytest java.lang.Character.digit(s.charAt(i.+(1)), 16)
36 51687 1345 - 1347 Literal <nosymbol> org.make.api.userhistory.userhistorytest 16
36 51660 1307 - 1308 Literal <nosymbol> org.make.api.userhistory.userhistorytest 4
36 51403 1328 - 1343 Apply java.lang.String.charAt org.make.api.userhistory.userhistorytest s.charAt(i.+(1))
36 51699 1255 - 1356 Apply scala.Array.update org.make.api.userhistory.userhistorytest data.update(i./(2), java.lang.Character.digit(s.charAt(i), 16).<<(4).+(java.lang.Character.digit(s.charAt(i.+(1)), 16)).toByte)
37 51545 1363 - 1369 Apply scala.Int.+ org.make.api.userhistory.userhistorytest i.+(2)
42 51688 1435 - 1436 Literal <nosymbol> org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest 2
47 51528 1625 - 1706 Apply scala.collection.MapOps.apply org.make.api.technical.crm.crmservicecomponenttest,org.make.api.userhistory.userhistorytest new akka.serialization.Serialization.Settings(system.settings.config).Serializers.apply("make-serializer")
48 51802 1574 - 1718 Select scala.util.Try.get org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest akka.serialization.SerializationExtension.apply(system).serializerOf(new akka.serialization.Serialization.Settings(system.settings.config).Serializers.apply("make-serializer")).get
54 51700 1970 - 2002 Select java.nio.ByteOrder.LITTLE_ENDIAN org.make.api.technical.crm.crmservicecomponenttest,org.make.api.userhistory.userhistorytest java.nio.ByteOrder.LITTLE_ENDIAN
56 51536 2068 - 2083 Apply java.lang.String.length org.make.api.technical.crm.crmservicecomponenttest,org.make.api.userhistory.userhistorytest eventKey.length()
57 51830 2101 - 2127 Apply java.lang.String.getBytes org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest eventKey.getBytes("UTF-8")
59 51653 2166 - 2195 Apply akka.util.ByteString.apply org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest stamina.`package`.ByteString.apply(eventAsJsonString)
61 51498 2032 - 2227 ApplyToImplicitArgs akka.util.ByteString.toArray org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest stamina.`package`.ByteString.newBuilder.putInt(eventKey.length())(byteOrder).putBytes(eventKey.getBytes("UTF-8")).putInt(version)(byteOrder).append(stamina.`package`.ByteString.apply(eventAsJsonString)).result().toArray[Byte]((ClassTag.Byte: scala.reflect.ClassTag[Byte]))
63 51396 2233 - 2286 TypeApply scala.Any.asInstanceOf org.make.api.technical.crm.crmservicecomponenttest,org.make.api.userhistory.userhistorytest StaminaTestUtils.this.makeEventSerializer(system).fromBinary(bytes).asInstanceOf[A]
68 51682 2516 - 2539 Select org.make.api.StaminaTestUtils.initialCharactersToSkip org.make.api.userhistory.userhistorytest StaminaTestUtils.this.initialCharactersToSkip
68 51529 2495 - 2540 Apply java.lang.String.substring org.make.api.userhistory.userhistorytest serialized.substring(StaminaTestUtils.this.initialCharactersToSkip)
68 51792 2480 - 2541 Apply org.make.api.StaminaTestUtils.parseHexBinary org.make.api.userhistory.userhistorytest StaminaTestUtils.this.parseHexBinary(serialized.substring(StaminaTestUtils.this.initialCharactersToSkip))
70 51723 2547 - 2600 TypeApply scala.Any.asInstanceOf org.make.api.userhistory.userhistorytest StaminaTestUtils.this.makeEventSerializer(system).fromBinary(bytes).asInstanceOf[A]
74 51832 2704 - 2749 Apply java.lang.String.substring org.make.api.userhistory.userhistorytest serialized.substring(StaminaTestUtils.this.initialCharactersToSkip)
74 51538 2725 - 2748 Select org.make.api.StaminaTestUtils.initialCharactersToSkip org.make.api.userhistory.userhistorytest StaminaTestUtils.this.initialCharactersToSkip
74 51672 2689 - 2750 Apply org.make.api.StaminaTestUtils.parseHexBinary org.make.api.userhistory.userhistorytest StaminaTestUtils.this.parseHexBinary(serialized.substring(StaminaTestUtils.this.initialCharactersToSkip))
75 51505 2767 - 2780 Apply scala.collection.ArrayOps.take org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(bytes).take(4)
76 51815 2796 - 2818 ApplyToImplicitArgs scala.collection.IterableOnceOps.sum org.make.api.userhistory.userhistorytest scala.Predef.wrapIntArray(scala.Predef.byteArrayOps(start).map[Int](((x$1: Byte) => x$1.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))).sum[Int](math.this.Numeric.IntIsIntegral)
76 51692 2796 - 2814 ApplyToImplicitArgs scala.collection.ArrayOps.map org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(start).map[Int](((x$1: Byte) => x$1.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))
76 51417 2806 - 2813 Select scala.Byte.toInt org.make.api.userhistory.userhistorytest x$1.toInt
76 51531 2815 - 2815 Select scala.math.Numeric.IntIsIntegral org.make.api.userhistory.userhistorytest math.this.Numeric.IntIsIntegral
78 51411 2869 - 2869 Select scala.math.Numeric.IntIsIntegral org.make.api.userhistory.userhistorytest math.this.Numeric.IntIsIntegral
78 51674 2860 - 2867 Select scala.Byte.toInt org.make.api.userhistory.userhistorytest x$2.toInt
78 51554 2846 - 2854 Apply scala.Int.+ org.make.api.userhistory.userhistorytest skip.+(8)
78 51497 2824 - 2868 ApplyToImplicitArgs scala.collection.ArrayOps.map org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(scala.Predef.byteArrayOps(bytes).slice(skip.+(4), skip.+(8))).map[Int](((x$2: Byte) => x$2.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))
78 51693 2824 - 2872 ApplyToImplicitArgs scala.collection.IterableOnceOps.sum org.make.api.userhistory.userhistorytest scala.Predef.wrapIntArray(scala.Predef.byteArrayOps(scala.Predef.byteArrayOps(bytes).slice(skip.+(4), skip.+(8))).map[Int](((x$2: Byte) => x$2.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))).sum[Int](math.this.Numeric.IntIsIntegral)
78 51827 2824 - 2855 Apply scala.collection.ArrayOps.slice org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(bytes).slice(skip.+(4), skip.+(8))
78 51648 2836 - 2844 Apply scala.Int.+ org.make.api.userhistory.userhistorytest skip.+(4)
82 51638 2966 - 3027 Apply org.make.api.StaminaTestUtils.parseHexBinary org.make.api.userhistory.userhistorytest StaminaTestUtils.this.parseHexBinary(serialized.substring(StaminaTestUtils.this.initialCharactersToSkip))
82 51524 3002 - 3025 Select org.make.api.StaminaTestUtils.initialCharactersToSkip org.make.api.userhistory.userhistorytest StaminaTestUtils.this.initialCharactersToSkip
82 51803 2981 - 3026 Apply java.lang.String.substring org.make.api.userhistory.userhistorytest serialized.substring(StaminaTestUtils.this.initialCharactersToSkip)
83 51537 3044 - 3057 Apply scala.collection.ArrayOps.take org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(bytes).take(4)
84 51667 3073 - 3091 ApplyToImplicitArgs scala.collection.ArrayOps.map org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(start).map[Int](((x$3: Byte) => x$3.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))
84 51499 3092 - 3092 Select scala.math.Numeric.IntIsIntegral org.make.api.userhistory.userhistorytest math.this.Numeric.IntIsIntegral
84 51828 3083 - 3090 Select scala.Byte.toInt org.make.api.userhistory.userhistorytest x$3.toInt
84 51406 3073 - 3095 ApplyToImplicitArgs scala.collection.IterableOnceOps.sum org.make.api.userhistory.userhistorytest scala.Predef.wrapIntArray(scala.Predef.byteArrayOps(start).map[Int](((x$3: Byte) => x$3.toInt))((ClassTag.Int: scala.reflect.ClassTag[Int]))).sum[Int](math.this.Numeric.IntIsIntegral)
85 51518 3126 - 3134 Apply scala.Int.+ org.make.api.userhistory.userhistorytest skip.+(4)
85 51539 3100 - 3145 Apply java.lang.String.<init> org.make.api.userhistory.userhistorytest new scala.Predef.String(scala.Predef.byteArrayOps(bytes).slice(4, skip.+(4)), "UTF-8")
85 51640 3137 - 3144 Literal <nosymbol> org.make.api.userhistory.userhistorytest "UTF-8"
85 51795 3111 - 3135 Apply scala.collection.ArrayOps.slice org.make.api.userhistory.userhistorytest scala.Predef.byteArrayOps(bytes).slice(4, skip.+(4))
85 51681 3123 - 3124 Literal <nosymbol> org.make.api.userhistory.userhistorytest 4