Exemplo n.º 1
0
        public void TestDirectMethodGetCertificate()
        {
            // Expected values
            var expectedNonce         = "AFAFAFAFAF";
            var expectedHealthCert    = "Dummy health cert";
            var expectedCorrelationId = "Dummy correlation id";

            // Mock Object setup
            var callback = new CallBackMockup();

            callback.SendMessageHook      = SendMessageHook;
            callback.ReportPropertiesHook = ReportPropertiesHook;

            var systemConfigurator = new ConfigurationProxyMockup();

            systemConfigurator.SendCommandHook = (IRequest request) =>
            {
                if (request.Tag == DMMessageKind.DeviceHealthAttestationGetReport)
                {
                    var request2 = (DeviceHealthAttestationGetReportRequest)request;
                    Assert.AreEqual(expectedNonce, request2.Nonce);
                    return(new DeviceHealthAttestationGetReportResponse(expectedHealthCert, expectedCorrelationId));
                }
                Assert.Fail($"Got unexpected command - {request.Tag}");
                return(null);
            };

            var dha = new DeviceHealthAttestationHandler(callback, systemConfigurator);

            // Test begins
            var getCertificateMethod = dha.GetDirectMethodHandler()[DeviceHealthAttestationDataContract.GetReportMethodName];
            var param = new DeviceHealthAttestationDataContract.GetReportMethodParam()
            {
                Nonce = expectedNonce
            };

            var response = getCertificateMethod(JsonConvert.SerializeObject(param)).Result;

            // Test validation
            Assert.IsTrue(sentMessageEvent.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.AreEqual(SuccessResponse, response);
            Assert.AreEqual(DeviceHealthAttestationDataContract.HealthReportTag, sentMessageProperties["MessageType"]);
            var message = JsonConvert.DeserializeObject <DeviceHealthAttestationDataContract.HealthReport>(sentMessaage);

            Assert.AreEqual(expectedCorrelationId, message.CorrelationId);
            Assert.AreEqual(expectedHealthCert, message.HealthCertificate);
            EnsureReportedPropertyStatus("Reported");
        }