/// <summary> /// インスタレーション登録/更新 - 任意の情報あり - 未ログイン /// </summary> public static async Task <NbSsePushInstallation> UpsertInstallation() { // Not SignUp & Login NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); // Channels Set ISet <string> channels = new HashSet <string>(); channels.Add("chan1"); currentInstallation.Channels = channels; // AllowedSenders Set ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:anonymous"); currentInstallation.AllowedSenders = allowedSenders; // Options Set NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue"); currentInstallation.Options = options; // Main return(await currentInstallation.Save()); }
private NbJsonObject CreateResponseBody(bool flag) { NbJsonObject body = new NbJsonObject(); ISet <string> channels = new HashSet <string>(); channels.Add("chan0"); body["_channels"] = channels; ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:group1"); body["_allowedSenders"] = allowedSenders; body["_osType"] = "dotnet"; body["_osVersion"] = "Unknown"; body["_deviceToken"] = "abcdefg"; body["_appVersionCode"] = -1; body["_appVersionString"] = "4.0.0.0"; body["_pushType"] = "sse"; if (flag) { body["_id"] = "12345"; } body["_owner"] = "ownerString"; return(body); }
private void CheckRequest() { ISet <string> channels = new HashSet <string>(); channels.Add("chan0"); ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:group1"); var req = executor.LastRequest; var reqJson = NbJsonParser.Parse(req.Content.ReadAsStringAsync().Result); NbJsonObject reqFullUpdateJson = (NbJsonObject)reqJson["$full_update"]; Assert.AreEqual(HttpMethod.Put, req.Method); Assert.IsTrue(req.Uri.EndsWith("/installations/" + "12345")); Assert.AreEqual(3, req.Headers.Count); Assert.IsTrue(req.Headers.ContainsKey(appKey)); Assert.IsTrue(req.Headers.ContainsKey(appId)); Assert.AreEqual(reqFullUpdateJson["_osType"], "dotnet"); Assert.AreEqual(reqFullUpdateJson["_osVersion"], "Unknown"); Assert.AreEqual(reqFullUpdateJson["_deviceToken"], "abcdefg"); Assert.AreEqual(reqFullUpdateJson["_pushType"], "sse"); Assert.AreEqual(reqFullUpdateJson["_channels"], channels); Assert.AreEqual(reqFullUpdateJson["_appVersionCode"], -1); // NUnitの場合は"0"が設定される Assert.AreEqual(reqFullUpdateJson["_appVersionString"], "0"); Assert.AreEqual(reqFullUpdateJson["_allowedSenders"], allowedSenders); Assert.AreEqual(reqFullUpdateJson["option1"], "option1value"); Assert.AreEqual(reqFullUpdateJson["option2"], "option2value"); }
public async void TestUpdateInstallationNormalNotLoggedIn() { // Signup & Login var user = await ITUtil.SignUpAndLogin(); // Save Installation await ITUtil.UpsertInstallation(); NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); Assert.AreEqual(user.UserId, currentInstallation.Owner); var preInstallationId = currentInstallation.InstallationId; var preUsername = currentInstallation.Username; var prePassword = currentInstallation.Password; var preUri = currentInstallation.Uri; // Logout await ITUtil.Logout(); // Change Channels, AllowedSenders and Options // Channels Set ISet <string> channels = new HashSet <string>(); channels.Add("chan2"); currentInstallation.Channels = channels; // AllowedSenders Set ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:anonymous"); currentInstallation.AllowedSenders = allowedSenders; // Options Set NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue2"); currentInstallation.Options = options; // Main var result = await currentInstallation.Save(); // Check Response ITUtil.CheckCommonResponse(result); Assert.AreEqual(preInstallationId, result.InstallationId); Assert.AreEqual(channels, result.Channels); Assert.AreEqual(allowedSenders, result.AllowedSenders); Assert.AreEqual(options, result.Options); // Check Owner is null Assert.IsNull(result.Owner); Assert.AreEqual(preUsername, result.Username); Assert.AreEqual(prePassword, result.Password); Assert.AreEqual(preUri, result.Uri); // Check Storage ITUtil.CheckSaveStorage(result); }
/// <summary> /// インスタレーション情報からリクエストボディを生成する。 /// </summary> internal override NbJsonObject MakeRequestBody() { NbJsonObject body = base.MakeRequestBody(); // pushTypeを設定 body[KeyPushType] = "sse"; return(body); }
/// <summary> /// NbHttpException発生時にResponseからエラー情報を取得する /// </summary> /// <param name="response">Response情報</param> /// <param name="error">Responseから情報を取得するキー</param> /// <returns>エラー情報</returns> public static string GetErrorInfo(HttpResponseMessage response, string error = "error") { if (response.Content != null) { var bodyString = response.Content.ReadAsStringAsync().Result; var responseJson = NbJsonObject.Parse(bodyString); return((string)responseJson[error]); } return(null); }
public void TestMakeRequestBodyNormal() { // インスタレーションに必須パラメータとオプションをセット NbSsePushInstallation installation = SetInstallationParameterAndOption(); // Main NbJsonObject body = installation.MakeRequestBody(); // Check Response CheckBody(body); }
/// <summary> /// ストレージからJsonを読み出す /// </summary> /// <returns>読み出した値</returns> protected NbJsonObject GetJsonFromStorage() { var attributes = File.GetAttributes(InstallationFilename); if ((attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) { // シンボリックリンクの場合は例外を投げる throw new FileNotFoundException(); } var jsonString = System.IO.File.ReadAllText(InstallationFilename); return(NbJsonObject.Parse(jsonString)); }
/// <summary> /// JSONをインスタレーションにセット /// </summary> /// <param name="json">JSON</param> internal virtual void SetInstallationFromJson(NbJsonObject json) { this.OsType = json.Opt <string>(KeyOsType, null); this.OsVersion = json.Opt <string>(KeyOsVersion, null); this.DeviceToken = json.Opt <string>(KeyDeviceToken, null); this.PushType = json.Opt <string>(KeyPushType, null); this.Channels = ConvertJsonArrayToSet(json.Opt <NbJsonArray>(KeyChannels, null)); this.AppVersionCode = json.Opt <int>(KeyAppVersionCode, -1); this.AppVersionString = json.Opt <string>(KeyAppVersionString, null); this.AllowedSenders = ConvertJsonArrayToSet(json.Opt <NbJsonArray>(KeyAllowedSenders, null)); this.Owner = json.Opt <string>(KeyOwner, null); this.InstallationId = json.Opt <string>(KeyId, null); this.Options = json.Opt <NbJsonObject>(KeyOptions, null); }
public async void TestSaveInstallationNormalSameDeviceToken() { // Save Installation await ITUtil.UpsertInstallation(); NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); var preInstallationId = currentInstallation.InstallationId; var preDeviceToken = currentInstallation.DeviceToken; // Set InstallationId null currentInstallation.InstallationId = null; // Change Channels, AllowedSenders and Options // Channels Set ISet <string> channels = new HashSet <string>(); channels.Add("chan2"); currentInstallation.Channels = channels; // AllowedSenders Set ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:anonymous"); currentInstallation.AllowedSenders = allowedSenders; // Options Set NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue2"); currentInstallation.Options = options; // Main var result = await currentInstallation.Save(); // Check Response ITUtil.CheckCommonResponse(result); Assert.AreEqual(preInstallationId, result.InstallationId); Assert.AreEqual(channels, result.Channels); Assert.AreEqual(allowedSenders, result.AllowedSenders); Assert.AreEqual(preDeviceToken, result.DeviceToken); // Check Owner is null Assert.IsNull(result.Owner); Assert.AreEqual(options, result.Options); // Check Storage ITUtil.CheckSaveStorage(result); }
/// <summary> /// ストレージにJSON情報を保存する /// </summary> /// <param name="json">JSON情報</param> /// <exception cref="InvalidOperationException">ファイル保存失敗</exception> public static void SaveJsonToStorage(NbJsonObject json) { // ストレージ内情報を削除 DeleteStorage(); // ファイルに保存する try { System.IO.File.WriteAllText(InstallationFilename, json.ToString()); } catch (Exception) { throw new InvalidOperationException("Failed to SaveJsonToStorage()"); } }
/// <summary> /// インスタレーション情報を作成してストレージに保存する /// </summary> /// <param name="flag">インスタレーションIDを付与する場合はtrueを指定する</param> private void SaveInstallationToStorage(bool flag) { var json = new NbJsonObject(); json = CreateResponseBody(flag); // ファイルに保存する try { System.IO.File.WriteAllText(InstallationFilename, json.ToString()); } catch (Exception) { throw new InvalidOperationException("Failed to SaveJsonToStorage()"); } }
/// <summary> /// ストレージからインスタレーション情報を取得する /// ストレージに情報がない場合は、各プロパティが初期状態で設定される /// </summary> internal virtual void LoadFromStorage() { NbJsonObject json = new NbJsonObject(); try { lock (this) { json = GetJsonFromStorage(); } } catch (System.IO.FileNotFoundException) { // do nothing } SetInstallationFromJson(json); }
/// <summary> /// ストレージ保存用のNbJsonObjectを作成する /// </summary> /// <param name="installationFlag">インスタレーションIDの付与フラグ</param> /// <param name="sseFlag">SSE情報の付与フラグ</param> /// <returns></returns> private NbJsonObject CreateBody(bool installationFlag, bool sseFlag) { NbJsonObject body = new NbJsonObject(); ISet <string> channels = new HashSet <string>(); channels.Add("chan0"); body["_channels"] = channels; ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:group1"); body["_allowedSenders"] = allowedSenders; body["_osType"] = "dotnet"; body["_osVersion"] = "Unknown"; body["_deviceToken"] = "abcdefg"; body["_appVersionCode"] = -1; body["_appVersionString"] = "4.0.0.0"; body["_pushType"] = "sse"; if (installationFlag) { body["_id"] = "12345"; } body["_owner"] = "ownerString"; // SSE情報付加 NbJsonObject sse = new NbJsonObject(); if (sseFlag) { sse["username"] = "******"; sse["password"] = "******"; } // テストの都合上、URIだけは入れておく sse["uri"] = "http://example.push.server/foo/bar"; body["_sse"] = sse; // Option付加 body["option1"] = "option1value"; body["option2"] = "option2value"; return(body); }
public async void TestDeleteAndSaveInstallationNormal() { // Save Installation await ITUtil.UpsertInstallation(); NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); var deviceToken = currentInstallation.DeviceToken; // Main await currentInstallation.DeleteInstallation(); // Check Delete Storage CheckDeleteStorage(); // Save(again) var result = await ITUtil.UpsertInstallation(); // Check Response ITUtil.CheckCommonResponse(result); ISet <string> channels = new HashSet <string>(); channels.Add("chan1"); Assert.AreEqual(channels, result.Channels); ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:anonymous"); Assert.AreEqual(allowedSenders, result.AllowedSenders); // Check Owner is null Assert.IsNull(result.Owner); NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue"); Assert.AreEqual(options, result.Options); // Check DeviceToken Assert.AreEqual(deviceToken, result.DeviceToken); // Check Storage ITUtil.CheckSaveStorage(result); }
/// <summary> /// JSONをインスタレーションにセット /// </summary> /// <param name="json">JSON</param> internal override void SetInstallationFromJson(NbJsonObject json) { base.SetInstallationFromJson(json); var sseJson = json.GetJsonObject(KeySse); if (sseJson == null) { this.Username = null; this.Password = null; this.Uri = null; } else { this.Username = sseJson.Opt <string>(KeyUsername, null); this.Password = sseJson.Opt <string>(KeyPassword, null); this.Uri = sseJson.Opt <string>(KeyUri, null); } }
/// <summary> /// インスタレーション情報からリクエストボディを生成する。 /// </summary> internal virtual NbJsonObject MakeRequestBody() { NbJsonObject body = new NbJsonObject(); if (this.Options != null) { foreach (var opt in this.Options) { string key = opt.Key; object val = opt.Value; body[key] = val; } } if (this.Channels != null) { body[KeyChannels] = this.Channels; } if (this.AllowedSenders != null) { body[KeyAllowedSenders] = this.AllowedSenders; } body[KeyOsType] = "dotnet"; body[KeyOsVersion] = "Unknown"; if (this.DeviceToken != null) { body[KeyDeviceToken] = this.DeviceToken; } body[KeyAppVersionCode] = -1; body[KeyAppVersionString] = GetVersionName(); // PushTypeはサブクラスで設定する return(body); }
// リクエストボディ内容をチェック private void CheckBody(NbJsonObject body) { Assert.AreEqual(body["option1"], "option1value"); Assert.AreEqual(body["option2"], "option2value"); ISet <string> channels = new HashSet <string>(); channels.Add("chan0"); Assert.AreEqual(body["_channels"], channels); ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:group1"); body["_allowedSenders"] = allowedSenders; body["_osType"] = "dotnet"; body["_osVersion"] = "Unknown"; body["_deviceToken"] = "abcdefg"; body["_appVersionCode"] = -1; body["_appVersionString"] = "0"; body["_pushType"] = "sse"; }
public async void TestSaveInstallationNormalNotLoggedIn() { // Not SignUp & Login NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); // Channels Set ISet <string> channels = new HashSet <string>(); channels.Add("chan1"); currentInstallation.Channels = channels; // AllowedSenders Set ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:authenticated"); currentInstallation.AllowedSenders = allowedSenders; // Options Set NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue"); currentInstallation.Options = options; // Main var result = await currentInstallation.Save(); // Check Response ITUtil.CheckCommonResponse(result); Assert.AreEqual(channels, result.Channels); Assert.AreEqual(allowedSenders, result.AllowedSenders); // Check Owner is null Assert.IsNull(result.Owner); Assert.AreEqual(options, result.Options); // Check Storage ITUtil.CheckSaveStorage(result); }
/// <summary> /// Save用リクエストを生成・送信する。 /// </summary> /// <returns>送信結果</returns> /// <exception cref="NbHttpException">リクエスト送信失敗</exception> internal async Task <NbJsonObject> SendRequestForSave() { NbRestRequest request; NbJsonObject bodyJson = MakeRequestBody(); // リクエスト生成 if (!string.IsNullOrEmpty(this.InstallationId)) { // 上書きの場合 var fullUpdateJson = new NbJsonObject(); fullUpdateJson["$full_update"] = bodyJson; request = Service.RestExecutor.CreateRequest("/push/installations/{installationId}", HttpMethod.Put) .SetUrlSegment("installationId", this.InstallationId).SetJsonBody(fullUpdateJson); } else { // 新規登録の場合 request = Service.RestExecutor.CreateRequest("/push/installations", HttpMethod.Post).SetJsonBody(bodyJson); } // リクエスト送信 return(await Service.RestExecutor.ExecuteRequestForJson(request)); }
private void CheckInstallation(NbSsePushInstallation installation, bool isInstallationExists) { if (isInstallationExists) { var installationJson = CreateResponseBody(true); Assert.AreEqual(installation.AllowedSenders, installationJson["_allowedSenders"]); Assert.AreEqual(installation.AppVersionCode, installationJson["_appVersionCode"]); Assert.AreEqual(installation.AppVersionString, installationJson["_appVersionString"]); Assert.AreEqual(installation.Channels, installationJson["_channels"]); Assert.AreEqual(installation.DeviceToken, installationJson["_deviceToken"]); Assert.AreEqual(installation.InstallationId, installationJson["_id"]); Assert.AreEqual(installation.Options["option1"], installationJson["option1"]); Assert.AreEqual(installation.Options["option2"], installationJson["option2"]); Assert.AreEqual(installation.OsType, installationJson["_osType"]); Assert.AreEqual(installation.OsVersion, installationJson["_osVersion"]); Assert.AreEqual(installation.Owner, installationJson["_owner"]); Assert.AreEqual(installation.PushType, installationJson["_pushType"]); NbJsonObject sse = (NbJsonObject)installationJson["_sse"]; Assert.AreEqual(installation.Username, sse["username"]); Assert.AreEqual(installation.Password, sse["password"]); Assert.AreEqual(installation.Uri, sse["uri"]); } else { Assert.IsNull(installation.AllowedSenders); Assert.AreEqual(installation.AppVersionCode, -1); Assert.IsNull(installation.AppVersionString); Assert.IsNull(installation.Channels); Assert.IsNotNull(installation.DeviceToken); Assert.IsNull(installation.InstallationId); Assert.IsNull(installation.Options); Assert.IsNull(installation.OsType); Assert.IsNull(installation.OsVersion); Assert.IsNull(installation.Owner); Assert.IsNull(installation.PushType); } }
public async void TestSaveInstallationExceptionNoDeviceToken() { NbSsePushInstallation currentInstallation = NbSsePushInstallation.GetCurrentInstallation(); // Channels Set ISet <string> channels = new HashSet <string>(); channels.Add("chan1"); currentInstallation.Channels = channels; // AllowedSenders Set ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:authenticated"); currentInstallation.AllowedSenders = allowedSenders; // Options Set NbJsonObject options = new NbJsonObject(); options.Add("email", "*****@*****.**"); options.Add("test", "testValue"); currentInstallation.Options = options; // Set DeviceToken Null currentInstallation.DeviceToken = null; // Main try { await currentInstallation.Save(); Assert.Fail("No Exception"); } catch (InvalidOperationException e) { Assert.AreEqual("Null DeviceToken.", e.Message); } }
/// <summary> /// ストレージにJSON情報を保存する /// </summary> /// <param name="json">JSON情報</param> /// <exception cref="InvalidOperationException">ファイル保存失敗</exception> protected void SaveJsonToStorage(NbJsonObject json) { if (json == null) { return; } // ストレージ内情報を削除 DeleteStorage(); // オプションフィールドが存在する場合は、"options"キーに入れる var modifiedJson = ConvertJsonToOptionsIncluded(json); // ファイルに保存する try { System.IO.File.WriteAllText(InstallationFilename, modifiedJson.ToString()); } catch (Exception) { throw new InvalidOperationException("Failed to SaveJsonToStorage()"); } }
/// <summary> /// オプションフィールドを検索し、"options"キーに入れる /// </summary> /// <param name="json">NbJsonObject</param> /// <returns>修正されたNbJsonObject</returns> protected NbJsonObject ConvertJsonToOptionsIncluded(NbJsonObject json) { var modifiedJson = new NbJsonObject(); var option = new NbJsonObject(); //キーをListに変換する List <string> keysList = json.Keys.ToList(); foreach (string key in keysList) { if (IsKeyForOption(key)) { // オプションに格納 option.Add(key, json[key]); } else { // modifiedJsonにコピー modifiedJson.Add(key, json[key]); } } // オプションが存在する場合 if (option.Count() > 0) { // "options"キーに入れる modifiedJson[KeyOptions] = option; // 元のキー/値を削除 List <string> list = option.Keys.ToList(); foreach (string key in list) { modifiedJson.Remove(key); } } return(modifiedJson); }
private void FindOptions(string key, NbJsonObject json, NbJsonObject option) { switch (key) { case KeyOsType: case KeyOsVersion: case KeyDeviceToken: case KeyPushType: case KeyAppVersionString: case KeyId: case KeyOwner: case KeyAppVersionCode: case KeyChannels: case KeyAllowedSenders: case KeySse: // そのまま break; default: // それ以外はオプションとしてセット option.Add(key, json[key]); break; } }
// オプションプロパティを検索し、"options"キーに入れる protected void FindAndSetOptions(NbJsonObject json) { NbJsonObject option = new NbJsonObject(); //キーをListに変換する List <string> keysList = json.Keys.ToList(); foreach (string key in keysList) { FindOptions(key, json, option); } if (option.Count() > 0) { json[KeyOptions] = option; // 元のキー/値を削除 List <string> list = option.Keys.ToList(); foreach (string key in list) { json.Remove(key); } } }
public async void TestSendRequestForSaveNormalFullUpdate() { SaveInstallationToStorage(true); installation.LoadFromStorage(); var installationId = installation.InstallationId; var responseBody = CreateResponseBody(true); var response = new MockRestResponse(HttpStatusCode.OK, responseBody.ToString()); executor.AddResponse(response); // Main var result = await installation.SendRequestForSave(); // Check Response Assert.AreEqual(result, responseBody); // Check Request var req = executor.LastRequest; var reqJson = NbJsonParser.Parse(req.Content.ReadAsStringAsync().Result); NbJsonObject reqFullUpdateJson = (NbJsonObject)reqJson["$full_update"]; Assert.AreEqual(HttpMethod.Put, req.Method); Assert.IsTrue(req.Uri.EndsWith("/installations/" + installationId)); Assert.AreEqual(3, req.Headers.Count); Assert.IsTrue(req.Headers.ContainsKey(appKey)); Assert.IsTrue(req.Headers.ContainsKey(appId)); Assert.AreEqual(reqFullUpdateJson["_channels"], installation.Channels); Assert.AreEqual(reqFullUpdateJson["_allowedSenders"], installation.AllowedSenders); Assert.AreEqual(reqFullUpdateJson["_osType"], installation.OsType); Assert.AreEqual(reqFullUpdateJson["_osVersion"], installation.OsVersion); Assert.AreEqual(reqFullUpdateJson["_deviceToken"], installation.DeviceToken); Assert.AreEqual(reqFullUpdateJson["_appVersionCode"], installation.AppVersionCode); // NUnitの場合は"0"が設定される Assert.AreEqual(reqFullUpdateJson["_appVersionString"], "0"); }
// インスタレーションに必須パラメータとオプションを設定 private NbSsePushInstallation SetInstallationParameterAndOption() { NbSsePushInstallation installation = new NbSsePushInstallation(); ISet <string> channels = new HashSet <string>(); channels.Add("chan0"); installation.Channels = channels; ISet <string> allowedSenders = new HashSet <string>(); allowedSenders.Add("g:group1"); installation.AllowedSenders = allowedSenders; installation.DeviceToken = "abcdefg"; NbJsonObject options = new NbJsonObject(); options.Add("option1", "option1value"); options.Add("option2", "option2value"); installation.Options = options; return(installation); }
/// <summary> /// ストレージからJsonを読み出す /// </summary> /// <returns>読み出した値</returns> public static NbJsonObject GetJsonFromStorage() { var jsonString = System.IO.File.ReadAllText(InstallationFilename); return(NbJsonObject.Parse(jsonString)); }
/// <summary> /// JSON形式のインスタレーション情報をストレージに保存し、その後インスタレーションクラスにセットする /// </summary> /// <param name="json">インスタレーション情報</param> protected void SaveAndLoad(NbJsonObject json) { SaveJsonToStorage(json); LoadFromStorage(); }