Skip to content

Commit

Permalink
Merge pull request #134 from eclipse/add-constructor
Browse files Browse the repository at this point in the history
Add support to Record at Eclipse JNoSQL Lite
  • Loading branch information
otaviojava authored Oct 27, 2024
2 parents eb3234f + 9812aa0 commit 0302b06
Show file tree
Hide file tree
Showing 40 changed files with 2,189 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version

== [Unreleased]

=== Added

- Include support to Record at JNoSQL Lite

== [1.1.2] - 2023-09-15

== Added

- Include Jakarta Data TCK Runner for Eclipse JNoSQL;

== [1.1.1] - 2023-05-25
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

import java.util.List;

@Embeddable
public record Guest(@Column String name, @Column String document, @Column List<String> phones) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

@Entity
public record Hotel(@Id String number, @Column HotelManager manager) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

@Embeddable(Embeddable.EmbeddableType.GROUPING)
public record HotelManager(@Column String name, @Column String document) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

@Entity
public record Room(@Id int number, @Column Guest guest) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities;

import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.communication.TypeReference;
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.Element;
import org.eclipse.jnosql.lite.mapping.entities.record.Guest;
import org.eclipse.jnosql.lite.mapping.entities.record.Hotel;
import org.eclipse.jnosql.lite.mapping.entities.record.HotelManager;
import org.eclipse.jnosql.lite.mapping.entities.record.Room;
import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata;
import org.eclipse.jnosql.mapping.column.ColumnTemplate;
import org.eclipse.jnosql.mapping.column.spi.ColumnExtension;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension;
import org.eclipse.jnosql.mapping.semistructured.EntityConverter;
import org.jboss.weld.junit5.auto.AddExtensions;
import org.jboss.weld.junit5.auto.AddPackages;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

@EnableAutoWeld
@AddPackages(value = {Converters.class, EntityConverter.class, ColumnTemplate.class})
@AddPackages(LiteEntitiesMetadata.class)
@AddExtensions({EntityMetadataExtension.class, ColumnExtension.class})
public class ColumnEntityConverterRecordTest {

@Inject
private EntityConverter converter;

@Test
void shouldConvertToCommunication() {
Room room = new Room(1231, new Guest("Ada", "12321", asList("123", "321")));

CommunicationEntity entity = converter.toCommunication(room);

SoftAssertions.assertSoftly(s -> {
s.assertThat(entity.name()).isEqualTo("Room");
s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo(1231);
s.assertThat(entity.find("name")).isPresent().get().isEqualTo(Element.of("name", "Ada"));
s.assertThat(entity.find("document")).isPresent().get().isEqualTo(Element.of("document","12321"));
s.assertThat(entity.find("phones")).isPresent().get().isEqualTo( Element.of("phones",asList("123", "321")));
});
}


@Test
void shouldConvertToDocumentEntity() {
var entity = CommunicationEntity.of("Room");
entity.add("_id", 1231);
entity.add("name", "Ada");
entity.add("document", "12321");
entity.add("phones", List.of("123", "321"));

Room room = converter.toEntity(entity);

SoftAssertions.assertSoftly(s -> {
s.assertThat(room.number()).isEqualTo(1231);
});
}


@Test
void shouldConvertToCommunicationGroup() {
Hotel hotel = new Hotel("123", new HotelManager("Ada", "12321"));

CommunicationEntity entity = converter.toCommunication(hotel);

SoftAssertions.assertSoftly(s -> {
s.assertThat(entity.name()).isEqualTo("Hotel");
s.assertThat(entity.find("_id").orElseThrow().get()).isEqualTo("123");
s.assertThat(entity.find("manager")).isNotEmpty();
});
}

@Test
void shouldConvertToDocumentEntityGroup() {
var entity = CommunicationEntity.of("Hotel");
entity.add("_id", "123");
entity.add("manager", List.of(Element.of("name", "Ada"), Element.of("document", "12321")));

Hotel room = converter.toEntity(entity);

SoftAssertions.assertSoftly(s -> {
s.assertThat(room.number()).isEqualTo("123");
s.assertThat(room.manager().name()).isEqualTo("Ada");
s.assertThat(room.manager().document()).isEqualTo("12321");
});
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Convert;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;
import org.eclipse.jnosql.lite.mapping.entities.Money;
import org.eclipse.jnosql.lite.mapping.entities.MoneyConverter;

import java.util.Map;

@Entity
public record DocumentRecord(@Id String id, @Column Map<String, String> data, @Column @Convert(MoneyConverter.class) Money money) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Embeddable;

import java.util.List;

@Embeddable
public record Guest(@Column String name, @Column String document, @Column List<String> phones) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.Map;

@Entity
public record Hotel(@Id String document, @Column Map<String, String> socialMedias, @Column String[] cities) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.entities.record;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

@Entity
public record Room(@Id int number, @Column Guest guest) {
}
Loading

0 comments on commit 0302b06

Please sign in to comment.