Skip to content

Commit f0056b1

Browse files
committed
feat: include code
Signed-off-by: Otavio Santana <[email protected]>
1 parent 6fe9ec0 commit f0056b1

File tree

13 files changed

+596
-0
lines changed

13 files changed

+596
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
public enum CleanStatus {
4+
CLEAN,
5+
DIRTY,
6+
INSPECTION_NEEDED
7+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Entity;
5+
import jakarta.nosql.Id;
6+
7+
import java.util.Objects;
8+
9+
@Entity
10+
public class Room {
11+
12+
@Id
13+
private String id;
14+
15+
@Column
16+
private int number;
17+
18+
@Column
19+
private RoomType type;
20+
21+
@Column
22+
private RoomStatus status;
23+
24+
@Column
25+
private CleanStatus cleanStatus;
26+
27+
@Column
28+
private boolean smokingAllowed;
29+
30+
@Column
31+
private boolean underMaintenance;
32+
33+
34+
public Room() {
35+
}
36+
37+
public Room(String id, int number,
38+
RoomType type, RoomStatus status,
39+
CleanStatus cleanStatus,
40+
boolean smokingAllowed, boolean underMaintenance) {
41+
this.id = id;
42+
this.number = number;
43+
this.type = type;
44+
this.status = status;
45+
this.cleanStatus = cleanStatus;
46+
this.smokingAllowed = smokingAllowed;
47+
this.underMaintenance = underMaintenance;
48+
}
49+
50+
public String getId() {
51+
return id;
52+
}
53+
54+
public int getNumber() {
55+
return number;
56+
}
57+
58+
public RoomType getType() {
59+
return type;
60+
}
61+
62+
public RoomStatus getStatus() {
63+
return status;
64+
}
65+
66+
public CleanStatus getCleanStatus() {
67+
return cleanStatus;
68+
}
69+
70+
public boolean isSmokingAllowed() {
71+
return smokingAllowed;
72+
}
73+
74+
public boolean isUnderMaintenance() {
75+
return underMaintenance;
76+
}
77+
78+
79+
@Override
80+
public boolean equals(Object o) {
81+
if (o == null || getClass() != o.getClass()) {
82+
return false;
83+
}
84+
Room room = (Room) o;
85+
return Objects.equals(id, room.id);
86+
}
87+
88+
@Override
89+
public int hashCode() {
90+
return Objects.hashCode(id);
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return "Room{" +
96+
"id='" + id + '\'' +
97+
", roomNumber=" + number +
98+
", type=" + type +
99+
", status=" + status +
100+
", cleanStatus=" + cleanStatus +
101+
", smokingAllowed=" + smokingAllowed +
102+
", underMaintenance=" + underMaintenance +
103+
'}';
104+
}
105+
106+
107+
public static RoomBuilder builder() {
108+
return new RoomBuilder();
109+
}
110+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
public class RoomBuilder {
4+
private String id;
5+
private int roomNumber;
6+
private RoomType type;
7+
private RoomStatus status;
8+
private CleanStatus cleanStatus;
9+
private boolean smokingAllowed;
10+
private boolean underMaintenance;
11+
12+
public RoomBuilder id(String id) {
13+
this.id = id;
14+
return this;
15+
}
16+
17+
public RoomBuilder roomNumber(int roomNumber) {
18+
this.roomNumber = roomNumber;
19+
return this;
20+
}
21+
22+
public RoomBuilder type(RoomType type) {
23+
this.type = type;
24+
return this;
25+
}
26+
27+
public RoomBuilder status(RoomStatus status) {
28+
this.status = status;
29+
return this;
30+
}
31+
32+
public RoomBuilder cleanStatus(CleanStatus cleanStatus) {
33+
this.cleanStatus = cleanStatus;
34+
return this;
35+
}
36+
37+
public RoomBuilder smokingAllowed(boolean smokingAllowed) {
38+
this.smokingAllowed = smokingAllowed;
39+
return this;
40+
}
41+
42+
public RoomBuilder underMaintenance(boolean underMaintenance) {
43+
this.underMaintenance = underMaintenance;
44+
return this;
45+
}
46+
47+
public Room build() {
48+
return new Room(id, roomNumber, type, status, cleanStatus, smokingAllowed, underMaintenance);
49+
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.data.repository.Param;
4+
import jakarta.data.repository.Query;
5+
import jakarta.data.repository.Repository;
6+
import jakarta.data.repository.Save;
7+
8+
import java.util.List;
9+
10+
@Repository
11+
public interface RoomRepository {
12+
13+
@Query("WHERE type = 'VIP_SUITE' AND status = 'AVAILABLE' AND underMaintenance = false")
14+
List<Room> findVipRoomsReadyForGuests();
15+
16+
@Query(" WHERE type <> 'VIP_SUITE' AND status = 'AVAILABLE' AND cleanStatus = 'CLEAN'")
17+
List<Room> findAvailableStandardRooms();
18+
19+
@Query("WHERE cleanStatus <> 'CLEAN' AND status <> 'OUT_OF_SERVICE'")
20+
List<Room> findRoomsNeedingCleaning();
21+
22+
@Query("WHERE smokingAllowed = true AND status = 'AVAILABLE'")
23+
List<Room> findAvailableSmokingRooms();
24+
25+
@Save
26+
void save(List<Room> rooms);
27+
28+
@Save
29+
Room newRoom(Room room);
30+
void deleteBy();
31+
32+
@Query("WHERE type = :type")
33+
List<Room> findByType(@Param("type") RoomType type);
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
public enum RoomStatus {
4+
AVAILABLE,
5+
RESERVED,
6+
UNDER_MAINTENANCE,
7+
OUT_OF_SERVICE
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
public enum RoomType {
4+
STANDARD,
5+
DELUXE,
6+
SUITE,
7+
VIP_SUITE
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
~ Copyright (c) 2022 Contributors to the Eclipse Foundation
3+
~ All rights reserved. This program and the accompanying materials
4+
~ are made available under the terms of the Eclipse Public License v1.0
5+
~ and Apache License v2.0 which accompanies this distribution.
6+
~ The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
~ and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
~
9+
~ You may elect to redistribute this code under either of these licenses.
10+
-->
11+
12+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
13+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
14+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
15+
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
16+
bean-discovery-mode="all">
17+
</beans>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (c) 2022 Contributors to the Eclipse Foundation
3+
# All rights reserved. This program and the accompanying materials
4+
# are made available under the terms of the Eclipse Public License v1.0
5+
# and Apache License v2.0 which accompanies this distribution.
6+
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
# and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
#
9+
# You may elect to redistribute this code under either of these licenses.
10+
#
11+
jnosql.mongodb.url=mongodb://localhost:27017
12+
# mandatory define the database name
13+
jnosql.document.database=products
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import jakarta.inject.Inject;
4+
import org.assertj.core.api.SoftAssertions;
5+
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
6+
import org.eclipse.jnosql.mapping.Database;
7+
import org.eclipse.jnosql.mapping.core.Converters;
8+
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
9+
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
10+
import org.eclipse.jnosql.mapping.reflection.Reflections;
11+
import org.eclipse.jnosql.mapping.reflection.spi.ReflectionEntityMetadataExtension;
12+
import org.eclipse.jnosql.mapping.semistructured.EntityConverter;
13+
import org.jboss.weld.junit5.auto.AddExtensions;
14+
import org.jboss.weld.junit5.auto.AddPackages;
15+
import org.jboss.weld.junit5.auto.EnableAutoWeld;
16+
import org.junit.jupiter.api.Test;
17+
18+
19+
@EnableAutoWeld
20+
@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class, MongoDBTemplate.class})
21+
@AddPackages(Room.class)
22+
@AddPackages(ManagerSupplier.class)
23+
@AddPackages(MongoDBTemplate.class)
24+
@AddPackages(Reflections.class)
25+
@AddPackages(Converters.class)
26+
@AddExtensions({ReflectionEntityMetadataExtension.class, DocumentExtension.class})
27+
class AppTest {
28+
29+
@Inject
30+
private DocumentTemplate template;
31+
32+
@Test
33+
void shouldTest() {
34+
Room room = new RoomBuilder()
35+
.id("room-1")
36+
.roomNumber(101)
37+
.type(RoomType.SUITE)
38+
.status(RoomStatus.AVAILABLE)
39+
.cleanStatus(CleanStatus.CLEAN)
40+
.smokingAllowed(false)
41+
.underMaintenance(false)
42+
.build();
43+
44+
Room insert = template.insert(room);
45+
SoftAssertions.assertSoftly(softly -> {
46+
softly.assertThat(room.getId()).isEqualTo(insert.getId());
47+
softly.assertThat(room.getNumber()).isEqualTo(insert.getNumber());
48+
softly.assertThat(room.getType()).isEqualTo(insert.getType());
49+
softly.assertThat(room.getStatus()).isEqualTo(insert.getStatus());
50+
softly.assertThat(room.getCleanStatus()).isEqualTo(insert.getCleanStatus());
51+
softly.assertThat(room.isSmokingAllowed()).isEqualTo(insert.isSmokingAllowed());
52+
softly.assertThat(room.isUnderMaintenance()).isEqualTo(insert.isUnderMaintenance());
53+
softly.assertThat(insert.getId()).isNotNull();
54+
});
55+
}
56+
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.soujava.demos.mongodb.document;
2+
3+
import org.eclipse.jnosql.communication.Settings;
4+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfiguration;
5+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
6+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentManager;
7+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentManagerFactory;
8+
import org.eclipse.jnosql.mapping.core.config.MappingConfigurations;
9+
import org.testcontainers.containers.GenericContainer;
10+
import org.testcontainers.containers.wait.strategy.Wait;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
public enum DatabaseContainer {
15+
16+
INSTANCE;
17+
18+
private final GenericContainer<?> mongodb =
19+
new GenericContainer<>("mongo:latest")
20+
.withExposedPorts(27017)
21+
.waitingFor(Wait.defaultWaitStrategy());
22+
23+
{
24+
mongodb.start();
25+
}
26+
public MongoDBDocumentManager get(String database) {
27+
Settings settings = getSettings(database);
28+
MongoDBDocumentConfiguration configuration = new MongoDBDocumentConfiguration();
29+
MongoDBDocumentManagerFactory factory = configuration.apply(settings);
30+
return factory.apply(database);
31+
}
32+
33+
34+
private Settings getSettings(String database) {
35+
Map<String,Object> settings = new HashMap<>();
36+
settings.put(MongoDBDocumentConfigurations.HOST.get()+".1", host());
37+
settings.put(MappingConfigurations.DOCUMENT_DATABASE.get(), database);
38+
return Settings.of(settings);
39+
}
40+
41+
public String host() {
42+
return mongodb.getHost() + ":" + mongodb.getFirstMappedPort();
43+
}
44+
}

0 commit comments

Comments
 (0)