Skip to content

Commit

Permalink
Merge pull request #323 from bci-oss/feature/286-implement-crud-acces…
Browse files Browse the repository at this point in the history
…s-management-api__05-implement-mappers

feat: Implementation of CRUD API for Access management APIs - Implement mappers
  • Loading branch information
tunacicek authored Feb 19, 2024
2 parents 390806d + 1c21599 commit 87f0e5d
Show file tree
Hide file tree
Showing 5 changed files with 425 additions and 7 deletions.
6 changes: 6 additions & 0 deletions access-control-service-sql-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- Mapping -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>

<!-- Tests -->
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH and others
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
******************************************************************************/
package org.eclipse.tractusx.semantics.accesscontrol.sql.model.converter;

import org.eclipse.tractusx.semantics.accesscontrol.sql.model.AccessRule;
import org.eclipse.tractusx.semantics.accesscontrol.sql.model.policy.PolicyOperator;
import org.eclipse.tractusx.semantics.accesscontrol.sql.rest.model.CreateAccessRule;
import org.eclipse.tractusx.semantics.accesscontrol.sql.rest.model.OperatorType;
import org.eclipse.tractusx.semantics.accesscontrol.sql.rest.model.ReadUpdateAccessRule;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;

@Mapper( uses = { CustomAccessRuleMapper.class },
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )
public interface AccessRuleMapper {
@Mappings( {
@Mapping( target = "id", ignore = true ),
@Mapping( target = "tid", ignore = true ),
@Mapping( target = "targetTenant", ignore = true ),
@Mapping( target = "policyType", source = "policyType" ),
@Mapping( target = "policy", source = "policy" ),
@Mapping( target = "description", source = "description" ),
@Mapping( target = "validFrom",
expression = "java( java.util.Optional.ofNullable( source.getValidFrom() ).map( java.time.OffsetDateTime::toInstant ).orElse( null ) )" ),
@Mapping( target = "validTo",
expression = "java( java.util.Optional.ofNullable( source.getValidTo() ).map( java.time.OffsetDateTime::toInstant ).orElse( null ) )" )
} )
AccessRule map( CreateAccessRule source );

@Mappings( {
@Mapping( target = "id", source = "id" ),
@Mapping( target = "tid", source = "tid" ),
@Mapping( target = "targetTenant", ignore = true ),
@Mapping( target = "policyType", source = "policyType" ),
@Mapping( target = "policy", source = "policy" ),
@Mapping( target = "description", source = "description" ),
@Mapping( target = "validFrom",
expression = "java( java.util.Optional.ofNullable( source.getValidFrom() ).map( java.time.OffsetDateTime::toInstant ).orElse( null ) )" ),
@Mapping( target = "validTo",
expression = "java( java.util.Optional.ofNullable( source.getValidTo() ).map( java.time.OffsetDateTime::toInstant ).orElse( null ) )" )
} )
AccessRule map( ReadUpdateAccessRule source );

@Mappings( {
@Mapping( target = "id", source = "id" ),
@Mapping( target = "tid", source = "tid" ),
@Mapping( target = "policyType", source = "policyType" ),
@Mapping( target = "policy", source = "policy" ),
@Mapping( target = "description", source = "description" ),
@Mapping( target = "validFrom",
expression = "java( java.util.Optional.ofNullable( source.getValidFrom() ).map( i -> i.atOffset( java.time.ZoneOffset.UTC ) ).orElse( null ) )" ),
@Mapping( target = "validTo",
expression = "java( java.util.Optional.ofNullable( source.getValidTo() ).map( i -> i.atOffset( java.time.ZoneOffset.UTC ) ).orElse( null ) )" )
} )
ReadUpdateAccessRule map( AccessRule source );

@ValueMappings( {
@ValueMapping( target = "EQUALS", source = "EQ" ),
@ValueMapping( target = "INCLUDES", source = "INCLUDES" )
} )
PolicyOperator map( OperatorType source );

@ValueMappings( {
@ValueMapping( target = "EQ", source = "EQUALS" ),
@ValueMapping( target = "INCLUDES", source = "INCLUDES" )
} )
OperatorType map( PolicyOperator source );

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH and others
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
******************************************************************************/
package org.eclipse.tractusx.semantics.accesscontrol.sql.model.converter;

import org.eclipse.tractusx.semantics.accesscontrol.sql.model.AccessRule;
import org.mapstruct.AfterMapping;
import org.mapstruct.MappingTarget;
import org.springframework.stereotype.Component;

@Component
public class CustomAccessRuleMapper {

@AfterMapping
public void calledWithSourceAndTarget(Object anySource, @MappingTarget AccessRule target) {
target.setTargetTenant( target.getPolicy().getBpn() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,11 @@ schemas:
description: Optional validity end of the rule.
type: string
format: date-time
SingleOperatorType:
OperatorType:
type: string
description: Operators for a single value
enum:
- eq
ListOperatorType:
type: string
description: Operators for a list values
enum:
- includes
AccessRuleValue:
description: Policy with a single value
Expand All @@ -67,7 +63,7 @@ schemas:
attribute:
type: string
operator:
$ref: "#/schemas/SingleOperatorType"
$ref: "#/schemas/OperatorType"
value:
type: string
required:
Expand All @@ -82,7 +78,7 @@ schemas:
attribute:
type: string
operator:
$ref: "#/schemas/ListOperatorType"
$ref: "#/schemas/OperatorType"
values:
type: array
items:
Expand Down
Loading

0 comments on commit 87f0e5d

Please sign in to comment.