Skip to content

Commit

Permalink
Merge pull request #465 from PHOENIXCONTACT/fix/instatiate-default-va…
Browse files Browse the repository at this point in the history
…lues

Add DefaultValue handling for Instatiate resources for MORYX 8
  • Loading branch information
Toxantron authored Sep 18, 2024
2 parents e529eb9 + 181526b commit 9e80c47
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System.Reflection;
using System.Runtime.Serialization;
using Moryx.Configuration;

namespace Moryx.AbstractionLayer.Resources.Endpoints
{
internal class DataMemberAttributeValueProviderFilter : IValueProviderFilter
{
private readonly bool _filterDataMembers;

public DataMemberAttributeValueProviderFilter(bool filterDataMembers)
{
_filterDataMembers = filterDataMembers;
}

public bool CheckProperty(PropertyInfo propertyInfo)
{
if (_filterDataMembers)
return propertyInfo.GetCustomAttribute<DataMemberAttribute>() == null;
return propertyInfo.GetCustomAttribute<DataMemberAttribute>() != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<ItemGroup>
<ProjectReference Include="..\Moryx.AbstractionLayer\Moryx.AbstractionLayer.csproj" />
<ProjectReference Include="..\Moryx.Asp.Extensions\Moryx.Asp.Extensions.csproj" />
<ProjectReference Include="..\Moryx.Resources.Management\Moryx.Resources.Management.csproj" />
<ProjectReference Include="..\Moryx.Runtime\Moryx.Runtime.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Moryx.AbstractionLayer.Properties;
using Moryx.Runtime.Modules;
using Moryx.Configuration;
using Moryx.Resources.Management;

namespace Moryx.AbstractionLayer.Resources.Endpoints
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Reflection;
using System.Runtime.Serialization;
using Moryx.Configuration;

namespace Moryx.Resources.Management
{
[Obsolete("No longer needed within the ResourceManagement")]
public class DataMemberAttributeValueProviderFilter : IValueProviderFilter
{
private readonly bool _filterDataMembers;
Expand Down
25 changes: 12 additions & 13 deletions src/Moryx.Resources.Management/Resources/ResourceEntityAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public Resource Instantiate(IResourceTypeController typeController, IResourceGra
Instance.Name = Name;
Instance.Description = Description;

// Copy extended data from json
if (ExtensionData != null)
JsonConvert.PopulateObject(ExtensionData, Instance, JsonSettings.Minimal);
// Copy extended data from json, or simply use JSON to provide defaults
JsonConvert.PopulateObject(ExtensionData ?? "{}", Instance, JsonSettings.Minimal);

return Instance;
}
Expand Down Expand Up @@ -110,14 +109,14 @@ public static ICollection<ResourceEntityAccessor> FetchResourceTemplates(IUnitOf
{
var resourceRepo = uow.GetRepository<IResourceRepository>();

var resources =
var resources =
(from res in resourceRepo.Linq
where res.Deleted == null
select res)
.ToList();

var resourcEntityAccessors = resources
.Select(res =>
.Select(res =>
new ResourceEntityAccessor
{
Id = res.Id,
Expand All @@ -126,13 +125,13 @@ public static ICollection<ResourceEntityAccessor> FetchResourceTemplates(IUnitOf
Description = res.Description,
ExtensionData = res.ExtensionData,
Relations = (from target in res.Targets
where target.Target.Deleted == null
// Attention: This is Copy&Paste because of LinQ limitations
select new ResourceRelationAccessor
{
Entity = target,
Role = ResourceReferenceRole.Target,
}).Concat(
where target.Target.Deleted == null
// Attention: This is Copy&Paste because of LinQ limitations
select new ResourceRelationAccessor
{
Entity = target,
Role = ResourceReferenceRole.Target,
}).Concat(
from source in res.Sources
where source.Source.Deleted == null
// Attention: This is Copy&Paste because of LinQ limitations
Expand Down Expand Up @@ -180,7 +179,7 @@ internal class ResourceRelationAccessor
/// Type of the reference relation
/// </summary>
public ResourceRelationType RelationType => (ResourceRelationType)Entity.RelationType;

/// <summary>
/// Id of the referenced resource
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Moryx.Model.Repositories;
using Newtonsoft.Json;
using NUnit.Framework;
using System.ComponentModel;

namespace Moryx.Resources.Management.Tests
{
Expand All @@ -22,14 +23,35 @@ public class ResourceEntityAccessorTests
public void Setup()
{
var typeControllerMock = new Mock<IResourceTypeController>();
typeControllerMock.Setup(tc => tc.Create(It.IsAny<string>())).Returns(new TestResource());
typeControllerMock.Setup(tc => tc.Create(It.Is<string>(type => type == typeof(TestResource).ResourceType()))).Returns(new TestResource());
typeControllerMock.Setup(tc => tc.Create(It.Is<string>(type => type == typeof(DefaultTestResource).ResourceType()))).Returns(new DefaultTestResource());

_typeControllerMock = typeControllerMock.Object;

var resourceCreator = new Mock<IResourceGraph>();
_resourceGraph = resourceCreator.Object;
}

[Test(Description = "Calling Instantiate without an entity sets default value")]
public void InstantiateWithoutEntitySetsDefaults()
{
// Arrange
var accessor = new ResourceEntityAccessor
{
Type = typeof(DefaultTestResource).ResourceType()
};

// Act
var resource = accessor.Instantiate(_typeControllerMock, _resourceGraph) as DefaultTestResource;

// Assert
Assert.NotNull(resource);
Assert.AreEqual(accessor.Type, resource.GetType().ResourceType());

Assert.IsTrue(resource.Enabled);
Assert.AreEqual(42, resource.Number);
}

[Test(Description = "Instantiates a resource")]
public void InstantiateCreatesAValidResourceObject()
{
Expand Down Expand Up @@ -118,6 +140,15 @@ private class ExtensionDataInherited : ExtensionDataTestBase
public long Value3 => 42;
}

private class DefaultTestResource : Resource
{
[DataMember, DefaultValue(42)]
public int Number { get; set; }

[DataMember, DefaultValue(true)]
public bool Enabled { get; set; }
}

private class TestResource : Resource
{
[DataMember]
Expand Down

0 comments on commit 9e80c47

Please sign in to comment.