Skip to content

Commit

Permalink
added some test cases from #946
Browse files Browse the repository at this point in the history
  • Loading branch information
anidotnet committed May 7, 2024
1 parent d20a92e commit 8a7a8e7
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@

package org.dizitart.no2.filters;

import org.dizitart.no2.collection.Document;
import org.dizitart.no2.collection.NitriteId;
import org.dizitart.no2.common.tuples.Pair;
import org.dizitart.no2.exceptions.FilterException;
import org.dizitart.no2.index.fulltext.EnglishTextTokenizer;
import org.dizitart.no2.store.memory.InMemoryMap;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TextFilterTest {
@Test
Expand Down Expand Up @@ -61,7 +67,23 @@ public void testApplyOnIndex3() {
TextFilter textFilter = new TextFilter("Field", "*");
textFilter.setTextTokenizer(new EnglishTextTokenizer());
assertThrows(FilterException.class,
() -> textFilter.applyOnTextIndex(new InMemoryMap<>("Map Name", null)));
() -> textFilter.applyOnTextIndex(new InMemoryMap<>("Map Name", null)));
}

@Test
public void applyTestWhenDocStringContainsSearchString() {
//Arrange
TextFilter textFilter = new TextFilter("fieldString", "string");
Document docMock = mock(Document.class);
Pair<NitriteId, Document> pairElementMock = mock(Pair.class);
when(pairElementMock.getSecond()).thenReturn(docMock);
when(docMock.get(Mockito.anyString())).thenReturn("parent doc string");

//Action
var result = textFilter.apply(pairElementMock);
//Assert
assertTrue(result);

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.dizitart.no2.migration;

import org.dizitart.no2.Nitrite;
import org.dizitart.no2.NitriteConfig;
import org.dizitart.no2.store.StoreMetaData;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.Method;
import java.util.Queue;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

public class MigrationManagerTest {

@Test
@SuppressWarnings("unchecked")
public void findMigrationPathTestWhenStartAndEndAreSame() throws Exception {
NitriteConfig nitriteConfigMock = Mockito.mock(NitriteConfig.class);
StoreMetaData storeMetaDataMock = Mockito.mock(StoreMetaData.class);

Nitrite nitriteMock = Mockito.mock(Nitrite.class);
when(nitriteMock.getConfig()).thenReturn(nitriteConfigMock);

MigrationManager migrationManager = new MigrationManager(nitriteMock);

when(nitriteMock.getDatabaseMetaData()).thenReturn(storeMetaDataMock);

Method method = MigrationManager.class.getDeclaredMethod("findMigrationPath", int.class, int.class);
method.setAccessible(true);
var result = (Queue<Migration>) method.invoke(migrationManager, 1, 1);

assertNotNull(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

package org.dizitart.no2.store;

import org.dizitart.no2.exceptions.NitriteSecurityException;
import org.dizitart.no2.store.memory.InMemoryStore;
import org.junit.Test;
import org.mockito.Mockito;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class UserAuthenticationServiceTest {
@Test
Expand All @@ -45,5 +50,38 @@ public void testAuthenticate4() {
public void testAuthenticate5() {
(new UserAuthenticationService(new InMemoryStore())).authenticate("", "iloveyou");
}

@Test(expected = NitriteSecurityException.class)
public void testAuthenticateIfUsernamePasswordAreNull() {
//region ARRANGE
NitriteStore<?> storeMock = mock(NitriteStore.class);
when(storeMock.hasMap(Mockito.anyString())).thenReturn(true);

UserAuthenticationService userAuthenticationService = new UserAuthenticationService(storeMock);
//endregion

//region ACTION
userAuthenticationService.authenticate(null, null);
//endregion
}

@Test(expected = NitriteSecurityException.class)
@SuppressWarnings({"unchecked", "rawtypes"})
public void testAuthenticateIfHasMapIsTrueAndUserCredentialIsNull() {
//region ARRANGE
NitriteMap nitriteMapMock = mock(NitriteMap.class);
when(nitriteMapMock.get(Mockito.anyString())).thenReturn(null);

NitriteStore<?> storeMock = mock(NitriteStore.class);
when(storeMock.hasMap(Mockito.anyString())).thenReturn(true);
when(storeMock.openMap(Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(nitriteMapMock);

UserAuthenticationService userAuthenticationService = new UserAuthenticationService(storeMock);
//endregion

//region ACTION
userAuthenticationService.authenticate("usernameString", "passwordString");
//endregion
}
}

0 comments on commit 8a7a8e7

Please sign in to comment.