|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Snowplow Analytics Ltd. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | + |
| 14 | +package com.snowplowanalytics.snowplow.analytics.scalasdk |
| 15 | + |
| 16 | +// java |
| 17 | +import java.time.Instant |
| 18 | +import java.util.UUID |
| 19 | +import java.nio.ByteBuffer |
| 20 | +import java.nio.charset.StandardCharsets |
| 21 | + |
| 22 | +// cats |
| 23 | +import cats.data.Validated.{Invalid, Valid} |
| 24 | +import cats.data.NonEmptyList |
| 25 | +import cats.syntax.either._ |
| 26 | + |
| 27 | +// circe |
| 28 | +import io.circe.{Decoder, Encoder, Json, JsonObject} |
| 29 | +import io.circe.syntax._ |
| 30 | +import io.circe.parser._ |
| 31 | +import io.circe.generic.semiauto._ |
| 32 | +import io.circe.literal._ |
| 33 | + |
| 34 | +// Specs2 |
| 35 | +import org.specs2.mutable.Specification |
| 36 | + |
| 37 | +// Iglu |
| 38 | +import com.snowplowanalytics.iglu.core.{SchemaKey, SelfDescribingData} |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests Event case class |
| 42 | + */ |
| 43 | +class SnowplowEventSpec extends Specification { |
| 44 | + import EventSpec._ |
| 45 | + |
| 46 | + "Contexts toShreddedJson" should { |
| 47 | + "return a map of JSON entities, keyed by column name" in { |
| 48 | + |
| 49 | + val sdd1 = SelfDescribingData[Json](SchemaKey.fromUri("iglu:myvendor1/myname1/jsonschema/1-2-3").toOption.get, json"""{"xyz": 42}""") |
| 50 | + val sdd2 = |
| 51 | + SelfDescribingData[Json](SchemaKey.fromUri("iglu:myvendor2/myname2/jsonschema/2-3-4").toOption.get, json"""{"abc": true}""") |
| 52 | + |
| 53 | + val input = SnowplowEvent.Contexts(List(sdd1, sdd2)) |
| 54 | + |
| 55 | + val result = input.toShreddedJson |
| 56 | + |
| 57 | + val expected = Map( |
| 58 | + "contexts_myvendor1_myname1_1" -> json"""[{"_schema_version": "1-2-3", "xyz": 42}]""", |
| 59 | + "contexts_myvendor2_myname2_2" -> json"""[{"_schema_version": "2-3-4", "abc": true}]""" |
| 60 | + ) |
| 61 | + |
| 62 | + result must beEqualTo(expected) |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + "return a map of JSON entities for all types of JSON value (object, array, string, number, boolean, null)" in { |
| 67 | + |
| 68 | + def sdd(version: Int, v: Json): SelfDescribingData[Json] = |
| 69 | + SelfDescribingData[Json](SchemaKey.fromUri(s"iglu:myvendor/myname/jsonschema/$version-0-0").toOption.get, v) |
| 70 | + |
| 71 | + val input = SnowplowEvent.Contexts( |
| 72 | + List( |
| 73 | + sdd(1, json"""{"xyz": 123}"""), |
| 74 | + sdd(2, json"""[1, 2, 3]"""), |
| 75 | + sdd(3, json""""foo""""), |
| 76 | + sdd(4, json"""42"""), |
| 77 | + sdd(5, json"""true"""), |
| 78 | + sdd(6, json"""null""") |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + val result = input.toShreddedJson |
| 83 | + |
| 84 | + val expected = Map( |
| 85 | + "contexts_myvendor_myname_1" -> json"""[{"_schema_version": "1-0-0", "xyz": 123}]""", |
| 86 | + "contexts_myvendor_myname_2" -> json"""[[1, 2, 3]]""", |
| 87 | + "contexts_myvendor_myname_3" -> json"""["foo"]""", |
| 88 | + "contexts_myvendor_myname_4" -> json"""[42]""", |
| 89 | + "contexts_myvendor_myname_5" -> json"""[true]""", |
| 90 | + "contexts_myvendor_myname_6" -> json"""[null]""" |
| 91 | + ) |
| 92 | + |
| 93 | + result must beEqualTo(expected) |
| 94 | + |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments