Skip to content

Fixes: Byte arrays cannot (always) be decoded from the database #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version

== [Unreleased]

=== Fixed

- Fixes delete all at CouchDB

== [1.1.6] - 2025-03-24

== [1.1.5] - 2025-03-23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class CouchdbDeleteQuery implements SelectQuery {

@Override
public long limit() {
return 10;
return 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ void shouldUpdateNull(){
});
}

@Test
void shouldInsertByteArray() {
CommunicationEntity entity = CommunicationEntity.of("Failure");
entity.add(CouchDBConstant.ID, "id");
entity.add("data", new byte[]{'a','b','c','d'});
var communication = entityManager.insert(entity);

SoftAssertions.assertSoftly(soft -> {
soft.assertThat(communication).isNotNull();
soft.assertThat(communication.find("data").get().get()).isInstanceOf(byte[].class);
});
}

private CommunicationEntity createDocumentList() {
CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");
List<List<Element>> documents = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jnosql.databases.couchdb.communication.CouchDBConfigurations;
import org.eclipse.jnosql.databases.couchdb.communication.CouchDBDocumentConfiguration;
import org.eclipse.jnosql.databases.couchdb.communication.CouchDBDocumentManagerFactory;
import org.jetbrains.annotations.NotNull;
import org.testcontainers.containers.GenericContainer;

import java.util.function.Supplier;
Expand All @@ -40,10 +41,22 @@ public enum DocumentDatabase implements Supplier<CouchDBDocumentManagerFactory>
public CouchDBDocumentManagerFactory get() {
CouchDBDocumentConfiguration configuration = new CouchDBDocumentConfiguration();
SettingsBuilder builder = Settings.builder();
builder.put(CouchDBConfigurations.PORT, couchDB.getFirstMappedPort());
builder.put(CouchDBConfigurations.USER, "admin");
builder.put(CouchDBConfigurations.PASSWORD, "password");
builder.put(CouchDBConfigurations.PORT, getPort());
builder.put(CouchDBConfigurations.USER, getUser());
builder.put(CouchDBConfigurations.PASSWORD, getPassword());
return configuration.apply(builder.build());
}

public String getPassword() {
return "password";
}

public String getUser() {
return "admin";
}

public Integer getPort() {
return couchDB.getFirstMappedPort();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2025 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.databases.couchdb.communication.integration;

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

@Entity
public record Failure(
@Id String id,
@Column byte[] data) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2025 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.databases.couchdb.communication.integration;

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

@Entity
public record Magazine(@Id String id, @Column("title") String title, @Column("edition") int edition) {


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (c) 2025 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.databases.couchdb.communication.integration;


import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.databases.couchdb.communication.CouchDBConfigurations;
import org.eclipse.jnosql.databases.couchdb.communication.configuration.DocumentDatabase;
import org.eclipse.jnosql.mapping.Database;
import org.eclipse.jnosql.mapping.core.Converters;
import org.eclipse.jnosql.mapping.core.config.MappingConfigurations;
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
import org.eclipse.jnosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.mapping.reflection.spi.ReflectionEntityMetadataExtension;
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.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import java.util.List;
import java.util.Optional;

import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;

@EnableAutoWeld
@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class})
@AddPackages(Magazine.class)
@AddPackages(Reflections.class)
@AddPackages(Converters.class)
@AddExtensions({ReflectionEntityMetadataExtension.class,
DocumentExtension.class})
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
class TemplateIntegrationTest {

static {
DocumentDatabase database = DocumentDatabase.INSTANCE;
System.setProperty(CouchDBConfigurations.PORT.get(), database.getPort().toString());
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library");
System.setProperty(CouchDBConfigurations.USER.get(), database.getUser());
System.setProperty(CouchDBConfigurations.PASSWORD.get(), database.getPassword());
}

@Inject
private DocumentTemplate template;

@Test
void shouldFindById() {
Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(magazine))
.isNotNull()
.isEqualTo(magazine);

assertThat(template.find(Magazine.class, magazine.id()))
.isNotNull().get().isEqualTo(magazine);
}

@Test
void shouldInsert() {
Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1);
template.insert(magazine);
Optional<Magazine> optional = template.find(Magazine.class, magazine.id());
assertThat(optional).isNotNull().isNotEmpty()
.get().isEqualTo(magazine);
}

@Test
void shouldUpdate() {
Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(magazine))
.isNotNull()
.isEqualTo(magazine);

Magazine updated = new Magazine(magazine.id(), magazine.title() + " updated", 2);

assertThat(template.update(updated))
.isNotNull()
.isNotEqualTo(magazine);

assertThat(template.find(Magazine.class, magazine.id()))
.isNotNull().get().isEqualTo(updated);

}

@Test
void shouldDeleteById() {
Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(magazine))
.isNotNull()
.isEqualTo(magazine);

template.delete(Magazine.class, magazine.id());
assertThat(template.find(Magazine.class, magazine.id()))
.isNotNull().isEmpty();
}

@Test
void shouldDeleteAll(){
for (int index = 0; index < 20; index++) {
Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", index);
assertThat(template.insert(magazine))
.isNotNull()
.isEqualTo(magazine);
}

template.delete(Magazine.class).execute();
var magazines = template.select(Magazine.class).result();
assertThat(magazines).isNotNull().isEmpty();
}

@Test
void shouldInsertByteArray() {
var failure = new Failure("test", new byte[]{'a','b','c','d'});
template.insert(failure);
Optional<Failure> entity = template.find(Failure.class, "test");
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(entity).isNotNull().isNotEmpty();
softly.assertThat(entity).map(Failure::id).get().isEqualTo("test");
softly.assertThat(entity).map(Failure::data).get().isEqualTo(failure.data());
});
}

}