Skip to content

Commit

Permalink
test: create wine factory scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Mar 16, 2024
1 parent 46ff246 commit e7bcdc4
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024 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.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.Objects;

@Entity
public class Wine {

@Id
private String id;

@Column
private String name;

@Column
private WineFactory factory;

public String getId() {
return id;
}

public String getName() {
return name;
}

public WineFactory getFactory() {
return factory;
}

void setId(String id) {
this.id = id;
}

void setName(String name) {
this.name = name;
}

void setFactory(WineFactory factory) {
this.factory = factory;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Wine wine = (Wine) o;
return Objects.equals(id, wine.id);
}

@Override
public int hashCode() {
return Objects.hashCode(id);
}

public static Wine of(String id, String name, WineFactory factory) {
Wine wine = new Wine();
wine.name = name;
wine.id = id;
wine.factory = factory;
return wine;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2024 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.nosql.Column;
import jakarta.nosql.Embeddable;

import static jakarta.nosql.Embeddable.EmbeddableType.GROUPING;

@Embeddable(GROUPING)
public class WineFactory {

@Column
private String name;

@Column
private String location;

public String getName() {
return name;
}

public String getLocation() {
return location;
}

void setName(String name) {
this.name = name;
}

void setLocation(String location) {
this.location = location;
}

@Override
public String toString() {
return "WineFactory{" +
"name='" + name + '\'' +
", location='" + location + '\'' +
'}';
}

public static WineFactory of(String name, String location) {
WineFactory factory = new WineFactory();
factory.name = name;
factory.location = location;
return factory;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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;

import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.metadata.FieldMetadata;
import org.eclipse.jnosql.mapping.metadata.GenericFieldMetadata;
import org.eclipse.jnosql.mapping.metadata.MappingType;
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.List;
import java.util.Map;
import java.util.Optional;

class WineTest {

private EntitiesMetadata mappings;

private EntityMetadata entityMetadata;

@BeforeEach
public void setUp() {
this.mappings = new LiteEntitiesMetadata();
this.entityMetadata = this.mappings.get(Wine.class);
}

@Test
void shouldGetName() {
Assertions.assertEquals("Wine", entityMetadata.name());
}

@Test
void shouldGetSimpleName() {
Assertions.assertEquals(Wine.class.getSimpleName(), entityMetadata.simpleName());
}

@Test
void shouldGetClassName() {
Assertions.assertEquals(Wine.class.getName(), entityMetadata.className());
}

@Test
void shouldGetClassInstance() {
Assertions.assertEquals(Wine.class, entityMetadata.type());
}

@Test
void shouldGetId() {
Optional<FieldMetadata> id = this.entityMetadata.id();
Assertions.assertTrue(id.isPresent());
}

@Test
void shouldCreateNewInstance() {
Wine wine = entityMetadata.newInstance();
Assertions.assertNotNull(wine);
Assertions.assertTrue(wine instanceof Wine);
}

@Test
void shouldGetFieldsName() {
List<String> fields = entityMetadata.fieldsName();
Assertions.assertEquals(3, fields.size());
Assertions.assertTrue(fields.contains("id"));
Assertions.assertTrue(fields.contains("name"));
Assertions.assertTrue(fields.contains("factory"));
}

@Test
void shouldGetFieldsGroupByName() {
Map<String, FieldMetadata> groupByName = this.entityMetadata.fieldsGroupByName();
Assertions.assertNotNull(groupByName);
Assertions.assertNotNull(groupByName.get("_id"));
Assertions.assertNotNull(groupByName.get("name"));
Assertions.assertNotNull(groupByName.get("factory"));
}


@Test
public void shouldReturnGrouping() {
Map<String, FieldMetadata> groupByName = this.entityMetadata.fieldsGroupByName();
FieldMetadata factory = groupByName.get("factory");
SoftAssertions.assertSoftly(soft -> {
soft.assertThat(factory.type()).isEqualTo(WineFactory.class);
soft.assertThat(factory.mappingType()).isEqualTo(MappingType.EMBEDDED_GROUP);
});
}
}

0 comments on commit e7bcdc4

Please sign in to comment.