-
Notifications
You must be signed in to change notification settings - Fork 3
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
10 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
carp.common/src/commonMain/kotlin/dk/cachet/carp/common/application/devices/Website.kt
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,104 @@ | ||
@file:Suppress("NON_EXPORTABLE_TYPE") | ||
|
||
package dk.cachet.carp.common.application.devices | ||
|
||
import dk.cachet.carp.common.application.Trilean | ||
import dk.cachet.carp.common.application.data.DataType | ||
import dk.cachet.carp.common.application.sampling.DataTypeSamplingSchemeMap | ||
import dk.cachet.carp.common.application.sampling.SamplingConfiguration | ||
import dk.cachet.carp.common.application.tasks.TaskConfigurationList | ||
import dk.cachet.carp.common.application.toTrilean | ||
import dk.cachet.carp.common.infrastructure.serialization.NotSerializable | ||
import kotlinx.serialization.Required | ||
import kotlinx.serialization.Serializable | ||
import kotlin.js.JsExport | ||
import kotlin.reflect.KClass | ||
|
||
|
||
/** | ||
* A device representing a website. | ||
* | ||
* @param unsupportedBrowsers A set of web [Browser]s which do not yield a valid [WebsiteDeviceRegistration]. | ||
* Empty by default. | ||
*/ | ||
@Serializable | ||
@JsExport | ||
data class Website( | ||
val unsupportedBrowsers: Set<Browser> = emptySet(), | ||
override val roleName: String, | ||
override val isOptional: Boolean = false | ||
) : PrimaryDeviceConfiguration<WebsiteDeviceRegistration, WebsiteDeviceRegistrationBuilder>() | ||
{ | ||
object Sensors : DataTypeSamplingSchemeMap() | ||
object Tasks : TaskConfigurationList() | ||
|
||
override fun getSupportedDataTypes(): Set<DataType> = Sensors.keys | ||
|
||
override val defaultSamplingConfiguration: Map<DataType, SamplingConfiguration> = emptyMap() | ||
|
||
override fun getDataTypeSamplingSchemes(): DataTypeSamplingSchemeMap = Sensors | ||
|
||
override fun createDeviceRegistrationBuilder(): WebsiteDeviceRegistrationBuilder = | ||
WebsiteDeviceRegistrationBuilder() | ||
|
||
override fun getRegistrationClass(): KClass<WebsiteDeviceRegistration> = WebsiteDeviceRegistration::class | ||
|
||
override fun isValidRegistration( registration: WebsiteDeviceRegistration ): Trilean = | ||
unsupportedBrowsers.contains( registration.browser ).not().toTrilean() | ||
} | ||
|
||
/** | ||
* Describes a browser type which can be used to make [WebsiteDeviceRegistration]s. | ||
*/ | ||
@Serializable | ||
@JsExport | ||
enum class Browser | ||
{ | ||
Chrome, | ||
Firefox, | ||
Safari, | ||
Edge, | ||
Opera, | ||
Other; | ||
companion object | ||
{ | ||
fun fromStringIgnoreCase( value: String ): Browser = | ||
entries.firstOrNull { it.name.equals( value, ignoreCase = true ) } ?: Other | ||
} | ||
} | ||
|
||
/** | ||
* A [DeviceRegistration] for a website, specifying the [Browser] to be used and URL at which it is hosted. | ||
*/ | ||
@Serializable | ||
@JsExport | ||
data class WebsiteDeviceRegistration( | ||
val browser: Browser, | ||
val url: String, | ||
@Required | ||
override val deviceDisplayName: String? = null | ||
) : DeviceRegistration() | ||
{ | ||
@Required | ||
override val deviceId: String = "${browser.name}:$url" | ||
} | ||
|
||
|
||
@Suppress( "SERIALIZER_TYPE_INCOMPATIBLE" ) | ||
@Serializable( NotSerializable::class ) | ||
@JsExport | ||
class WebsiteDeviceRegistrationBuilder : DeviceRegistrationBuilder<WebsiteDeviceRegistration>() | ||
{ | ||
/** | ||
* The web [Browser] to register. The built [WebsiteDeviceRegistration] defaults to [Browser.Other] if not set. | ||
*/ | ||
var browser: String = "" | ||
|
||
/** | ||
* The URL to register. | ||
*/ | ||
var url: String = "" | ||
|
||
override fun build(): WebsiteDeviceRegistration = | ||
WebsiteDeviceRegistration( Browser.fromStringIgnoreCase( browser ), url ) | ||
} |
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
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
59 changes: 59 additions & 0 deletions
59
carp.common/src/commonTest/kotlin/dk/cachet/carp/common/application/devices/WebsiteTest.kt
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,59 @@ | ||
package dk.cachet.carp.common.application.devices | ||
|
||
import dk.cachet.carp.common.application.Trilean | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class WebsiteTest | ||
{ | ||
@Test | ||
// Browser.entries() is not supported in JS. | ||
@Suppress( "EnumValuesSoftDeprecate") | ||
fun browser_enum_contains_all_supported_browsers() | ||
{ | ||
val browsers = | ||
setOf( Browser.Chrome, Browser.Firefox, Browser.Safari, Browser.Edge, Browser.Opera, Browser.Other ) | ||
assertEquals( browsers, Browser.values().toSet() ) | ||
} | ||
|
||
@Test | ||
fun browser_fromStringIgnoreCase_should_ignore_case() | ||
{ | ||
assertEquals( Browser.Chrome, Browser.fromStringIgnoreCase( "chrome" ) ) | ||
assertEquals( Browser.Chrome, Browser.fromStringIgnoreCase( "CHROME" ) ) | ||
} | ||
|
||
@Test | ||
fun browser_fromStringIgnoreCase_returns_Other_for_unknown_browsers() | ||
{ | ||
assertEquals( Browser.Other, Browser.fromStringIgnoreCase( "unknown" ) ) | ||
} | ||
|
||
@Test | ||
fun registration_builder_sets_properties() | ||
{ | ||
val registration = WebsiteDeviceRegistrationBuilder().apply { | ||
browser = "CHROME" | ||
url = "https://www.example.com" | ||
}.build() | ||
|
||
assertEquals( Browser.Chrome, registration.browser ) | ||
assertEquals( "https://www.example.com", registration.url ) | ||
} | ||
|
||
@Test | ||
fun registration_valid_if_browser_is_supported() | ||
{ | ||
val configuration = Website( setOf( Browser.Firefox ), "Web browser" ) | ||
val registration = WebsiteDeviceRegistration( Browser.Chrome, "https://www.example.com" ) | ||
assertEquals( Trilean.TRUE, configuration.isValidRegistration( registration ) ) | ||
} | ||
|
||
@Test | ||
fun registration_invalid_if_browser_is_not_supported() | ||
{ | ||
val configuration = Website( setOf( Browser.Firefox ), "Web browser" ) | ||
val registration = WebsiteDeviceRegistration( Browser.Firefox, "https://www.example.com" ) | ||
assertEquals( Trilean.FALSE, configuration.isValidRegistration( registration ) ) | ||
} | ||
} |
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
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,10 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2019-09/schema", | ||
"type": "object", | ||
"properties": { | ||
"__type": { "const": "dk.cachet.carp.common.application.devices.browser" }, | ||
"value": { "enum": [ "Chrome", "Firefox", "Safari", "Edge", "Opera", "Other" ] } | ||
}, | ||
"required": [ "__type", "value" ], | ||
"additionalProperties": false | ||
} |
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
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
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,19 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2019-09/schema", | ||
"type": "object", | ||
"allOf": [ { "$ref": "PrimaryDeviceConfiguration.json#PrimaryDeviceConfiguration" } ], | ||
"properties": { | ||
"__type": { "const": "dk.cachet.carp.common.application.devices.Website" }, | ||
"unsupportedBrowsers": { | ||
"type": "array", | ||
"items": { "$ref": "Browser.json" } | ||
} | ||
}, | ||
"unevaluatedProperties": false, | ||
"$defs": { | ||
"DeviceRegistration": { | ||
"$anchor": "DeviceRegistration", | ||
"$ref": "WebsiteDeviceRegistration.json" | ||
} | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2019-09/schema", | ||
"type": "object", | ||
"allOf": [ { "$ref": "DeviceRegistration.json#DeviceRegistration" } ], | ||
"properties": { | ||
"url": { "type": "string", "format": "uri" }, | ||
"browser": { "$ref": "Browser.json" } | ||
}, | ||
"required": [ "url", "browser" ], | ||
"unevaluatedProperties": false | ||
} |