|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for |
| 4 | + * license information. |
| 5 | + */ |
| 6 | +package sample.gremlin; |
| 7 | + |
| 8 | +import com.microsoft.spring.data.gremlin.common.GremlinFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.CommandLineRunner; |
| 11 | +import org.springframework.boot.SpringApplication; |
| 12 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 13 | +import org.springframework.util.Assert; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +@SpringBootApplication |
| 19 | +public class GremlinApplication implements CommandLineRunner { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private GremlinFactory factory; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private PersonRepository repository; |
| 26 | + |
| 27 | + public static void main(String[] args) { |
| 28 | + SpringApplication.run(GremlinApplication.class, args); |
| 29 | + } |
| 30 | + |
| 31 | + private void closeClusterConnection() { |
| 32 | + if (this.factory.getGremlinCluster().isClosed() || this.factory.getGremlinCluster().isClosing()) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + this.factory.getGremlinCluster().closeAsync(); |
| 37 | + } |
| 38 | + |
| 39 | + public void run(String... vars) { |
| 40 | + final Person person = new Person("fake-id", "fake-name", 123); |
| 41 | + |
| 42 | + this.repository.deleteAll(); |
| 43 | + this.repository.save(person); |
| 44 | + |
| 45 | + final Optional<Person> foundPerson = this.repository.findById(person.getId()); |
| 46 | + Assert.isTrue(foundPerson.isPresent(), "optional of Person should be present"); |
| 47 | + Assert.state(foundPerson.get().equals(person), "should be the equals"); |
| 48 | + |
| 49 | + final List<Person> foundPersons = this.repository.findByNameAndLevel(person.getName(), person.getLevel()); |
| 50 | + Assert.isTrue(foundPersons.size() == 1, "should be only one element"); |
| 51 | + Assert.state(foundPersons.get(0).getId().equals(person.getId()), "should be the same id"); |
| 52 | + |
| 53 | + this.closeClusterConnection(); |
| 54 | + } |
| 55 | +} |
0 commit comments