Commit ee1a94ed authored by Hans William Forbrigd's avatar Hans William Forbrigd Committed by Olaf Rosendahl
Browse files

Add more tests

parent 671e69ff
import Components.ImageAnalyzer;
import Components.UserInfo;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.User;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* Class for testing of
* ImageAnalyzer class
*/
class ImageAnalyzerTest {
/**
* Test method for analyze-method when everything is executed as expected.
* Firstly, a user is initialized. Then we create a Photo object by calling the
* analyze-method we want to test. The method then checks if all metadata of the
* photo object is not equals null or 0. The test executes successfully.
*/
@Test
void analyze_TitleAndUrlIsGiven_AllMetadataTestedSuccessfully() {
try {
User user = new User();
user.setUsername("_Test_");
user.setId(32);
UserInfo.initializeUser(user);
Photo photo = ImageAnalyzer.analyze("TestPicture", "https://www.dropbox.com/s/vz90348372vevwt/IMG_20191018_101819.jpg?dl=1");
assertNotNull(photo); //Checking if the object != null
assertTrue(photo instanceof Photo);
assertEquals("TestPicture", photo.getTitle());
assertEquals("https://www.dropbox.com/s/vz90348372vevwt/IMG_20191018_101819.jpg?dl=1", photo.getUrl());
assertNotNull(photo.getTime());
assertNotNull(photo.getCamera());
assertNotNull(photo.getExposureTime());
assertNotNull(photo.getAperture());
assertNotEquals(0, photo.getHeight(), 0);
assertNotEquals(0, photo.getWidth(), 0);
assertNotEquals(0, photo.getFileSize(), 0);
assertNotNull(photo.getFileType());
assertEquals(user.getId(), photo.getUserId());
System.out.println("Tested all metadata successfully.");
} catch (Exception e) {
fail("Caught an Exception, wanted an expression..." + e);
}
}
/**
* Tests that analyze-method throws IOException
* when we pass null and null as arguments.
*/
@Test
void analyze_TitleAndUrlIsNull_IOExceptionThrown() {
assertThrows(IOException.class, () -> {
ImageAnalyzer.analyze(null, null);
});
}
}
import Components.UserInfo;
import Database.HibernateClasses.Album;
import Database.HibernateClasses.User;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test class to check if a user can upload an delete an album.
*/
class AlbumTest {
static User user;
static Album album;
@BeforeAll
static void setup(){
user = new User();
user.setUsername("testAlbumUser");
user.setId(10);
UserInfo.initializeUser(user);
album = new Album();
album.setName("Album 1");
album.setUserId(user.getId());
album.setId(100);
user.getAlbums().add(album);
}
/**
* Checks whether the album has the correct user id.
*/
@Test
void getID_AlbumCorrectID_isEqual() {
assertEquals(album.getUserId(), user.getId());
}
@Test
void getAlbums_AddNewAlbum_True() {
assertTrue(user.getAlbums().add(new Album("Album 2", user.getId())));
}
/**
* Test to check whether the user has multiple albums with the same id.
*/
@Test
void getAlbumId_AlbumIdIsUnique_OneWithSame() {
List<Album> findAlbumId = user.getAlbums().stream()
.filter(a -> a.getId() == album.getId())
.collect(Collectors.toList());
assert findAlbumId.size() <= 1 : "Only one album can have the same id";
}
}
......@@ -25,8 +25,8 @@ public class EncrypterTest {
@Test
void getHash_EqualPasswordAndSalt_ReturnEqualHashes() {
String password = "test";
String hash1 = Encrypter.getHash(Encrypter.encrypt(password, "salt"));
String hash2 = Encrypter.getHash(Encrypter.encrypt(password, "salt"));
String hash1 = Encrypter.getHash(Encrypter.encrypt(password, "0e9aaa6ac4e05ae4ad260ef69e9047d4"));
String hash2 = Encrypter.getHash(Encrypter.encrypt(password, "0e9aaa6ac4e05ae4ad260ef69e9047d4"));
assertEquals(hash1, hash2);
}
......
import Components.FileLogger;
import Components.ImageAnalyzer;
import Components.UserInfo;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.User;
import java.io.IOException;
import java.util.logging.Level;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* Class for testing of
* ImageAnalyzer class
*/
class ImageAnalyzerTest {
static User user;
static Photo photo;
@BeforeAll
static void setup() {
try {
user = new User();
user.setUsername("_Test_");
user.setId(32);
UserInfo.initializeUser(user);
photo = ImageAnalyzer.analyze("TestPicture", "file:src/Test/Assets/test_image.jpg");
} catch(IOException e) {
FileLogger.getLogger().log(Level.ALL, e.getMessage());
FileLogger.closeHandler();
}
}
/**
* Test to check if the photo object is not null.
*/
@Test
void analyse_PhotoExist_NotNull() {
assertNotNull(photo);
}
/**
* Check if the title of the photo is valid.
*/
@Test
void getTitle_CorrectPhotoTitle_isEqual() {
assertEquals("TestPicture", photo.getTitle());
}
/**
* Check if the photo was initialized with the correct url.
*/
@Test
void getUrl_CorrectUrl_isEqual() {
assertEquals("file:src/Test/Assets/test_image.jpg", photo.getUrl());
}
@Test
void getTime_ValidTime_NotNull() {
assertNotNull(photo.getTime());
}
@Test
void getCamera_ValidCamera_NotNull() {
assertNotNull(photo.getCamera());
}
@Test
void getExposureTime_ValidPhotoExposureTime_NotNull() {
assertNotNull(photo.getExposureTime());
}
@Test
void getAperture_ValidPhotoApertureTime_NotNull() {
assertNotNull(photo.getAperture());
}
@Test
void getHeight_PhotoHeightNotNull_isNotEqual() {
assertNotEquals(0, photo.getHeight(), 0);
}
@Test
void getWidth_PhotoWidthNotNull_isNotEqual() {
assertNotEquals(0, photo.getWidth(), 0);
}
@Test
void getFileSize_PhotoFileSizeNotNull_isNotEqual() {
assertNotEquals(0, photo.getFileSize(), 0);
}
@Test
void getFileType_ValidPhotoFileType_NotNull() {
assertNotNull(photo.getFileType());
}
@Test
void getID_CorrectPhotoUserID_IsEqual() {
assertEquals(user.getId(), photo.getUserId());
}
/**
* Tests that analyze-method throws IOException
* when we pass null and null as arguments.
*/
@Test
void analyze_TitleAndUrlIsNull_IOExceptionThrown() {
assertThrows(IOException.class, () -> {
ImageAnalyzer.analyze(null, null);
});
}
}
import Components.PdfCreator;
import Components.UserInfo;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.User;
import com.itextpdf.text.DocumentException;
import org.junit.jupiter.api.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class PdfCreatorTest {
private static final String PATH_TO_PDF = "src/Test/Assets/generatedAlbumPdf.pdf";
private static List<Photo> photoList = new ArrayList<>();
@BeforeAll
static void setup() {
User user = new User();
user.setUsername("testPhotoUser");
user.setId(10);
UserInfo.initializeUser(user);
Photo photo = new Photo();
photo.setUrl("file:src/Test/Assets/test_image.jpg");
photo.setTitle("Test image");
photo.setWidth(4000);
photo.setHeight(3000);
photoList.add(photo);
}
/**
* Delete the generated pdf after all tests have been executed.
*/
@AfterAll
static void tearDown() {
File file = new File(PATH_TO_PDF);
file.delete();
}
@Test
void createPdf_ShouldWork_GeneratedFileNotNull() throws DocumentException, IOException {
PdfCreator.createPdf(photoList, PATH_TO_PDF, "Test");
File pdfFile = new File(PATH_TO_PDF);
assertNotNull(pdfFile);
}
}
\ No newline at end of file
import Components.ImageAnalyzer;
import Components.UserInfo;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.User;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test class to upload and delete a photo.
*/
class PhotoTest {
private static Photo photo;
private static User user;
@BeforeAll
static void setup() {
try {
user = new User();
user.setUsername("testPhotoUser");
user.setId(10);
UserInfo.initializeUser(user);
photo = ImageAnalyzer.analyze("TestPicture", "file:src/Test/Assets/test_image.jpg");
photo.setId(232);
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Test to upload a photo.
*/
@Test
void add_NewPhoto_True() {
assertTrue(user.getPhotos().add(photo));
}
/**
* An IndexOutOfBoundsException is expected to be thrown when adding a photo to a non existing album.
*/
@Test
void add_PhotoToNonExistingAlbum_IndexOutOfBoundsExceptionThrown() {
assertThrows(IndexOutOfBoundsException.class, () -> {
user.getAlbums().get(0).getPhotos().add(photo);
});
}
/**
* Test to check whether the photo was uploaded to the correct user.
*/
@Test
void getUserId_CorrectPhotoUserID_isEqual() {
assertEquals(photo.getUserId(), user.getId());
}
/**
* Test to check whether the user has multiple photos with the same id.
* The id is unique and the user should not be able to register
* multiple photos with the same id.
*/
@Test
void getId_MaxOnePhotoSameId_OneWithSame() {
List<Photo> findPhotoId = user.getPhotos().stream()
.filter(p -> p.getId() == photo.getId())
.collect(Collectors.toList());
assert findPhotoId.size() <= 1 : "Only one photo can have the same id";
}
}
import Components.ImageAnalyzer;
import Components.UserInfo;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.Tags;
import Database.HibernateClasses.User;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test class to add and remove a tag from a photo.
*/
class TagTest {
private static User user;
private static Photo photo;
private static Tags golfTag;
@BeforeAll
static void setup() {
try {
user = new User();
user.setUsername("testPhotoUser");
user.setId(10);
UserInfo.initializeUser(user);
photo = ImageAnalyzer.analyze("TestPicture", "file:src/Test/Assets/test_image.jpg");
golfTag = new Tags("golf", photo.getId());
golfTag.setId(1000);
photo.getTags().add(golfTag);
} catch(IOException e) {
e.printStackTrace();
}
}
/**
* Add a tag to a photo and check if the tag has the correct photo id.
*/
@Test
void getId_TagValidPhotoId_isEqual() {
Tags tag = new Tags("sport", photo.getId());
tag.setId(23);
photo.getTags().add(tag);
assertEquals(tag.getPhotoId(), photo.getId());
}
/**
* Trying to add a new tag to the photo, but the tag has the incorrect photoId.
*/
@Test
void add_NewTagIncorrectPhotoId_isNotEqual() {
Tags newTag = new Tags("football", 32213);
newTag.setId(21);
photo.getTags().add(newTag);
assertNotEquals(newTag.getPhotoId(), photo.getId());
}
/**
* An IndexOutOfBoundsException is thrown when trying to get
* a photo tag that does not exist.
*/
@Test
void getTags_PhotoHasNoTags_IndexOutOfBoundsExceptionThrown() {
assertThrows(IndexOutOfBoundsException.class, () -> {
photo.getTags().clear();
photo.getTags().get(0).getTag();
});
}
/**
* Test to check if there exists multiple tags with the same id.
*/
@Test
void getId_TagIdIsUnique_OneWithSame() {
List<Tags> findTagId = photo.getTags()
.stream()
.filter(tag -> tag.getId() == golfTag.getId())
.collect(Collectors.toList());
assert findTagId.size() <= 1 : "Can only add one tag with the same id";
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment