#2233 Failing test

This commit is contained in:
rbygrave
2021-05-21 08:33:31 +12:00
parent b17593ba3c
commit 538361d5fd
4 changed files with 210 additions and 0 deletions
@@ -0,0 +1,36 @@
package org.tests.lazyloadconf;
import io.ebean.annotation.Cache;
import javax.persistence.*;
import java.util.List;
@Entity
@Cache
@Table(name = "app_config")
public class AppConfig {
@Id
@Column(name = "id")
private Integer id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "appConfig")
//@JoinColumn(name = "id", referencedColumnName = "id")
private List<AppConfigControl> items;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<AppConfigControl> getItems() {
return items;
}
public void setItems(List<AppConfigControl> items) {
this.items = items;
}
}
@@ -0,0 +1,42 @@
package org.tests.lazyloadconf;
import javax.persistence.*;
@Entity
@Table(name = "app_config_control")
public class AppConfigControl {
@Id
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "config_id", referencedColumnName = "id")
private AppConfig appConfig;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AppConfig getAppConfig() {
return appConfig;
}
public void setAppConfig(AppConfig appConfig) {
this.appConfig = appConfig;
}
}
@@ -0,0 +1,97 @@
package org.tests.lazyloadconf;
import io.ebean.Ebean;
import io.ebean.Query;
import org.junit.Test;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
public class MyTest {
@Test
public void testRe() {
AppConfig globalAppConfig = new AppConfig();
globalAppConfig.setId(1);
globalAppConfig.setItems(new ArrayList<>());
AppConfigControl global = new AppConfigControl();
global.setId(1);
global.setName("global");
global.setAppConfig(globalAppConfig);
globalAppConfig.getItems().add(global);
Ebean.save(globalAppConfig);
AppConfig userAppConfig = new AppConfig();
userAppConfig.setId(2);
userAppConfig.setItems(new ArrayList<>());
AppConfigControl user = new AppConfigControl();
user.setId(2);
user.setName("user");
user.setAppConfig(userAppConfig);
userAppConfig.getItems().add(user);
Ebean.save(userAppConfig);
AppConfig otherAppConfig = new AppConfig();
otherAppConfig.setId(3);
otherAppConfig.setItems(new ArrayList<>());
Ebean.save(otherAppConfig);
Relationship globalRe = new Relationship();
globalRe.setId(1);
globalRe.setAppConfig(globalAppConfig);
Ebean.save(globalRe);
Relationship userRe = new Relationship();
userRe.setId(2);
userRe.setAppConfig(userAppConfig);
Ebean.save(userRe);
Relationship otherRe = new Relationship();
otherRe.setId(3);
otherRe.setAppConfig(otherAppConfig);
Ebean.save(otherRe);
// Start business processing
Query<Relationship> relationshipQuery = Ebean.find(Relationship.class);
List<Relationship> relationshipList = relationshipQuery.where().idIn(1, 2, 3).findList();
assertThat(relationshipList.size()).isEqualTo(3);
Map<Integer, AppConfig> map = relationshipList.stream()
.map(Relationship::getAppConfig)
.map((ac) -> new AbstractMap.SimpleImmutableEntry<>(ac.getId(), ac))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// final List<AppConfig> items = DB.find(AppConfig.class)
// .fetch("items")
// .findList();
AppConfig g = map.get(1);
AppConfig u = map.get(2);
AppConfig o = map.get(3);
if(!u.getItems().isEmpty()){
g.setItems(u.getItems());
}
assertThat(g.getItems().size()).isEqualTo(1);
// If this line of code is commented out, this test case will be successfully passed
assertThat(o.getItems().size()).isEqualTo(0);
// org.junit.ComparisonFailure:
// Expected :1
// Actual :2
assertThat(g.getItems().size()).isEqualTo(1);
assertThat(g.getItems().get(0).getName()).isEqualTo("user");
}
}
@@ -0,0 +1,35 @@
package org.tests.lazyloadconf;
import io.ebean.annotation.Cache;
import javax.persistence.*;
@Entity
@Cache(enableBeanCache = true)
@Table(name="app_config_re")
public class Relationship {
@Id
private Integer id;
@ManyToOne
@JoinColumn(name="config_id",referencedColumnName="id")
private AppConfig appConfig;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public AppConfig getAppConfig() {
return appConfig;
}
public void setAppConfig(AppConfig appConfig) {
this.appConfig = appConfig;
}
}