Skip to content

Commit

Permalink
workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
pulledtim committed Oct 10, 2023
1 parent 11eef6a commit 7b240cd
Show file tree
Hide file tree
Showing 25 changed files with 850 additions and 268 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<strictSpec>true</strictSpec>
<modelPackage>org.fiware.iam.ccs.model</modelPackage>
<generateAliasAsModel>true</generateAliasAsModel>
<generateModels>false</generateModels>
<generateApiTests>true</generateApiTests>
<generatorName>micronaut</generatorName>
<modelNameSuffix>VO</modelNameSuffix>
Expand Down
246 changes: 113 additions & 133 deletions src/main/java/org/fiware/iam/ServiceMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,147 +4,127 @@
import org.fiware.iam.ccs.model.ServiceScopesEntryVO;
import org.fiware.iam.ccs.model.ServiceScopesVO;
import org.fiware.iam.ccs.model.ServiceVO;
import org.fiware.iam.repository.*;
import org.fiware.iam.repository.Credential;
import org.fiware.iam.repository.EndpointEntry;
import org.fiware.iam.repository.EndpointType;
import org.fiware.iam.repository.Service;
import org.mapstruct.Mapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

/**
* Responsible for mapping entities from the Service api domain to the internal model.
*/
@Mapper(componentModel = "jsr330")
public interface ServiceMapper {

default Service map(ServiceVO serviceVO) {
return new Service()
.setDefaultOidcScope(serviceVO.getDefaultOidcScope())
.setId(serviceVO.getId())
.setOidcScopes(map(serviceVO.getOidcScopes(), serviceVO.getId()));
}

ServiceVO map(Service service);

default ServiceScope map(ServiceScopesEntryVO serviceScopesEntryVO, String scopeName, String serviceName) {
return new ServiceScope()
.setId("%s_%s".formatted(scopeName, serviceName))
.setScopeName(scopeName)
.setCredentials(serviceScopesEntryVO.stream().map(this::map).toList());
}

ServiceScopesEntryVO map(ServiceScope serviceScope);

default Collection<ServiceScope> map(ServiceScopesVO value, String serviceName) {
if (value.getAdditionalProperties() == null) {
return List.of();
}
return value
.getAdditionalProperties()
.entrySet()
.stream()
.map(e -> map(e.getValue(), e.getKey(), serviceName))
.toList();
}

default ServiceScopesVO mapEntries(Collection<ServiceScope> value) {
ServiceScopesVO mappedScopes = new ServiceScopesVO();
if (value != null) {
value.forEach(e -> {
ServiceScopesEntryVO scopes = new ServiceScopesEntryVO();
scopes.addAll(map(e.getCredentials()));
mappedScopes.setAdditionalProperties(e.getScopeName(), scopes);
}
);
}
return mappedScopes;
}

default Credential map(CredentialVO credentialVO) {
if (credentialVO == null) {
return null;
}
Credential credential = new Credential()
.setCredentialType(credentialVO.getType());
List<EndpointEntry> trustedList = new ArrayList<>();
Optional.ofNullable(issuersToEntries(credentialVO.getTrustedIssuersLists())).ifPresent(trustedList::addAll);
Optional.ofNullable(participantsToEntries(credentialVO.getTrustedParticipantsLists())).ifPresent(trustedList::addAll);
credential.setTrustedLists(trustedList);
return credential;
}

default Collection<CredentialVO> map(Collection<Credential> credentials) {
if (credentials == null) {
return null;
}
return credentials.stream().map(this::map).toList();
}

default CredentialVO map(Credential credential) {
if (credential == null) {
return null;
}
return new CredentialVO()
.type(credential.getCredentialType())
.trustedIssuersLists(entriesToIssuers(credential.getTrustedLists()))
.trustedParticipantsLists(entriesToParticipants(credential.getTrustedLists()));
}

/**
* Map a list of string-entries, encoding TrustedParticipants endpoints to a list of {@link EndpointEntry} with
* type {{@link EndpointType.TRUSTED_PARTICIPANTS}
*/
default List<EndpointEntry> participantsToEntries(List<String> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.map(endpoint -> new EndpointEntry()
.setEndpoint(endpoint)
.setType(EndpointType.TRUSTED_PARTICIPANTS))
.toList();
}

/**
* Map a list of string-entries, encoding TrustedIssuers endpoints to a list of {@link EndpointEntry} with
* type {{@link EndpointType.TRUSTED_ISSUERS}
*/
default List<EndpointEntry> issuersToEntries(List<String> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.map(endpoint -> new EndpointEntry()
.setEndpoint(endpoint)
.setType(EndpointType.TRUSTED_ISSUERS))
.toList();
}

/**
* Return issuer endpoints from the {@link EndpointEntry} list to a list of strings
*/
default List<String> entriesToIssuers(List<EndpointEntry> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.filter(entry -> entry.getType().equals(EndpointType.TRUSTED_ISSUERS))
.map(EndpointEntry::getEndpoint)
.toList();
}

/**
* Return participant endpoints from the {@link EndpointEntry} list to a list of strings
*/
default List<String> entriesToParticipants(List<EndpointEntry> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.filter(entry -> entry.getType().equals(EndpointType.TRUSTED_PARTICIPANTS))
.map(EndpointEntry::getEndpoint)
.toList();
}
Service map(ServiceVO serviceVO);

default Map<String, Collection<Credential>> map(ServiceScopesVO value) {
return Optional.ofNullable(value)
.orElseGet(ServiceScopesVO::new)
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().stream().map(this::map).toList()));
}

default ServiceScopesVO mapCredentials(Map<String, Collection<Credential>> scopes) {
ServiceScopesVO answer = new ServiceScopesVO();
scopes.entrySet().forEach(entry -> {
ServiceScopesEntryVO credentialVOS = new ServiceScopesEntryVO();
entry.getValue().stream().map(this::map).forEach(credentialVOS::add);
answer.put(entry.getKey(), credentialVOS);
});
return answer;
}

ServiceVO map(Service service);

default Credential map(CredentialVO credentialVO) {
if (credentialVO == null) {
return null;
}
Credential credential = new Credential()
.setCredentialType(credentialVO.getType());
List<EndpointEntry> trustedList = new ArrayList<>();
Optional.ofNullable(issuersToEntries(credentialVO.getTrustedIssuersLists())).ifPresent(trustedList::addAll);
Optional.ofNullable(participantsToEntries(credentialVO.getTrustedParticipantsLists())).ifPresent(trustedList::addAll);
credential.setTrustedLists(trustedList);
return credential;
}

default Collection<CredentialVO> map(Collection<Credential> credentials) {
if (credentials == null) {
return null;
}
return credentials.stream().map(this::map).toList();
}

default CredentialVO map(Credential credential) {
if (credential == null) {
return null;
}
return new CredentialVO()
.type(credential.getCredentialType())
.trustedIssuersLists(entriesToIssuers(credential.getTrustedLists()))
.trustedParticipantsLists(entriesToParticipants(credential.getTrustedLists()));
}

/**
* Map a list of string-entries, encoding TrustedParticipants endpoints to a list of {@link EndpointEntry} with
* type {{@link EndpointType.TRUSTED_PARTICIPANTS}
*/
default List<EndpointEntry> participantsToEntries(List<String> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.map(endpoint -> new EndpointEntry()
.setEndpoint(endpoint)
.setType(EndpointType.TRUSTED_PARTICIPANTS))
.toList();
}

/**
* Map a list of string-entries, encoding TrustedIssuers endpoints to a list of {@link EndpointEntry} with
* type {{@link EndpointType.TRUSTED_ISSUERS}
*/
default List<EndpointEntry> issuersToEntries(List<String> endpoints) {
if (endpoints == null) {
return null;
}
return endpoints.stream()
.map(endpoint -> new EndpointEntry()
.setEndpoint(endpoint)
.setType(EndpointType.TRUSTED_ISSUERS))
.toList();
}

/**
* Return issuer endpoints from the {@link EndpointEntry} list to a list of strings
*/
default List<String> entriesToIssuers(List<EndpointEntry> endpoints) {
if (endpoints == null) {
return List.of();
}
return endpoints.stream()
.filter(entry -> entry.getType().equals(EndpointType.TRUSTED_ISSUERS))
.map(EndpointEntry::getEndpoint)
.toList();
}

/**
* Return participant endpoints from the {@link EndpointEntry} list to a list of strings
*/
default List<String> entriesToParticipants(List<EndpointEntry> endpoints) {
if (endpoints == null) {
return List.of();
}
return endpoints.stream()
.filter(entry -> entry.getType().equals(EndpointType.TRUSTED_PARTICIPANTS))
.map(EndpointEntry::getEndpoint)
.toList();
}

}
Loading

0 comments on commit 7b240cd

Please sign in to comment.