• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FIWARE / trusted-issuers-registry / #16

11 Jun 2024 06:19AM UTC coverage: 77.186% (-0.2%) from 77.376%
#16

push

web-flow
Merge pull request #21 from FIWARE/skipFailingDids

fix log

1 of 1 new or added line in 1 file covered. (100.0%)

4 existing lines in 2 files now uncovered.

406 of 526 relevant lines covered (77.19%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/main/java/org/fiware/iam/common/repository/NgsiLdBaseRepository.java
1
package org.fiware.iam.common.repository;
2

3
import io.github.wistefan.mapping.EntityVOMapper;
4
import io.github.wistefan.mapping.JavaObjectMapper;
5
import io.micronaut.cache.annotation.Cacheable;
6
import io.micronaut.http.HttpStatus;
7
import io.micronaut.http.client.exceptions.HttpClientResponseException;
8
import lombok.RequiredArgsConstructor;
9
import org.fiware.iam.common.configuration.GeneralProperties;
10
import org.fiware.iam.common.exception.NgsiLdRepositoryException;
11
import org.fiware.iam.common.mapping.NGSIMapper;
12
import org.fiware.ngsi.api.EntitiesApiClient;
13
import org.fiware.ngsi.model.EntityVO;
14
import reactor.core.publisher.Mono;
15

16
import java.net.URI;
17
import java.util.Arrays;
18
import java.util.List;
19
import java.util.Optional;
20
import java.util.stream.Stream;
21

22
/**
23
 * Base-Repository implementation for using the NGSI-LD API as a storage backend. Supports caching and asynchronous retrieval of entities.
24
 */
25
@RequiredArgsConstructor
1✔
26
public abstract class NgsiLdBaseRepository{
27

28
    /**
29
     * Name for the entities cache
30
     */
31
    private static final String ENTITIES_CACHE_NAME = "entities";
32

33
    protected final GeneralProperties generalProperties;
34
    protected final EntitiesApiClient entitiesApi;
35
    protected final JavaObjectMapper javaObjectMapper;
36
    protected final NGSIMapper ngsiMapper;
37
    protected final EntityVOMapper entityVOMapper;
38

39

40
    protected String getLinkHeader() {
41
        return String.format("<%s>; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json", generalProperties.getContextUrl());
1✔
42
    }
43

44
    /**
45
     * Retrieve entity from the broker or from the cache if they are available there.
46
     *
47
     * @param entityId id of the entity
48
     * @return the entity
49
     */
50
    @Cacheable(ENTITIES_CACHE_NAME)
51
    public Mono<EntityVO> retrieveEntityById(URI entityId) {
52
        return asyncRetrieveEntityById(entityId, generalProperties.getTenant(), null, null, null, getLinkHeader());
×
53
    }
54

55
    public <T> Mono<T> get(URI id, Class<T> entityClass) {
56
        return retrieveEntityById(id)
×
57
                .flatMap(entityVO -> entityVOMapper.fromEntityVO(entityVO, entityClass));
×
58
    }
59

60
    /**
61
     * Helper method for combining a stream of entites to a single mono.
62
     *
63
     * @param entityVOStream stream of entites
64
     * @param targetClass    target class to map them
65
     * @param <T>            type of the target
66
     * @return a mono, emitting a list of mapped entities
67
     */
68
    protected <T> Mono<List<T>> zipToList(Stream<EntityVO> entityVOStream, Class<T> targetClass) {
69
        return Mono.zip(
1✔
70
                entityVOStream.map(entityVO -> entityVOMapper.fromEntityVO(entityVO, targetClass)).toList(),
1✔
71
                oList -> Arrays.stream(oList).map(targetClass::cast).toList()
1✔
72
        );
73
    }
74

75
    /**
76
     * Uncached call to the broker
77
     */
78
    private Mono<EntityVO> asyncRetrieveEntityById(URI entityId, String ngSILDTenant, String attrs, String type, String options, String link) {
79
        return entitiesApi
×
80
                .retrieveEntityById(entityId, ngSILDTenant, attrs, type, options, link)
×
81
                .onErrorResume(this::handleClientException);
×
82
    }
83

84
    public <T> Mono<List<T>> findEntities(Integer offset, Integer limit, String entityType, Class<T> entityClass) {
85
        return entitiesApi.queryEntities(generalProperties.getTenant(),
1✔
86
                        null,
87
                        null,
88
                        entityType,
89
                        null,
90
                        null,
91
                        null,
92
                        null,
93
                        null,
94
                        null,
95
                        null,
96
                        limit,
97
                        offset,
98
                        null,
99
                        getLinkHeader())
1✔
100
                .map(List::stream)
1✔
101
                .flatMap(entityVOStream -> zipToList(entityVOStream, entityClass))
1✔
102
                .onErrorResume(t -> {
1✔
UNCOV
103
                    throw new NgsiLdRepositoryException("Was not able to list entities.", Optional.of(t));
×
104
                });
105
    }
106

107
    private Mono<EntityVO> handleClientException(Throwable e) {
108
        if (e instanceof HttpClientResponseException httpException && httpException.getStatus().equals(HttpStatus.NOT_FOUND)) {
×
109
            return Mono.empty();
×
110
        }
111
        throw new NgsiLdRepositoryException("Was not able to successfully call the broker.", Optional.of(e));
×
112
    }
113

114
}
115

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc