Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Apr 22, 2023
1 parent c139763 commit aee04bd
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public abstract class GxsMessageItem extends Item implements GxsMetaAndData
@AttributeOverride(name = "identifier", column = @Column(name = "author_id"))
private GxsId authorId;

private String name; // tlv string message name (use serialize(buf, TlvType.STRING, name);
private String name;

@UpdateTimestamp
private Instant published; // publishts (32-bits)
Expand Down Expand Up @@ -129,6 +129,16 @@ public void setAuthorId(GxsId authorId)
this.authorId = authorId;
}

public String getName()
{
return name;
}

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

public Instant getPublished()
{
return published;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.xeres.app.database.model.gxs.GxsMessageItem;
import io.xeres.app.xrs.serialization.SerializationFlags;
import io.xeres.app.xrs.serialization.Serializer;
import io.xeres.common.id.GxsId;
import io.xeres.common.id.MessageId;
import jakarta.persistence.Entity;

import java.util.Set;
Expand All @@ -40,6 +42,13 @@ public ForumMessageItem()
// Needed for JPA
}

public ForumMessageItem(GxsId groupId, MessageId messageId, String name)
{
setGxsId(groupId);
setMessageId(messageId);
setName(name);
}

public String getContent()
{
return content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package io.xeres.app.api.controller.chat;

import io.xeres.app.api.controller.AbstractControllerTest;
import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import io.xeres.app.database.model.location.LocationFakes;
import io.xeres.app.xrs.service.chat.ChatRsService;
import io.xeres.app.xrs.service.chat.RoomFlags;
Expand Down Expand Up @@ -123,7 +123,7 @@ void ChatController_GetChatRoomContext_OK() throws Exception
var chatRoomLists = new ChatRoomLists();
chatRoomLists.addSubscribed(subscribedChatRoom);
chatRoomLists.addAvailable(availableChatRoom);
var ownIdentity = GxsIdFakes.createOwnIdentity();
var ownIdentity = IdentityFakes.createOwnIdentity();
var chatRoomUser = new ChatRoomUser(ownIdentity.getName(), ownIdentity.getGxsId(), ownIdentity.getImage());
when(chatRsService.getChatRoomContext()).thenReturn(new ChatRoomContext(chatRoomLists, chatRoomUser));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import io.xeres.app.api.controller.AbstractControllerTest;
import io.xeres.app.database.model.connection.Connection;
import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import io.xeres.app.database.model.location.Location;
import io.xeres.app.net.protocol.PeerAddress;
import io.xeres.app.service.CapabilityService;
Expand Down Expand Up @@ -311,7 +311,7 @@ void ConfigController_GetUsername_OK() throws Exception
@Test
void ConfigController_CreateIdentity_Signed_OK() throws Exception
{
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var identityRequest = new OwnIdentityRequest(identity.getName(), false);

when(identityService.createOwnIdentity(identityRequest.name(), true)).thenReturn(identity.getId());
Expand All @@ -326,7 +326,7 @@ void ConfigController_CreateIdentity_Signed_OK() throws Exception
@Test
void ConfigController_CreateIdentity_Anonymous_OK() throws Exception
{
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var identityRequest = new OwnIdentityRequest(identity.getName(), true);

when(identityService.createOwnIdentity(identityRequest.name(), false)).thenReturn(identity.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package io.xeres.app.database.model.chat;

import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import io.xeres.app.xrs.service.identity.item.IdentityGroupItem;
import io.xeres.common.message.chat.RoomType;
import org.apache.commons.lang3.RandomStringUtils;
Expand All @@ -35,7 +35,7 @@ private ChatRoomFakes()

public static ChatRoom createChatRoomEntity()
{
return createChatRoomEntity(ThreadLocalRandom.current().nextLong(), GxsIdFakes.createOwnIdentity(), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), 0);
return createChatRoomEntity(ThreadLocalRandom.current().nextLong(), IdentityFakes.createOwnIdentity(), RandomStringUtils.randomAlphabetic(8), RandomStringUtils.randomAlphabetic(8), 0);
}

public static ChatRoom createChatRoomEntity(IdentityGroupItem identityGroupItem)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import io.xeres.app.xrs.service.forum.ForumRsService;
import io.xeres.app.xrs.service.forum.item.ForumGroupItem;
import io.xeres.common.id.GxsId;
import io.xeres.testutils.GxsIdFakes;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.time.Instant;
import java.util.EnumSet;
Expand All @@ -37,7 +37,7 @@ private ForumGroupItemFakes()

public static ForumGroupItem createForumGroupItem()
{
return createForumGroupItem(new GxsId(RandomUtils.nextBytes(16)), RandomStringUtils.randomAlphabetic(8));
return createForumGroupItem(GxsIdFakes.createGxsId(), RandomStringUtils.randomAlphabetic(8));
}

public static ForumGroupItem createForumGroupItem(GxsId gxsId, String name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.app.database.model.gxs;

import io.xeres.app.xrs.service.forum.ForumRsService;
import io.xeres.app.xrs.service.forum.item.ForumMessageItem;
import io.xeres.common.id.GxsId;
import io.xeres.common.id.MessageId;
import io.xeres.testutils.GxsIdFakes;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.time.Instant;

public final class ForumMessageItemFakes
{
private ForumMessageItemFakes()
{
throw new UnsupportedOperationException("Utility class");
}

public static ForumMessageItem createForumMessageItem()
{
return createForumMessageItem(GxsIdFakes.createGxsId(), new MessageId(RandomUtils.nextBytes(20)), RandomStringUtils.randomAlphabetic(8));
}

private static ForumMessageItem createForumMessageItem(GxsId gxsId, MessageId messageId, String name)
{
var item = new ForumMessageItem(gxsId, messageId, name);
item.setPublished(Instant.now());
item.setService(new ForumRsService(null, null, null, null, null));
return item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import io.xeres.app.xrs.service.identity.item.IdentityGroupItem;
import io.xeres.common.id.GxsId;
import io.xeres.common.id.Sha1Sum;
import io.xeres.testutils.GxsIdFakes;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.time.Instant;
import java.util.EnumSet;
Expand All @@ -38,7 +38,7 @@ private IdentityGroupItemFakes()

public static IdentityGroupItem createIdentityGroupItem()
{
return createIdentityGroupItem(new GxsId(RandomUtils.nextBytes(16)), RandomStringUtils.randomAlphabetic(8));
return createIdentityGroupItem(GxsIdFakes.createGxsId(), RandomStringUtils.randomAlphabetic(8));
}

public static IdentityGroupItem createIdentityGroupItem(GxsId gxsId, String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

import static io.xeres.common.dto.identity.IdentityConstants.OWN_IDENTITY_ID;

public final class GxsIdFakes
public final class IdentityFakes
{
private GxsIdFakes()
private IdentityFakes()
{
throw new UnsupportedOperationException("Utility class");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void IdentityMapper_NoInstanceOK() throws NoSuchMethodException
@Test
void IdentityMapper_toDTO_OK()
{
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var identityDTO = IdentityMapper.toDTO(identity);

assertEquals(identity.getId(), identityDTO.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package io.xeres.app.database.repository;

import io.xeres.app.database.model.chat.ChatRoomFakes;
import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
Expand All @@ -36,7 +36,7 @@ class ChatRoomRepositoryTest
@Test
void ChatRoomRepository_CRUD_OK()
{
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();

var chatRoom1 = ChatRoomFakes.createChatRoomEntity(identity);
var chatRoom2 = ChatRoomFakes.createChatRoomEntity(identity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import io.xeres.app.database.model.chat.ChatRoom;
import io.xeres.app.database.model.chat.ChatRoomFakes;
import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import io.xeres.app.database.repository.ChatRoomRepository;
import io.xeres.common.message.chat.RoomType;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -50,15 +50,15 @@ class ChatRoomServiceTest
@Test
void ChatRoomService_CreateChatRoom_OK()
{
chatRoomService.createChatRoom(createSignedChatRoom(), GxsIdFakes.createOwnIdentity());
chatRoomService.createChatRoom(createSignedChatRoom(), IdentityFakes.createOwnIdentity());
verify(chatRoomRepository).save(any(ChatRoom.class));
}

@Test
void ChatRoomService_SubscribeToChatRoomAndJoin_OK()
{
var serviceChatRoom = createSignedChatRoom();
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var chatRoom = ChatRoomFakes.createChatRoomEntity(serviceChatRoom.getId(), identity, serviceChatRoom.getName(), serviceChatRoom.getTopic(), 0);

when(chatRoomRepository.findByRoomIdAndIdentityGroupItem(chatRoom.getRoomId(), identity)).thenReturn(Optional.of(chatRoom));
Expand All @@ -77,7 +77,7 @@ void ChatRoomService_SubscribeToChatRoomAndJoin_OK()
void ChatRoomService_UnsubscribeFromChatRoomAndLeave_OK()
{
var serviceChatRoom = createSignedChatRoom();
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var chatRoom = ChatRoomFakes.createChatRoomEntity(serviceChatRoom.getId(), identity, serviceChatRoom.getName(), serviceChatRoom.getTopic(), 0);

when(chatRoomRepository.findByRoomIdAndIdentityGroupItem(chatRoom.getRoomId(), identity)).thenReturn(Optional.of(chatRoom));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package io.xeres.app.service;

import io.xeres.app.crypto.pgp.PGP;
import io.xeres.app.database.model.identity.GxsIdFakes;
import io.xeres.app.database.model.identity.IdentityFakes;
import io.xeres.app.database.model.profile.ProfileFakes;
import io.xeres.app.database.repository.GxsIdentityRepository;
import io.xeres.app.xrs.service.identity.item.IdentityGroupItem;
Expand Down Expand Up @@ -143,7 +143,7 @@ void IdentityService_CreateOwnIdentity_Signed_OK() throws PGPException, Certific
void IdentityService_SaveIdentityImage_OK() throws IOException
{
var id = 1L;
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
var file = new MockMultipartFile("file", getClass().getResourceAsStream("/image/leguman.jpg"));

when(gxsIdentityRepository.findById(id)).thenReturn(Optional.of(identity));
Expand Down Expand Up @@ -187,7 +187,7 @@ void IdentityService_SaveIdentityImage_ImageTooBig_Error()
void IdentityService_DeleteIdentityImage_OK()
{
var id = 1L;
var identity = GxsIdFakes.createOwnIdentity();
var identity = IdentityFakes.createOwnIdentity();
identity.setImage(new byte[1]);

when(gxsIdentityRepository.findById(id)).thenReturn(Optional.of(identity));
Expand Down
Loading

0 comments on commit aee04bd

Please sign in to comment.