From eeacc2008138e746fd51abb3cd4e3e7d7ce8d602 Mon Sep 17 00:00:00 2001
From: Klaus Fischer <klaus.fischer@eloware.com>
Date: Mon, 31 Oct 2022 08:34:21 +0100
Subject: [PATCH] Implemented 3.11, 4.5

---
 FitConnect/Services/Models/v1/Api/Problems.cs  |  8 ++++++--
 FitConnect/Subscriber.cs                       | 12 +++++++++---
 IntegrationTests/Sender/SenderTestHappyPath.cs | 14 ++++++++++++--
 3 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/FitConnect/Services/Models/v1/Api/Problems.cs b/FitConnect/Services/Models/v1/Api/Problems.cs
index 2e67824c..20b8fa2b 100644
--- a/FitConnect/Services/Models/v1/Api/Problems.cs
+++ b/FitConnect/Services/Models/v1/Api/Problems.cs
@@ -23,6 +23,9 @@ public class Problems {
     public const string TitleMissingSchema = "Schema-Referenz fehlt";
     public const string DetailMissingSchema = "Die Schema-Referenz fehlt im Metadatensatz.";
 
+    public const string TitleSchemaViolationData = "Schema-Fehler";
+    public const string DetailsSchemaViolationData = "Der Fachdatensatz ist nicht schema-valide.";
+
     public const string TitleUnsupportedService = "Verwaltungsleistung nicht unterstützt";
     public const string DetailUnsupportedService = "Die angegebene Verwaltungsleistung wird nicht unterstützt.";
     
@@ -64,6 +67,7 @@ public class Problems {
     public enum ProblemInstanceEnum {
         Submission,
         Metadata,
+        Data
     }
 
     internal Problems() {
@@ -124,8 +128,8 @@ public class Problems {
                 break;
             case ProblemTypeEnum.SchemaViolation:
                 type += "schema-violation";
-                title = TitleEventLogInconsistent;
-                instance = "submission";
+                title = TitleSchemaViolationData;
+                instance = "data";
                 break;
             case ProblemTypeEnum.SyntaxViolation:
                 type += "syntax-violation";
diff --git a/FitConnect/Subscriber.cs b/FitConnect/Subscriber.cs
index 23b58c96..6a0c4963 100644
--- a/FitConnect/Subscriber.cs
+++ b/FitConnect/Subscriber.cs
@@ -188,8 +188,14 @@ public class Subscriber : FitConnectClient,
             if (destinationService.SubmissionSchemas != null && destinationService.SubmissionSchemas
                     .Select(s => s.SchemaUri)
                     .Contains(dataSchema)) {
-                CheckDataSchema(dataSchema, submission);
+                var problem = new Problems(Problems.ProblemTypeEnum.UnsupportedDataSchema,
+                    Problems.DetailUnsupportedDataSchema);
+                RejectSubmission(submission, problem);
+                throw new SecurityEventException(problem);
             }
+
+            // SuccessCriteria:4.5
+            CheckDataSchema(dataSchema, submission);
         }
 
 
@@ -216,8 +222,8 @@ public class Subscriber : FitConnectClient,
         var isValidSchemaData = JObject.Parse(submission.Data!).IsValid(jSchema);
 
         if (!isValidSchemaData) {
-            var problem = new Problems(Problems.ProblemTypeEnum.UnsupportedDataSchema,
-                Problems.DetailUnsupportedDataSchema);
+            var problem = new Problems(Problems.ProblemTypeEnum.SchemaViolation,
+                Problems.DetailsSchemaViolationData);
             RejectSubmission(submission, problem);
             throw new SecurityEventException(problem);
         }
diff --git a/IntegrationTests/Sender/SenderTestHappyPath.cs b/IntegrationTests/Sender/SenderTestHappyPath.cs
index 0835df97..90d46fa2 100644
--- a/IntegrationTests/Sender/SenderTestHappyPath.cs
+++ b/IntegrationTests/Sender/SenderTestHappyPath.cs
@@ -90,6 +90,16 @@ public class SenderTestHappyPath : SenderTestBase {
         submission.Should().NotBeNull();
     }
 
+    private class SampleData {
+        [JsonProperty(PropertyName = "$schema", Order = -2)]
+        public string Schema { get; set; } =
+            "https://git.fitko.de/fit-connect/sdk-dotnet/-/raw/feature/594-auto-reject/simple_schema.json";
+
+        public string FirstName { get; set; }
+        public string LastName { get; set; }
+        public int Age { get; set; }
+    }
+
     [Test]
     public void Submit_FinishSubmissionWithData_ShouldGetIdFromServer() {
         // Arrange
@@ -108,11 +118,11 @@ public class SenderTestHappyPath : SenderTestBase {
             .WithDestination(DestinationId)
             .WithServiceType("ServiceName", "urn:de:fim:leika:leistung:99400048079000")
             .WithAttachments(attachments)
-            .WithJsonData(JsonConvert.SerializeObject(new {
+            .WithJsonData(JsonConvert.SerializeObject(new SampleData{
                 FirstName = "John",
                 LastName = "Doe",
                 Age = 42,
-                Birthday = DateTime.Today.AddYears(-42)
+                // Birthday = DateTime.Today.AddYears(-42)
             }));
 
         // Act
-- 
GitLab