Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. @Data
  2. @Entity
  3. public class Dojo {
  4.  
  5. private @Id @GeneratedValue Long id;
  6. private String name;
  7. private String location;
  8. private Date created;
  9. @OneToMany(mappedBy = "dojo")
  10. @JsonIgnore
  11. private List<Workshop> workshops;
  12.  
  13. private Dojo() {}
  14.  
  15. public Dojo(String name, String location) {
  16. this.name = name;
  17. this.location = location;
  18. this.created = new Date();
  19. this.workshops = new ArrayList<>();
  20. }
  21. //getters and setters ...
  22. }
  23.  
  24. @Data
  25. @Entity
  26. public class Workshop {
  27.  
  28. private @Id @GeneratedValue Long id;
  29. private String name;
  30. @ManyToOne
  31. private Dojo dojo;
  32.  
  33. private Workshop() {}
  34.  
  35. public Workshop(String name, Dojo dojo) {
  36. this.name = name;
  37. this.dojo = dojo;
  38. }
  39. }
  40.  
  41. public interface WorkshopRepository extends CrudRepository<Workshop, Long> {}
  42.  
  43. @Test
  44. public void testUpdateWorkshop() throws Exception {
  45.  
  46. final String DOJO_NAME="My Dojo";
  47. final String DOJO_LOCATION="Liege";
  48. final String WORKSHOP_NAME="Stuff";
  49. final String HOST_PORT="http://localhost:8080";
  50.  
  51. //creation of a dojo
  52. final Dojo DOJO = dojoRep.save(new Dojo(DOJO_NAME,DOJO_LOCATION));
  53. //creation of a workshop
  54. Workshop workshop = workshopRep.save(new Workshop(WORKSHOP_NAME,DOJO));
  55.  
  56. String newValue = "After Test";
  57.  
  58. System.out.println("before update");
  59. System.out.println(workshop.getName()+" == "+WORKSHOP_NAME);
  60.  
  61. Long oldID = workshop.getId();
  62.  
  63. //As you can see I didn't modify the workshop object
  64. HttpEntity<Workshop> entity = new HttpEntity<Workshop>(workshop);
  65. ResponseEntity<String> response = template.exchange(HOST_PORT+"/api/workshops/"+oldID, HttpMethod.PUT, entity, String.class, oldID);
  66.  
  67. assert response.getStatusCodeValue() == 200;
  68.  
  69. //re-Get the updated workshop
  70. workshop = workshopRep.findOne(oldID);
  71.  
  72. System.out.println("after update");
  73. System.out.println(workshop.getName()+" == "+WORKSHOP_NAME);
  74.  
  75. // as I didn't set the newValue, it must fail and workshop.getName() must stay equal to "Stuff".
  76. Assert.assertEquals("Update does not work",newValue,workshop.getName());
  77. }
  78.  
  79. before update
  80. Stuff == Stuff
  81.  
  82. after update
  83. My Dojo == Stuff
  84.  
  85. Failed tests:
  86. WorkshopTests.testUpdateWorkshop:218 Update not work expected:<[After Test]> but was:<[My Dojo]>
  87.  
  88. mockMvc.perform(put(HOST_PORT+"/api/workshops/"+oldID)
  89. .contentType(MediaType.APPLICATION_JSON_UTF8)
  90. .content(convertObjectToJsonBytes(workshop))
  91. );
  92.  
  93. private byte[] convertObjectToJsonBytes(Object object) throws IOException {
  94. ObjectMapper mapper = new ObjectMapper();
  95. System.out.println("log my face ");
  96. System.out.println(mapper.writeValueAsString(object));
  97. return mapper.writeValueAsBytes(object);
  98. }
  99.  
  100. {"id":1,"name":"Stuff","dojo":{"id":1,"name":"My Dojo","location":"Liege","created":1500799092330}}
  101.  
  102. {
  103. "name" : "Stuff",
  104. "_links" : {
  105. "self" : {
  106. "href" : "http://localhost-core:8080/api/workshops/1"
  107. },
  108. "workshop" : {
  109. "href" : "http://localhost-core:8080/api/workshops/1"
  110. },
  111. "dojo" : {
  112. "href" : "http://localhost-core:8080/api/workshops/1/dojo"
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement