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