|
| 1 | +// |
| 2 | +// SILGattConfigurationCharacteristicEntityExportableSpec.swift |
| 3 | +// BlueGeckoTests |
| 4 | +// |
| 5 | +// Created by Grzegorz Janosz on 22/06/2021. |
| 6 | +// Copyright © 2021 SiliconLabs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +@testable import BlueGecko |
| 11 | + |
| 12 | +import Foundation |
| 13 | +import Quick |
| 14 | +import Nimble |
| 15 | +import RealmSwift |
| 16 | +import AEXML |
| 17 | + |
| 18 | +class SILGattConfigurationCharacteristicEntityExportableSpec: QuickSpec { |
| 19 | + |
| 20 | + override func spec() { |
| 21 | + let falseString = SILGattConfiguratorXmlDatabase.falseString |
| 22 | + let trueString = SILGattConfiguratorXmlDatabase.trueString |
| 23 | + |
| 24 | + let encryptedAttribute = SILGattConfiguratorXmlDatabase.GattConfigurationProperty.encryptedAttribute |
| 25 | + let authenticatedAttribute = SILGattConfiguratorXmlDatabase.GattConfigurationProperty.authenticatedAttribute |
| 26 | + let bondedAttribute = SILGattConfiguratorXmlDatabase.GattConfigurationProperty.bondedAttribute |
| 27 | + |
| 28 | + context("SILGattConfigurationCharacteristicEntity") { |
| 29 | + var characteristic: SILGattConfigurationCharacteristicEntity! |
| 30 | + var xmlElementResult: AEXMLElement? |
| 31 | + |
| 32 | + let nameAttribute = SILGattConfiguratorXmlDatabase.GattConfigurationGATTEntity.nameAttribute |
| 33 | + let uuidAttribute = SILGattConfiguratorXmlDatabase.GattConfigurationGATTEntity.uuidAttribute |
| 34 | + |
| 35 | + let propertiesName = SILGattConfiguratorXmlDatabase.GattConfigurationProperty.propertiesName |
| 36 | + let valueChildName = SILGattConfiguratorXmlDatabase.GattConfigurationValue.name |
| 37 | + |
| 38 | + describe("not imported SILGattConfigurationCharacteristicEntity") { |
| 39 | + let initialValue = "value" |
| 40 | + let name = "Alert Level" |
| 41 | + let characteristicUuid = "2A06" |
| 42 | + let descriptorUuid = UUID().uuidString |
| 43 | + let properties = [SILGattConfigurationProperty(type: .read, permission: .bonded), |
| 44 | + SILGattConfigurationProperty(type: .write, permission: .none), |
| 45 | + SILGattConfigurationProperty(type: .notify, permission: .bonded) |
| 46 | + ] |
| 47 | + let descriptor = SILGattConfigurationDescriptorEntity() |
| 48 | + |
| 49 | + beforeEach { |
| 50 | + descriptor.initialValueType = .none |
| 51 | + descriptor.initialValue = nil |
| 52 | + descriptor.cbuuidString = descriptorUuid |
| 53 | + descriptor.name = nil |
| 54 | + descriptor.properties = [SILGattConfigurationProperty(type: .read, permission: .none)] |
| 55 | + |
| 56 | + characteristic = SILGattConfigurationCharacteristicEntity() |
| 57 | + characteristic.initialValueType = .text |
| 58 | + characteristic.initialValue = initialValue |
| 59 | + characteristic.cbuuidString = characteristicUuid |
| 60 | + characteristic.name = name |
| 61 | + characteristic.properties = properties |
| 62 | + characteristic.descriptors.append(descriptor) |
| 63 | + xmlElementResult = characteristic.export() |
| 64 | + } |
| 65 | + |
| 66 | + it("should return proper format") { |
| 67 | + expect(xmlElementResult).notTo(beNil()) |
| 68 | + expect(xmlElementResult!.xmlCompact).to(equal(""" |
| 69 | +<characteristic name="\(name)" uuid="\(characteristicUuid)"> |
| 70 | + <properties> |
| 71 | + <read authenticated="false" bonded="true" encrypted="false" /> |
| 72 | + <write authenticated="false" bonded="false" encrypted="false" /> |
| 73 | + <notify authenticated="false" bonded="true" encrypted="false" /> |
| 74 | + </properties> |
| 75 | + <value length="\(initialValue.utf8.count)" type="utf-8" variable_length="true">\(initialValue)</value> |
| 76 | + <descriptor uuid="\(descriptorUuid)"> |
| 77 | + <properties> |
| 78 | + <read authenticated="false" bonded="false" encrypted="false" /> |
| 79 | + </properties> |
| 80 | + </descriptor> |
| 81 | +</characteristic> |
| 82 | +""".replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: "\n", with: "").replacingOccurrences(of: " ", with: ""))) |
| 83 | + } |
| 84 | + |
| 85 | + it("should have 2 attributes and 3 children") { |
| 86 | + expect(xmlElementResult?.attributes.count).to(equal(2)) |
| 87 | + expect(xmlElementResult?.children.count).to(equal(3)) |
| 88 | + } |
| 89 | + |
| 90 | + it("should have child node name properties with 3 properties") { |
| 91 | + let propertiesChild = xmlElementResult![propertiesName] |
| 92 | + expect(propertiesChild.error).to(beNil()) |
| 93 | + expect(propertiesChild.children.count).to(equal(3)) |
| 94 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.readName].error).to(beNil()) |
| 95 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.writeName].error).to(beNil()) |
| 96 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.notifyName].error).to(beNil()) |
| 97 | + } |
| 98 | + |
| 99 | + it("should have child node value") { |
| 100 | + let valueChild = xmlElementResult?[SILGattConfiguratorXmlDatabase.GattConfigurationValue.name] |
| 101 | + expect(valueChild?.error).to(beNil()) |
| 102 | + } |
| 103 | + |
| 104 | + it("should have name attribute set as Alert Level") { |
| 105 | + expect(xmlElementResult!.attributes[nameAttribute.name]).to(equal(name)) |
| 106 | + } |
| 107 | + |
| 108 | + it("should have uuid attribute set as 2A06") { |
| 109 | + expect(xmlElementResult!.attributes[uuidAttribute.name]).to(equal(characteristicUuid)) |
| 110 | + } |
| 111 | + |
| 112 | + it("should not have name attribute when name is nil") { |
| 113 | + characteristic.name = nil |
| 114 | + xmlElementResult = characteristic.export() |
| 115 | + expect(xmlElementResult!.attributes[nameAttribute.name]).to(beNil()) |
| 116 | + } |
| 117 | + |
| 118 | + it("should have two descriptors when second is appended") { |
| 119 | + let secondDescriptor = SILGattConfigurationDescriptorEntity() |
| 120 | + secondDescriptor.initialValueType = .none |
| 121 | + secondDescriptor.initialValue = nil |
| 122 | + secondDescriptor.cbuuidString = descriptorUuid |
| 123 | + secondDescriptor.name = nil |
| 124 | + secondDescriptor.properties = [SILGattConfigurationProperty(type: .write, permission: .none)] |
| 125 | + |
| 126 | + characteristic.descriptors.append(secondDescriptor) |
| 127 | + xmlElementResult = characteristic.export() |
| 128 | + expect(xmlElementResult?[SILGattConfiguratorXmlDatabase.GattConfigurationDescriptor.name].count).to(equal(2)) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + it("should not have value child when initial value is nil and type is none") { |
| 133 | + characteristic = SILGattConfigurationCharacteristicEntity() |
| 134 | + characteristic.initialValue = nil |
| 135 | + characteristic.initialValueType = .none |
| 136 | + characteristic.cbuuidString = UUID().uuidString |
| 137 | + characteristic.properties = [SILGattConfigurationProperty(type: .write, permission: .none)] |
| 138 | + xmlElementResult = characteristic.export() |
| 139 | + expect(xmlElementResult?.children.count).to(equal(1)) |
| 140 | + expect(xmlElementResult?[propertiesName].error).to(beNil()) |
| 141 | + expect(xmlElementResult?[valueChildName].error).to(equal(AEXMLError.elementNotFound)) |
| 142 | + } |
| 143 | + |
| 144 | + describe("imported SILGattConfigurationCharacteristicEntity") { |
| 145 | + let initialValue = "initial value" |
| 146 | + let uuid = UUID().uuidString |
| 147 | + let properties = [ |
| 148 | + SILGattConfigurationProperty(type: .read, permission: .bonded), |
| 149 | + SILGattConfigurationProperty(type: .writeWithoutResponse, permission: .none), |
| 150 | + SILGattConfigurationProperty(type: .indicate, permission: .none) |
| 151 | + ] |
| 152 | + |
| 153 | + let constAttribute = SILGattXMLAttribute(name: "const", value: falseString) |
| 154 | + let idAttribute = SILGattXMLAttribute(name: "id", value: "custom_id") |
| 155 | + let sourceIdAttribute = SILGattXMLAttribute(name: "sourceId", value: "") |
| 156 | + |
| 157 | + let descriptionXMLNode = AEXMLElement(name: "description", value: "custom description", attributes: [:]) |
| 158 | + |
| 159 | + let capabilitesXMLNode = AEXMLElement(name: "capabilities") |
| 160 | + capabilitesXMLNode.addChild(name: "capability", value: "custom_capability", attributes: [:]) |
| 161 | + |
| 162 | + let reliableWriteXMLNodeName = SILGattConfiguratorXmlDatabase.GattConfigurationProperty.reliableWriteName |
| 163 | + let reliableWriteXMLNode = AEXMLElement(name: reliableWriteXMLNodeName, value: nil, attributes: [ |
| 164 | + authenticatedAttribute.name: falseString, |
| 165 | + bondedAttribute.name: falseString, |
| 166 | + encryptedAttribute.name: trueString |
| 167 | + ]) |
| 168 | + |
| 169 | + beforeEach { |
| 170 | + characteristic = SILGattConfigurationCharacteristicEntity() |
| 171 | + characteristic.initialValueType = .text |
| 172 | + characteristic.initialValue = initialValue |
| 173 | + characteristic.cbuuidString = uuid |
| 174 | + characteristic.properties = properties |
| 175 | + characteristic.additionalXmlChildren = [ |
| 176 | + descriptionXMLNode, |
| 177 | + capabilitesXMLNode, |
| 178 | + reliableWriteXMLNode |
| 179 | + ] |
| 180 | + characteristic.additionalXmlAttributes = [ |
| 181 | + constAttribute, |
| 182 | + idAttribute, |
| 183 | + sourceIdAttribute |
| 184 | + ] |
| 185 | + xmlElementResult = characteristic.export() |
| 186 | + debugPrint(xmlElementResult!.xml as NSString) |
| 187 | + } |
| 188 | + |
| 189 | + it("should have 4 attributes and 4 children") { |
| 190 | + expect(xmlElementResult?.attributes.count).to(equal(4)) |
| 191 | + expect(xmlElementResult?.children.count).to(equal(4)) |
| 192 | + } |
| 193 | + |
| 194 | + it("should not have any descriptor child when there is no descriptors") { |
| 195 | + expect(xmlElementResult?[SILGattConfiguratorXmlDatabase.GattConfigurationDescriptor.name].error).to(equal(AEXMLError.elementNotFound)) |
| 196 | + } |
| 197 | + |
| 198 | + it("should have uuid attribute set as random uuid") { |
| 199 | + expect(xmlElementResult!.attributes[uuidAttribute.name]).to(equal(uuid)) |
| 200 | + } |
| 201 | + |
| 202 | + it("should have const attribute proper set") { |
| 203 | + expect(xmlElementResult!.attributes[constAttribute.name]).to(equal(constAttribute.value)) |
| 204 | + } |
| 205 | + |
| 206 | + it("should have id attribute proper set") { |
| 207 | + expect(xmlElementResult!.attributes[idAttribute.name]).to(equal(idAttribute.value)) |
| 208 | + } |
| 209 | + |
| 210 | + it("should have sourceId attribute proper set") { |
| 211 | + expect(xmlElementResult!.attributes[sourceIdAttribute.name]).to(equal(sourceIdAttribute.value)) |
| 212 | + } |
| 213 | + |
| 214 | + it("should have description node") { |
| 215 | + let descriptionNode = xmlElementResult?[descriptionXMLNode.name] |
| 216 | + expect(descriptionNode?.error).to(beNil()) |
| 217 | + expect(descriptionNode?.xml).to(equal(descriptionXMLNode.xml)) |
| 218 | + } |
| 219 | + |
| 220 | + it("should have capabilites node") { |
| 221 | + let capabilitiesNode = xmlElementResult?[capabilitesXMLNode.name] |
| 222 | + expect(capabilitiesNode?.error).to(beNil()) |
| 223 | + expect(capabilitiesNode?.xmlCompact).to(equal(capabilitesXMLNode.xmlCompact)) |
| 224 | + } |
| 225 | + |
| 226 | + it("should have child node name properties with 4 properties and one reliable write from import") { |
| 227 | + let propertiesChild = xmlElementResult![propertiesName] |
| 228 | + expect(propertiesChild.error).to(beNil()) |
| 229 | + expect(propertiesChild.children.count).to(equal(4)) |
| 230 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.readName].error).to(beNil()) |
| 231 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.indicateName].error).to(beNil()) |
| 232 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.writeNoResponseName].error).to(beNil()) |
| 233 | + |
| 234 | + expect(propertiesChild[SILGattConfiguratorXmlDatabase.GattConfigurationProperty.reliableWriteName].error).to(beNil()) |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + } |
| 240 | +} |
0 commit comments