Skip to content

Commit

Permalink
create a tck (#10)
Browse files Browse the repository at this point in the history
a test compatibility toolkit to test the json encoding/decoding implementation
  • Loading branch information
martinpallmann committed May 7, 2020
1 parent 8787a08 commit cd80dc7
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 148 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Expand Up @@ -40,8 +40,7 @@ lazy val tck = project
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"org.scalatest" %% "scalatest" % "3.1.1"
),
publish / skip := true
)
)

lazy val circe = project
Expand Down
@@ -1,15 +1,37 @@
/*
* Copyright 2020 Martin Pallmann
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.martinpallmann.gchat.circe

import de.martinpallmann.gchat.BotRequest
import de.martinpallmann.gchat.gen.Message
import de.martinpallmann.gchat.tck.Tck
import io.circe.Json
import io.circe.parser._

import scala.util.Try

class IntegrationTest extends Tck[Json] with BotRequestDecoder {
class IntegrationTest
extends Tck[Json]
with BotRequestDecoder
with MessageEncoder {

def parseJson: String => Try[Json] = parse(_).toTry

def decode: Json => Try[BotRequest] = _.as[BotRequest](decodeBotRequest).toTry

def encode: Message => Json = encodeMessage.apply
}
18 changes: 9 additions & 9 deletions tck/src/main/java/de/martinpallmann/gchat/tck/Reflection.java
Expand Up @@ -19,18 +19,18 @@
import java.lang.reflect.InvocationTargetException;

class Reflection {
// public static ResponseTestCase responseTestCase(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// return Class
// .forName(name)
// .asSubclass(ResponseTestCase.class)
// .getDeclaredConstructor()
// .newInstance();
// }
public static BotResponseTestCase responseTestCase(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
return Class
.forName(name)
.asSubclass(BotResponseTestCase.class)
.getDeclaredConstructor()
.newInstance();
}

public static EventTestCase eventTestCase(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
public static BotRequestTestCase requestTestCase(String name) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
return Class
.forName(name)
.asSubclass(EventTestCase.class)
.asSubclass(BotRequestTestCase.class)
.getDeclaredConstructor()
.newInstance();
}
Expand Down
3 changes: 0 additions & 3 deletions tck/src/main/resources/event/tests.txt

This file was deleted.

Expand Up @@ -9,6 +9,8 @@
"user": {
"name": "users/12345678901234567890",
"displayName": "Chuck Norris",
"type": "HUMAN",
"domainId": "domainId",
"avatarUrl": "https://lh3.googleusercontent.com/.../photo.jpg",
"email": "chuck@example.com"
}
Expand Down
3 changes: 3 additions & 0 deletions tck/src/main/resources/request/tests.txt
@@ -0,0 +1,3 @@
added_to_space
removed_from_space
message
Empty file.
@@ -0,0 +1,27 @@
/*
* Copyright 2020 Martin Pallmann
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.martinpallmann.gchat.tck

import de.martinpallmann.gchat.BotRequest

import scala.language.implicitConversions

abstract private[tck] class BotRequestTestCase {
protected final implicit def anyToOption[A](a: A): Option[A] = Option(a)
def request: BotRequest
override def toString: String = request.toString
}
Expand Up @@ -16,9 +16,9 @@

package de.martinpallmann.gchat.tck

import de.martinpallmann.gchat.BotRequest
import de.martinpallmann.gchat.gen.Message

abstract private[tck] class EventTestCase {
def event: BotRequest
override def toString: String = event.toString
abstract private[tck] class BotResponseTestCase {
def response: Message
override def toString: String = response.toString
}
44 changes: 22 additions & 22 deletions tck/src/main/scala/de/martinpallmann/gchat/tck/Tck.scala
Expand Up @@ -17,6 +17,7 @@
package de.martinpallmann.gchat.tck

import de.martinpallmann.gchat.BotRequest
import de.martinpallmann.gchat.gen.Message
import org.scalatest.funsuite.AnyFunSuite

import scala.io.Source
Expand All @@ -25,22 +26,21 @@ import scala.util.Try
trait Tck[JSON] extends AnyFunSuite {

def parseJson: String => Try[JSON]
// def encode: Response => JSON
def encode: Message => JSON
def decode: JSON => Try[BotRequest]

// tests("response").map(
// t =>
// test(s"should encode response: $t") {
// assert(readJson("response", t) == encode(response(t)))
// }
// ) ++
tests("event").map(
tests("response").map(
t =>
test(s"should decode event: $t") {
// TODO: better error handling
assert(event(t) == decode(readJson("event", t)).get)
test(s"should encode response: $t") {
assert(readJson("response", t) == encode(response(t)))
}
)
) ++
tests("request").map(
t =>
test(s"should decode bot request: $t") {
assert(request(t) == decode(readJson("request", t)).get)
}
)

private def tests(dir: String): List[String] =
Source
Expand All @@ -60,19 +60,19 @@ trait Tck[JSON] extends AnyFunSuite {
}
}

// private def response(test: String): Response =
// Reflection
// .responseTestCase(
// s"de.martinpallmann.gchat.tck.response.${toCamelCase(test)}"
// )
// .response
private def response(test: String): Message =
Reflection
.responseTestCase(
s"de.martinpallmann.gchat.tck.response.${toCamelCase(test)}TestCase"
)
.response

private def event(test: String): BotRequest =
private def request(test: String): BotRequest =
Reflection
.eventTestCase(
s"de.martinpallmann.gchat.tck.event.${toCamelCase(test)}TestCase"
.requestTestCase(
s"de.martinpallmann.gchat.tck.request.${toCamelCase(test)}TestCase"
)
.event
.request

private def toCamelCase(s: String): String = {
val toUpper: Char => Char = _.toUpper
Expand Down

This file was deleted.

Expand Up @@ -14,31 +14,29 @@
* limitations under the License.
*/

package de.martinpallmann.gchat.tck.event
package de.martinpallmann.gchat.tck.request

import java.time.Instant

import de.martinpallmann.gchat.BotRequest
import de.martinpallmann.gchat.gen._
import de.martinpallmann.gchat.BotRequest.AddedToSpace
import de.martinpallmann.gchat.tck.EventTestCase
import de.martinpallmann.gchat.tck.BotRequestTestCase

class AddedToSpaceTestCase extends EventTestCase {
def event: BotRequest = {
// AddedToSpaceEvent(
// Instant.parse("2017-03-02T19:02:59.910959Z"),
// Space(
// "spaces/AAAAAAAAAAA",
// "Chuck Norris Discussion Room",
// SpaceType.Room
// ),
// None,
// User(
// "users/12345678901234567890",
// "Chuck Norris",
// UserType.Human,
// "https://lh3.googleusercontent.com/.../photo.jpg"
// )
// )
???
}
class AddedToSpaceTestCase extends BotRequestTestCase {
def request: BotRequest =
BotRequest.AddedToSpace(
Instant.parse("2017-03-02T19:02:59.910959Z"),
Space(
name = "spaces/AAAAAAAAAAA",
displayName = "Chuck Norris Discussion Room",
`type` = SpaceType.Room
),
None,
User(
name = "users/12345678901234567890",
displayName = "Chuck Norris",
`type` = UserType.Human,
domainId = "domainId"
)
)
}
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Martin Pallmann
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.martinpallmann.gchat.tck.request

import java.time.Instant

import de.martinpallmann.gchat.BotRequest
import de.martinpallmann.gchat.gen._
import de.martinpallmann.gchat.tck.BotRequestTestCase

class MessageTestCase extends BotRequestTestCase {
def request: BotRequest =
BotRequest.MessageReceived(
Instant.parse("2017-03-02T19:02:59.910959Z"),
Space(
name = "spaces/AAAAAAAAAAA",
displayName = "Chuck Norris Discussion Room",
`type` = SpaceType.Room
),
Message(
name = "spaces/AAAAAAAAAAA/messages/CCCCCCCCCCC",
sender = User(
name = "users/12345678901234567890",
displayName = "Chuck Norris"
),
createTime = Instant.parse("2017-03-02T19:02:59.910959Z"),
text = "@TestBot Violence is my last option.",
argumentText = " Violence is my last option.",
thread = Thread(name = "spaces/AAAAAAAAAAA/threads/BBBBBBBBBBB"),
annotations = List(
Annotation(
length = 8,
startIndex = 0,
userMention = UserMentionMetadata(
`type` = UserMentionMetadataType.Mention,
user = User(
displayName = "TestBot",
name = "users/1234567890987654321",
`type` = UserType.Bot
)
),
`type` = AnnotationType.UserMention
)
)
),
User(name = "users/12345678901234567890", displayName = "Chuck Norris")
)
}

0 comments on commit cd80dc7

Please sign in to comment.