Refactor invalid operation exception handling (#87)

Addresses part of #80
This commit is contained in:
Jass Bagga 2017-06-21 12:35:31 -07:00 committed by GitHub
parent a8b6fa757a
commit c816bce13a
4 changed files with 67 additions and 11 deletions

View File

@ -2,26 +2,21 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Microsoft.AspNetCore.JsonPatch.Operations namespace Microsoft.AspNetCore.JsonPatch.Operations
{ {
public class OperationBase public class OperationBase
{ {
private string _op;
private OperationType _operationType;
[JsonIgnore] [JsonIgnore]
public OperationType OperationType public OperationType OperationType
{ {
get get
{ {
OperationType result; return _operationType;
if (!Enum.TryParse(op, ignoreCase: true, result: out result))
{
throw new JsonPatchException(
Resources.FormatInvalidJsonPatchOperation(op),
innerException: null);
}
return result;
} }
} }
@ -29,7 +24,23 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations
public string path { get; set; } public string path { get; set; }
[JsonProperty("op")] [JsonProperty("op")]
public string op { get; set; } public string op
{
get
{
return _op;
}
set
{
OperationType result;
if (!Enum.TryParse(value, ignoreCase: true, result: out result))
{
result = OperationType.Invalid;
}
_operationType = result;
_op = value;
}
}
[JsonProperty("from")] [JsonProperty("from")]
public string from { get; set; } public string from { get; set; }

View File

@ -75,6 +75,9 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations
break; break;
case OperationType.Test: case OperationType.Test:
throw new JsonPatchException(new JsonPatchError(objectToApplyTo, this, Resources.TestOperationNotSupported)); throw new JsonPatchException(new JsonPatchError(objectToApplyTo, this, Resources.TestOperationNotSupported));
case OperationType.Invalid:
throw new JsonPatchException(
Resources.FormatInvalidJsonPatchOperation(op), innerException: null);
default: default:
break; break;
} }

View File

@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Operations
Replace, Replace,
Move, Move,
Copy, Copy,
Test Test,
Invalid
} }
} }

View File

@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Operations
{
public class OperationBaseTests
{
[Theory]
[InlineData("ADd", OperationType.Add)]
[InlineData("Copy", OperationType.Copy)]
[InlineData("mOVE", OperationType.Move)]
[InlineData("REMOVE", OperationType.Remove)]
[InlineData("replace", OperationType.Replace)]
[InlineData("TeSt", OperationType.Test)]
public void SetValidOperationType(string op, OperationType operationType)
{
// Arrange
var operationBase = new OperationBase();
operationBase.op = op;
// Act & Assert
Assert.Equal(operationType, operationBase.OperationType);
}
[Theory]
[InlineData("invalid", OperationType.Invalid)]
[InlineData("coppy", OperationType.Invalid)]
[InlineData("notvalid", OperationType.Invalid)]
public void InvalidOperationType_SetsOperationTypeInvalid(string op, OperationType operationType)
{
// Arrange
var operationBase = new OperationBase();
operationBase.op = op;
// Act & Assert
Assert.Equal(operationType, operationBase.OperationType);
}
}
}