View Javadoc
1   package io.github.magwas.inez.query;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.mockito.Mockito.mock;
5   import static org.mockito.Mockito.when;
6   
7   import java.io.IOException;
8   import java.util.List;
9   import java.util.Set;
10  import java.util.stream.Collectors;
11  
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Tag;
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.api.extension.ExtendWith;
16  import org.osgi.framework.BundleContext;
17  import org.osgi.framework.ServiceReference;
18  import org.springframework.beans.factory.annotation.Autowired;
19  import org.springframework.test.context.ContextConfiguration;
20  import org.springframework.test.context.junit.jupiter.SpringExtension;
21  
22  import io.github.magwas.inez.Bridi;
23  import io.github.magwas.inez.BridiTestData;
24  import io.github.magwas.inez.InezImpl;
25  import io.github.magwas.inez.TestConfig;
26  import io.github.magwas.inez.functions.Save;
27  import io.github.magwas.inez.osgi.SpringBootBundleActivator;
28  import io.github.magwas.runtime.LogUtil;
29  import io.github.magwas.testing.TestUtil;
30  
31  @Tag("end-to-end")
32  @ExtendWith(SpringExtension.class)
33  @ContextConfiguration(classes = TestConfig.class)
34  class QueryProcessorEndToEndTest implements BridiTestData {
35  
36  	@Autowired
37  	InezImpl inez;
38  
39  	@BeforeEach
40  	void setUp() throws IOException {
41  		ServiceReference<Save> sr = mock(ServiceReference.class);
42  		BundleContext ctx = mock(BundleContext.class);
43  		when((ServiceReference<Save>) ctx.getServiceReference("io.github.magwas.inez.functions.Save"))
44  				.thenReturn(sr);
45  		when(ctx.getService(sr)).thenReturn(new Save());
46  		SpringBootBundleActivator.bundleContext = ctx;
47  		inez.initialize();
48  	}
49  
50  	@Test
51  	void test1() {
52  		inez.create(TEST_TEXT).peek(x -> LogUtil.debug("created:" + x)).toList();
53  		assertEquals(List.of(ALICE), inez.findAllByRepresentation(ALICE_REPR).toList());
54  		assertQuery(Set.of(ALICE_REPR), ALICE_REPR);
55  		assertQuery(Set.of(ALICE_EATS_BANANA), ALICE_EATS_BANANA);
56  		assertQuery(Set.of(ALICE_EATS_BANANA, BOB_EATS_BANANA, CECILE_EATS_BANANA), "{$?} {{eats} {banana}}");
57  		assertQuery(Set.of(CECILE_EATS_BANANA, CECILE_LOOKS_AT_BANANA), "{cecile} {{$?} {banana}}");
58  		assertQuery(Set.of(ALICE_EATS_BANANA, ALICE_EATS_CHIPS), "{alice} {{eats} {$?}}");
59  		assertQuery(Set.of("putty"), "doSave {" + "putty" + "}");
60  		assertEquals(1, inez.findAllByRepresentation("putty").count());
61  		List<Bridi> putty = inez.findAllByRepresentation("putty").toList();
62  		assertQuery(
63  				Set.of("osgi", "bitch", "{0} is a {1}", "{osgi} is a {bitch}"),
64  				"doSave {" + "{osgi} is a {bitch}" + "}");
65  		assertEquals(1, inez.findAllByRepresentation("osgi").count());
66  		assertEquals(1, inez.findAllByRepresentation("bitch").count());
67  		assertEquals(1, putty.size());
68  	}
69  
70  	private Set<Bridi> assertQuery(final Set<String> expected, final String query) {
71  		Set<Bridi> result = inez.query(query).collect(Collectors.toSet());
72  		Set<String> actual =
73  				result.stream().map(bridi -> bridi.representation()).collect(Collectors.toSet());
74  		if (!expected.equals(actual)) {
75  			System.out.println("actual:");
76  			actual.forEach(System.out::println);
77  			System.out.println("expected:");
78  			expected.forEach(System.out::println);
79  			TestUtil.diffCollections(expected, actual);
80  		}
81  		assertEquals(expected, actual);
82  		return result;
83  	}
84  }