-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
23 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...t/src/main/scala/com.snowplowanalytics.snowplow.collectors.scalastream/PrintingSink.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2013-2023 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, and | ||
* you may not use this file except in compliance with the Apache License | ||
* Version 2.0. You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Apache License Version 2.0 is distributed on an "AS | ||
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.collectors.scalastream | ||
|
||
import cats.effect.Sync | ||
import cats.implicits._ | ||
|
||
import java.io.PrintStream | ||
import java.util.Base64 | ||
|
||
class PrintingSink[F[_]: Sync](stream: PrintStream) extends Sink[F] { | ||
private val encoder: Base64.Encoder = Base64.getEncoder.withoutPadding() | ||
|
||
override val maxBytes: Int = Int.MaxValue // TODO: configurable? | ||
override def isHealthy: F[Boolean] = Sync[F].pure(true) | ||
|
||
override def storeRawEvents(events: List[Array[Byte]], key: String): F[Unit] = | ||
events.traverse_ { event => | ||
Sync[F].delay { | ||
stream.println(encoder.encodeToString(event)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
.../scala/com.snowplowanalytics.snowplow.collectors.scalastream/sinks/PrintingSinkSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2013-2022 Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Apache License Version 2.0, and | ||
* you may not use this file except in compliance with the Apache License | ||
* Version 2.0. You may obtain a copy of the Apache License Version 2.0 at | ||
* http://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Apache License Version 2.0 is distributed on an "AS | ||
* IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the Apache License Version 2.0 for the specific language | ||
* governing permissions and limitations there under. | ||
*/ | ||
package com.snowplowanalytics.snowplow.collectors.scalastream.sinks | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import com.snowplowanalytics.snowplow.collectors.scalastream.PrintingSink | ||
import org.specs2.mutable.Specification | ||
|
||
import java.io.{ByteArrayOutputStream, PrintStream} | ||
import java.nio.charset.StandardCharsets | ||
|
||
class PrintingSinkSpec extends Specification { | ||
|
||
"Printing sink" should { | ||
"print provided bytes encoded as BASE64 string" in { | ||
val baos = new ByteArrayOutputStream() | ||
val sink = new PrintingSink[IO](new PrintStream(baos)) | ||
val input = "Something" | ||
|
||
sink.storeRawEvents(List(input.getBytes(StandardCharsets.UTF_8)), "key").unsafeRunSync() | ||
|
||
baos.toString(StandardCharsets.UTF_8) must beEqualTo("U29tZXRoaW5n\n") // base64 of 'Something' + newline | ||
} | ||
} | ||
} |