StackDrenny
StackDrenny

Reputation: 175

JpaRepository findById method returning Optional.empty when id is matching to the correct database row?

For some reason my PluginRepository interface doesn't want to return the object saved in my database in this test here.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PluginServiceIntegrationTest {

@Autowired
private PluginRepository pluginRepository;

@Autowired
private PluginService pluginService;

@Autowired
private PluginController pluginController;

@LocalServerPort
private int port;

private String resourceUrl;

@Before
public void setUp() {
    resourceUrl = "http://localhost:" + port + "/plugin/";
}

@Test
public void notNullTest() {
    assertNotNull(pluginRepository);
    assertNotNull(pluginService);
    assertNotNull(pluginController);
}

@Test
public void postPluginTest() {
    RestTemplate restTemplate = new RestTemplate();

    PluginDTO requestDto = new PluginDTO();

    HttpEntity<PluginDTO> request = new HttpEntity<>(requestDto);
    ResponseEntity<PluginDTO> response = restTemplate.exchange(resourceUrl + "create", HttpMethod.POST, request, PluginDTO.class);

    assertEquals(response.getStatusCode(), HttpStatus.OK);
    PluginDTO pluginDTO = response.getBody();
    assertNotNull(pluginDTO);
}

@Test
public void getPluginTest() throws Exception {
    PluginDTO createdDto = new PluginDTO(UUID.fromString("20a246e8-7b74-4081-bb62-6911b8ef43ef"),"Foo", "0.1", "TestAuthor");
    Plugin created = new Plugin(createdDto.getUuid(), createdDto.getName(), createdDto.getVersion(), createdDto.getAuthor());
    pluginRepository.saveAndFlush(created);

    pluginRepository.findAll().forEach(plugin -> System.out.print(plugin.toString()));

    try {
        Plugin plugin = pluginRepository.findById(createdDto.getUuid());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    System.out.println(pluginService.fetchPlugin(createdDto.getUuid()).toString());

    assertEquals(pluginService.fetchPlugin(createdDto.getUuid()), created);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<PluginDTO> response = restTemplate.getForEntity(resourceUrl + createdDto.getUuid(), PluginDTO.class);

    assertEquals(response.getStatusCode(), HttpStatus.OK);
}

@After
public void tearDown() {
    pluginRepository.deleteAllInBatch();
}
}

    System.out.println(pluginRepository.findById(createdDto.getUuid()).get().toString()); // this line 

    assertEquals(pluginService.fetchPlugin(createdDto.getUuid()), created);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<PluginDTO> response = restTemplate.getForEntity(resourceUrl + createdDto.getUuid(), PluginDTO.class);

    assertEquals(response.getStatusCode(), HttpStatus.OK);
}

Even though the fixed values are set by the createdDto object it doesn't return it and I can see the values are in the database as well. This is what it returns...

20a246e8-7b74-4081-bb62-6911b8ef43ef:Foo:0.1:TestAuthor
java.util.NoSuchElementException: No value present

at java.util.Optional.get(Optional.java:135)
at net.ihq.plugin.controller.PluginServiceIntegrationTest.getPluginTest(PluginServiceIntegrationTest.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I am very stumped as I don't understand why it can save the object but can't get it back with the same fixed values I set it with, but it is actually there from me print the list of objects in the database on the line before the error occurs.

Repository

@Repository
public interface PluginRepository extends JpaRepository<Plugin, UUID> {

@Query(value = "SELECT * FROM plugins WHERE name=?", nativeQuery = true)
Plugin findByName(String name);
}

Entity

@Data
@Entity
@Table(name = "plugins")
@NoArgsConstructor
public class Plugin {

@Id
@Convert(converter = UUIDConverter.class)
@Column(unique = true, updatable = false, length = 36)
private UUID uuid;
private String name;
private String version;
private String author;

public Plugin(UUID uuid, String name, String version, String author) {
    this.uuid = uuid;
    this.name = name;
    this.version = version;

    if (author == null) author = "JoeTAMatthews";

    this.author = author;
}

public Plugin(String name, String version, String author) {
    this(UUID.randomUUID(), name, version, author);
}

public void update(PluginDTO updated) {
    this.name = updated.getName();
    this.version = updated.getVersion();
    this.author = updated.getAuthor();
}

@Override
public String toString() {
    return uuid.toString() + ":" + name + ":" + version + ":" + author;
}
}

Upvotes: 2

Views: 2396

Answers (1)

StackDrenny
StackDrenny

Reputation: 175

Sorry for the inconvenience but the reason it doesn't work is because the UUID type is not supported in the database, so all I had to do was get the string type of the uuid to actually get a result because of my UUIDConverter, thanks for all your help.

Upvotes: 2

Related Questions