public void ReadFromFile_WorksWhenFileExists() { PostTestCleanup(); //Test set up - generate a model and save it out so it can be retreived by the middleware var tutorialName = "tutorial 1"; var stepName = "step 1"; var genreName = "myGenre"; var stepLookupName = ConstructID(tutorialName, stepName); var textLookupName = ConstructID(stepLookupName, "text"); var actualModel = new TutorialManagerModel(); var tutorial = new TutorialEntity(tutorialName); tutorial.steps.Add(stepName); var step = new StepEntity(stepLookupName); step.messaging.isActive = true; var text = new ContentEntity(textLookupName, "text", "yooo what's up! I work!"); step.messaging.content.Add(text.id); actualModel.genre = genreName; actualModel.tutorials.Add(tutorial); actualModel.steps.Add(step); actualModel.content.Add(text); BinaryFormatter binaryFormatter = new BinaryFormatter(); FileStream file = File.Create(GetModelSavePath()); binaryFormatter.Serialize(file, actualModel); file.Close(); //Actual test code var modelMiddleware = TutorialManagerModelMiddleware.GetInstance(); Assert.That(genreName, Is.EqualTo(modelMiddleware.TMData.genre), string.Format("genre should be {0}", genreName)); Assert.IsNotEmpty(modelMiddleware.TMData.tutorials, "tutorials should be populated"); Assert.IsNotEmpty(modelMiddleware.TMData.steps, "steps should be populated"); Assert.IsNotEmpty(modelMiddleware.TMData.content, "content should be populated"); Assert.AreEqual(1, modelMiddleware.TMData.tutorials.Count, "there should only be one tutorial"); Assert.AreEqual(1, modelMiddleware.TMData.steps.Count, "there should only be one step"); Assert.AreEqual(1, modelMiddleware.TMData.content.Count, "there should only be one conent"); Assert.That(tutorialName, Is.EqualTo(modelMiddleware.TMData.tutorials[0].id), string.Format("the tutorial should be named {0}", tutorialName)); Assert.That(stepLookupName, Is.EqualTo(modelMiddleware.TMData.steps[0].id), string.Format("the step should be named {0}", stepLookupName)); Assert.That(textLookupName, Is.EqualTo(modelMiddleware.TMData.content[0].id), string.Format("the content should be named {0}", textLookupName)); Assert.IsTrue(modelMiddleware.TMData.tutorialTable.ContainsKey(tutorialName), string.Format("tutorial table should contain {0}", tutorialName)); Assert.IsTrue(modelMiddleware.TMData.stepTable.ContainsKey(stepLookupName), string.Format("steps table should contain {0}", stepLookupName)); Assert.IsTrue(modelMiddleware.TMData.contentTable.ContainsKey(textLookupName), string.Format("content table should contain {0}", textLookupName)); PostTestCleanup(); }
public static IEnumerator <AsyncOperation> Write(string appId, TutorialManagerModel model) { var json = TMModelToJsonInterpreter.ProcessModelToJson(model); //Given UnityWebRequest applies URL encoding to POST message payloads, we're using a PUT instead var settingsRequest = Authorize(UnityWebRequest.Put(GetUrl(appId), json)); if (IsAuthError(settingsRequest)) { WriteEvent(false); yield break; } #if UNITY_2017_2_OR_NEWER yield return(settingsRequest.SendWebRequest()); #else yield return(settingsRequest.Send()); #endif #if UNITY_2017_1_OR_NEWER if (settingsRequest.isNetworkError || settingsRequest.isHttpError) #else if (settingsRequest.isError || settingsRequest.responseCode >= 400) #endif { Debug.LogWarningFormat("Failed to write remote settings: {0}: {1}", settingsRequest.responseCode, settingsRequest.error); if (SuspectBadToken(settingsRequest) && TMRSWriteRetry != null) { AccessToken.OnTokenRefresh += OnTokenRefresh; IEnumerator <AsyncOperation> innerLoopEnumerator = AccessToken.RefreshAccessToken().GetEnumerator(); while (innerLoopEnumerator.MoveNext()) { yield return(innerLoopEnumerator.Current); } } else { WriteEvent(false); } yield break; } WriteEvent(true); }
public static string ProcessModelToJson(TutorialManagerModel model) { var rsDataList = new RemoteSettingsData(); rsDataList.genre = model.genre; rsDataList.remoteSettings = new List <TMRemoteSettingsKeyValueType>(); string tutorialsValue = null; if (model.tutorials.Count == 1) { tutorialsValue = "[" + Quotify(model.tutorials[0].id) + "]"; } else { tutorialsValue = "[" + model.tutorials.Select(x => x.id).Aggregate((current, next) => Quotify(current) + "," + Quotify(next)) + "]"; } rsDataList.remoteSettings.Add(MakeRSJSONObject("tutorials", tutorialsValue)); var stepsList = model.tutorials.Select(x => { if (x.steps.Count == 1) { return(MakeRSJSONObject(x.id, "[" + Quotify(x.steps[0]) + "]")); } return(MakeRSJSONObject(x.id, "[" + x.steps.Select(y => y).Aggregate((current, next) => Quotify(current) + "," + Quotify(next)) + "]")); }); rsDataList.remoteSettings.AddRange(stepsList); var contentList = model.content.Select(x => { return(MakeRSJSONObject(x.id, x.text)); }); rsDataList.remoteSettings.AddRange(contentList); return(JsonUtility.ToJson(rsDataList)); }