View Javadoc
1   package de.dlr.shepard.context.semantic.daos;
2   
3   import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   import static org.junit.jupiter.api.Assertions.assertThrows;
6   import static org.junit.jupiter.api.Assertions.assertTrue;
7   import static org.mockito.ArgumentMatchers.anyString;
8   import static org.mockito.ArgumentMatchers.eq;
9   import static org.mockito.Mockito.verify;
10  import static org.mockito.Mockito.when;
11  
12  import de.dlr.shepard.BaseTestCase;
13  import de.dlr.shepard.common.neo4j.entities.Annotatable;
14  import de.dlr.shepard.context.semantic.entities.SemanticAnnotation;
15  import jakarta.ws.rs.InternalServerErrorException;
16  import java.util.Collections;
17  import java.util.Iterator;
18  import java.util.List;
19  import java.util.Map;
20  import lombok.NonNull;
21  import org.apache.commons.lang3.NotImplementedException;
22  import org.junit.jupiter.api.Test;
23  import org.mockito.InjectMocks;
24  import org.mockito.Mock;
25  import org.neo4j.ogm.model.QueryStatistics;
26  import org.neo4j.ogm.model.Result;
27  import org.neo4j.ogm.session.Session;
28  
29  public class SemanticAnnotationDAOTest extends BaseTestCase {
30  
31    @Mock
32    private Session session;
33  
34    @InjectMocks
35    private SemanticAnnotationDAO dao;
36  
37    @Test
38    public void getEntityTypeTest() {
39      var type = dao.getEntityType();
40      assertEquals(SemanticAnnotation.class, type);
41    }
42  
43    @Test
44    public void findAllSemanticAnnotationsTest() {
45      var annotation = new SemanticAnnotation(1L);
46      annotation.setPropertyName("Test");
47  
48      var query =
49        """
50        MATCH (e)-[ha:has_annotation]->(a:SemanticAnnotation) \
51        WHERE ID(e)=1 WITH a MATCH path=(a)-[*0..1]->(n) WHERE n.deleted = FALSE OR n.deleted IS NULL \
52        RETURN a, nodes(path), relationships(path)""";
53      when(session.query(SemanticAnnotation.class, query, Collections.emptyMap())).thenReturn(List.of(annotation));
54  
55      var actual = dao.findAllSemanticAnnotationsByNeo4jId(1L);
56      verify(session).query(SemanticAnnotation.class, query, Collections.emptyMap());
57      assertEquals(List.of(annotation), actual);
58    }
59  
60    @Test
61    public void findAllSemanticAnnotationsByShepardIdTest() {
62      var annotation = new SemanticAnnotation(1L);
63      annotation.setPropertyName("Test");
64  
65      var query =
66        """
67        MATCH (e)-[ha:has_annotation]->(a:SemanticAnnotation) \
68        WHERE e.shepardId=11 WITH a MATCH path=(a)-[*0..1]->(n) WHERE n.deleted = FALSE OR n.deleted IS NULL \
69        RETURN a, nodes(path), relationships(path)""";
70      when(session.query(SemanticAnnotation.class, query, Collections.emptyMap())).thenReturn(List.of(annotation));
71  
72      var actual = dao.findAllSemanticAnnotationsByShepardId(11L);
73      verify(session).query(SemanticAnnotation.class, query, Collections.emptyMap());
74      assertEquals(List.of(annotation), actual);
75    }
76  
77    private static class TestAnnotatable implements Annotatable {
78  
79      @Override
80      public List<SemanticAnnotation> getAnnotations() {
81        throw new NotImplementedException();
82      }
83  
84      @Override
85      public void addAnnotation(SemanticAnnotation annotation) {
86        throw new NotImplementedException();
87      }
88    }
89  
90    /**
91     * Mocks the session query behavior to return predefined query results.
92     * Only the method {@link Session#query(String, Map)} is mocked.
93     * The mocked result is returned regardless of the query String.
94     *
95     * @param queryResult the iterable of map results to be returned by the mocked query
96     */
97    private void mockSessionQuery(Iterable<Map<String, Object>> queryResult) {
98      when(session.query(anyString(), eq(Map.of()))).thenReturn(
99        new Result() {
100         @Override
101         public QueryStatistics queryStatistics() {
102           throw new NotImplementedException();
103         }
104 
105         @Override
106         @NonNull
107         public Iterator<Map<String, Object>> iterator() {
108           return queryResult.iterator();
109         }
110 
111         @Override
112         public Iterable<Map<String, Object>> queryResults() {
113           return queryResult;
114         }
115       }
116     );
117   }
118 
119   @Test
120   public void findAnnotatableByNeo4jIdNotDeletedTest_ObjectFound() {
121     mockSessionQuery(List.of(Map.of("n", new TestAnnotatable())));
122     var result = dao.findAnnotatableByNeo4jIdNotDeleted(1L);
123     assertTrue(result.isPresent());
124   }
125 
126   @Test
127   public void findAnnotatableByNeo4jIdNotDeletedTest_ObjectNotFound() {
128     mockSessionQuery(List.of());
129     var result = dao.findAnnotatableByNeo4jIdNotDeleted(1L);
130     assertTrue(result.isEmpty());
131   }
132 
133   @Test
134   public void findAnnotatableByNeo4jIdNotDeletedTest_ObjectHasWrongClass() {
135     mockSessionQuery(List.of(Map.of("n", new Object())));
136     assertThrows(InternalServerErrorException.class, () -> dao.findAnnotatableByNeo4jIdNotDeleted(1L));
137   }
138 
139   @Test
140   public void testSaveAnnotatable() {
141     assertDoesNotThrow(() -> dao.saveAnnotatable(new TestAnnotatable()));
142   }
143 }