Exemplo n.º 1
0
        static public void Test()
        {
            try
            {
                //Input Spec
                InputSpecification iSpec = new InputSpecification();
                iSpec.Add("age", new Number(label: "Age", min: 0, unit: "years"));
                List aList = new List("Cheese Types");
                aList.Add("alp", new Text("Alp cheese"));
                aList.Add("brie", new Text("Brie cheese"));
                iSpec.Add("cheese-types", aList);
                SelectModuleResponse mResponse = new SelectModuleResponse(moduleId: "foo-bar_cheese-Module-v1-0",
                                                                          variantId: "503f191e8fcc19729de860ea", caseId: "5d9300d0-1574-4ae5-9d19-4e896b959f1c",
                                                                          kpiId: "cheese-taste-kpi", inputSpecification: iSpec);
                string json = Serialize.ToJsonString(mResponse);

                //Request from dashboard
                string jsonmessage = File.ReadAllText(@"../../../EcodistrictMessagingTests/TestData/Json/ModuleRequest/StartModuleRequest2.txt");
                object obj         = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonmessage);
                jsonmessage = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
                Type     expected = typeof(StartModuleRequest);
                IMessage message  = Deserialize <IMessage> .JsonString(jsonmessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        public void SelectModuleResponseTest()
        {
            try
            {
                // arrange
                InputSpecification iSpec = new InputSpecification();

                iSpec.Add("age", new Number(label: "Age", min: 0, unit: "years"));

                Options opt = new Options();
                opt.Add(new Option(value: "alp-cheese", label: "Alpk\u00e4se"));
                opt.Add(new Option(value: "edam-cheese", label: "Edammer"));
                Option brie = new Option(value: "brie-cheese", label: "Brie");
                opt.Add(brie);
                iSpec.Add("cheese-type", new Select(label: "Cheese type", options: opt, value: brie));

                SelectModuleResponse mResponse = new SelectModuleResponse(moduleId: "foo-bar_cheese-Module-v1-0",
                                                                          variantId: "503f191e8fcc19729de860ea", caseId: "5d9300d0-1574-4ae5-9d19-4e896b959f1c", kpiId: "cheese-taste-kpi", inputSpecification: iSpec);
                var      message = File.ReadAllText(@"../../TestData/Json/ModuleResponse/SelectModuleResponse.txt");
                IMessage obj     = Deserialize <IMessage> .JsonString(message);

                string expected = Serialize.ToJsonString(obj);

                // act
                string actual = Serialize.ToJsonString(mResponse);

                // assert
                Assert.AreEqual(expected, actual, false, "\nNot Json-serialized correctly:\n\n" + expected + "\n\n" + actual); //TODO is unordered => makes comparisson hard.
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Exemplo n.º 3
0
        public void StartModuleResponseReconstructionTest()
        {
            try
            {
                // arrange
                InputSpecification inputSpec = new InputSpecification();
                List aList = new List(label: "aList");
                aList.Add(key: "o1", item: new Number(label: "o1 label", value: 1));
                aList.Add(key: "o2", item: new Number(label: "o2 label", value: 2));
                aList.Add(key: "o3", item: new Number(label: "o3 label", value: 3));
                inputSpec.Add("list", aList);
                SelectModuleResponse mResponse = new SelectModuleResponse("", "", "", "", inputSpec);
                string expected = Serialize.ToJsonString(mResponse);

                // act
                SelectModuleResponse mResponseR = Deserialize <SelectModuleResponse> .JsonString(expected);

                string actual = Serialize.ToJsonString(mResponseR);

                // assert
                Assert.AreEqual(expected, actual, false, "\nSelectModuleResponse not Json-seralized correctly:\n\n" + expected + "\n\n" + actual);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            try
            {
                GetModulesRequestExemple();
                SelectModuleRequestExemple();
                StartModuleRequestExemple();
                GetModulesResponseExemple();
                SelectModuleResponseExemple();
                StartModuleResponseExemple();


                InputSpecification iSpec = new InputSpecification();

                iSpec.Add("age", new Number(label: "Age", min: 0, unit: "years"));

                Options opt = new Options();
                opt.Add(new Option(value: "alp-cheese", label: "Alpk\u00e4se"));
                opt.Add(new Option(value: "edam-cheese", label: "Edammer"));
                Option brie = new Option(value: "brie-cheese", label: "Brie");
                opt.Add(brie);
                iSpec.Add("cheese-type", new Select(label: "Cheese type", options: opt, value: brie));

                SelectModuleResponse mResponse = new SelectModuleResponse(moduleId: "foo-bar_cheese-Module-v1-0",
                                                                          variantId: "503f191e8fcc19729de860ea", caseId: "5d9300d0-1574-4ae5-9d19-4e896b959f1c",
                                                                          kpiId: "cheese-taste-kpi", inputSpecification: iSpec);
                var      message = File.ReadAllText(@"../../../EcodistrictMessagingTests/TestData/Json/ModuleResponse/SelectModuleResponse.txt");
                IMessage obj     = Deserialize <SelectModuleResponse> .JsonString(message);

                string expected = Serialize.ToJsonString(obj);

                // act
                string actual = Serialize.ToJsonString(mResponse);

                //StartModuleResponseReconstructionTest();

                // arrange
                //string jsonmessage = File.ReadAllText(@"../../../EcodistrictMessagingTests/TestData/Json/ModuleRequest/StartModuleRequest3.txt");

                //// act
                //IMessage message = Deserialize.JsonString(jsonmessage);
                //Type actual = message.GetType();


                StartModuleRequestComplex();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 5
0
        static void StartModuleResponseReconstructionTest()
        {
            // arrange
            InputSpecification inputSpec = new InputSpecification();
            List aList = new List(label: "aList");

            aList.Add(key: "o1", item: new Number(label: "o1 label", value: 1));
            aList.Add(key: "o2", item: new Number(label: "o2 label", value: 2));
            aList.Add(key: "o3", item: new Number(label: "o3 label", value: 3));
            inputSpec.Add("list", aList);
            SelectModuleResponse mResponse = new SelectModuleResponse("", "", "", "", inputSpec);
            string expected = Serialize.ToJsonString(mResponse);

            // act
            SelectModuleResponse mResponseR = Deserialize <SelectModuleResponse> .JsonString(expected);

            string actual = Serialize.ToJsonString(mResponseR);
        }
Exemplo n.º 6
0
        static void SelectModuleResponseExemple()
        {
            //This module's id
            string moduleId = "foo-bar_cheese-Module-v1-0";

            //The dashboard message recieved (as a json-string)
            string message = "{" +
                             "\"type\": \"request\"," +
                             "\"method\": \"selectModule\"," +
                             "\"moduleId\": \"foo-bar_cheese-Module-v1-0\"," +
                             "\"variantId\": \"503f191e8fcc19729de860ea\"," +
                             "\"kpiId\": \"cheese-taste-kpi\"" +
                             "}";
            //Message reconstructed into a .Net object.
            SelectModuleRequest recievedMessage = Deserialize <SelectModuleRequest> .JsonString(message);

            //Is this message meant for me?
            if (recievedMessage.moduleId == moduleId)
            {
                //For the selected kpi, create a input specification describing what data
                //the module need in order to calculate the selected kpi.
                InputSpecification iSpec = new InputSpecification();
                if (recievedMessage.kpiId == "cheese-taste-kpi")
                {
                    //In this case the module needs 2 things.

                    //A user specified age
                    Number numberAge = new Number(
                        label: "Age",
                        min: 0,
                        unit: "years");

                    Options opt = new Options();
                    opt.Add(new Option(value: "alp-cheese", label: "Alpk\u00e4se")); //Note the web-friendly string
                    opt.Add(new Option(value: "edam-cheese", label: "Edammer"));
                    Option brie = new Option(value: "brie-cheese", label: "Brie");
                    opt.Add(brie);

                    //And one of the above options of cheese-types.
                    //(The preselected value, "brie-cheese", is optional)
                    Select selectCheseType = new Select(
                        label: "Cheese type",
                        options: opt,
                        value: brie);

                    //Add these components to the input specification.
                    //(Note the choosed keys, its the keys that will be attached to the
                    //data when the dashboard returns with the user specified data in
                    //a StartModuleRequest.)
                    iSpec.Add(
                        key: "age",
                        value: numberAge);

                    iSpec.Add(
                        key: "cheese-type",
                        value: selectCheseType);
                }
                else
                {
                    //...
                }

                //Create the IMessage response.
                SelectModuleResponse mResponse = new SelectModuleResponse(
                    moduleId: recievedMessage.moduleId,
                    variantId: recievedMessage.variantId,
                    caseId: recievedMessage.caseId,
                    kpiId: recievedMessage.kpiId,
                    inputSpecification: iSpec);

                //json-string that can be interpreted by the dashboard
                //In this case indented in order to be easier to read (won't effect the dashboard).
                string messageResponse = Serialize.ToJsonString(mResponse, true);

                //Write the message to the console
                Console.WriteLine(messageResponse);

                //Output:
                //{
                //  "kpiId": "cheese-taste-kpi",
                //  "variantId": "503f191e8fcc19729de860ea",
                //  "inputSpecification": {
                //    "age": {
                //      "unit": "years",
                //      "min": 0,
                //      "label": "Age",
                //      "type": "number"
                //    },
                //    "cheese-type": {
                //      "options": [
                //        {
                //          "value": "alp-cheese",
                //          "label": "Alpk\u00e4se"
                //        },
                //        {
                //          "value": "edam-cheese",
                //          "label": "Edammer"
                //        },
                //        {
                //          "value": "brie-cheese",
                //          "label": "Brie"
                //        }
                //      ],
                //      "value": "brie-cheese",
                //      "label": "Cheese type",
                //      "type": "select"
                //    }
                //  },
                //  "moduleId": "foo-bar_cheese-Module-v1-0",
                //  "method": "selectModule",
                //  "type": "response"
                //}
            }

            Console.WriteLine("");
        }