Skip to content

Commit

Permalink
Merge pull request #6 from imperva/3.9.1_junit_linda
Browse files Browse the repository at this point in the history
Added first unit tests
  • Loading branch information
gabibeyo authored Jul 29, 2020
2 parents 765a377 + eb25f34 commit 815a0b3
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/imperva/stepping/ContainerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

public class ContainerService extends ContainerDefaultImpl {

static final String RUNNING_SCHEDULED = ".runningScheduled";
static final String DECORATOR = ".decorator";
static final String STEPPING_PRIVATE_CONTAINER = "__STEPPING_PRIVATE_CONTAINER__";

public RunningScheduled getTickCallbackRunning(String stepID) {
try {
String runningID = stepID + ".runningScheduled";
return ((RunningScheduled) ((Container) getById("__STEPPING_PRIVATE_CONTAINER__")).getById(runningID));
String runningID = stepID + RUNNING_SCHEDULED;
return ((RunningScheduled) ((Container) getById(STEPPING_PRIVATE_CONTAINER)).getById(runningID));
} catch (Exception e) {
throw new SteppingException("Running object not found. Please make sure that you enabled TickCallback for this step");
}
}

public int getQSize(String stepID) {
try {
String decoratorID = stepID + ".decorator";
return ((IStepDecorator) ((Container) getById("__STEPPING_PRIVATE_CONTAINER__")).getById(decoratorID)).getQSize();
String decoratorID = stepID + DECORATOR;
return ((IStepDecorator) ((Container) getById(STEPPING_PRIVATE_CONTAINER)).getById(decoratorID)).getQSize();
} catch (Exception e) {
throw new SteppingException("Q size not found. Please make sure you use the correct Step ID");
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/imperva/stepping/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/**
* Created by gabi.beyo on 12/13/2017.
* If List is passed as the value to this object, it must not be modified later.
*/
public class Data {
private final Object value;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/com/imperva/stepping/ContainerServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.imperva.stepping;

import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Author: Linda Nasredin
* Date: 06 May 2020
*/
class ContainerServiceTest {

@Test
void getTickCallbackRunning() {
RunningScheduled value = mock(RunningScheduled.class);

Container innerContainer = new ContainerService();
innerContainer.add(value, "stepId3" + ContainerService.RUNNING_SCHEDULED);

ContainerService containerService = new ContainerService();
containerService.add(innerContainer, ContainerService.STEPPING_PRIVATE_CONTAINER);

RunningScheduled actual = containerService.getTickCallbackRunning("stepId3");

Assert.assertEquals(value, actual);
}

@Test
void getQSize() {
IStepDecorator decoratorMock = mock(IStepDecorator.class);
when(decoratorMock.getQSize()).thenReturn(34);

Container innerContainer = new ContainerService();
innerContainer.add(decoratorMock, "stepId1" + ContainerService.DECORATOR);

ContainerService containerService = new ContainerService();
containerService.add(innerContainer, ContainerService.STEPPING_PRIVATE_CONTAINER);

int qSize = containerService.getQSize("stepId1");

Assert.assertEquals(34, qSize);
}
}
69 changes: 69 additions & 0 deletions src/test/java/com/imperva/stepping/DataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.imperva.stepping;

import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
* Author: Linda Nasredin
* Date: 06 May 2020
*/
class DataTest {

@Test
void getSize_nullValue() {
Data data = new Data(null);
Assert.assertEquals(0, data.getSize());
}

@Test
void getSize_stringValue() {
Data data = new Data("value");
Assertions.assertEquals(1, data.getSize());
}

@Test
void getSize_listValue() {
Data data = new Data(Arrays.asList(1, 2, 3));
Assertions.assertEquals(3, data.getSize());
}

@Test
void getSize_listValueChange() {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
Data data = new Data(list);
Assertions.assertEquals(2, data.getSize());
list.add(3);
Assertions.assertEquals(2, data.getSize());
}

@Test
void getValue() {
Data data = new Data("value");
Assertions.assertEquals("value", data.getValue());
}

@Test
void isExpirable() {
Data data = new Data("value");
Assertions.assertFalse(data.isExpirable());
}

@Test
void tryGrabAndExpire() {
Data data = new Data("value");
data.setExpirationCondition((data1, context) -> false, 1);
Assert.assertTrue(data.isExpirable());
Assertions.assertFalse(data.tryGrabAndExpire());
}

}
112 changes: 112 additions & 0 deletions src/test/java/com/imperva/stepping/QTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.imperva.stepping;

import com.sun.source.tree.AssertTree;
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Author: Linda Nasredin
* Date: 06 May 2020
*/
class QTest {

@Test
void q_zeroCapacityIsMaxCapacity() {
Q<Integer> q = new Q<>();
// TODO actual capacity of the inner queue is Integer.MAX_VALUE
Assert.assertEquals(0, q.getCapacity());
}

@Test
void q_positiveCapacity() {
Q<Integer> q = new Q<>(10);
Assert.assertEquals(10, q.getCapacity());
}

@Test
void q_negativeCapacity() {
Assertions.assertThrows(SteppingException.class, () -> new Q<>(-1));
}

@Test
void peek_found() {
Q<String> q = new Q<>(2);
q.queue("item1");
q.queue("item2");
Assertions.assertEquals("item1", q.peek());
}

@Test
void peek_empty() {
Q<String> q = new Q<>(2);
Assertions.assertNull(q.peek());
}

@Test
void queue_wait_take() throws InterruptedException {
Q<String> q = new Q<>(2);
q.queue("item1");

Runnable runnable = () -> {
q.queue("item2");
q.queue("item3");
};
new Thread(runnable).start();

Thread.sleep(50);
Assertions.assertEquals(2, q.size());
q.take();
Thread.sleep(50);
Assertions.assertEquals(2, q.size());
}

@Test
void contains() {
Q<String> q = new Q<>(2);
q.queue("item1");
q.queue("item2");
Assertions.assertTrue(q.contains());
}

@Test
void take_wait() throws InterruptedException {
Q<String> q = new Q<>(2);
Runnable runnable = () -> {
try {
q.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
};
new Thread(runnable).start();

Thread.sleep(50);
q.queue("item1");
Assertions.assertEquals(1, q.size());
Thread.sleep(50);
Assertions.assertEquals(0, q.size());
}

@Test
void clear() {
Q<String> q = new Q<>(2);
q.queue("item1");
q.queue("item2");
q.clear();
Assertions.assertEquals(0, q.size());
}

@Test
void offer() {
Q<String> q = new Q<>(1);
boolean offered1 = q.offer("item1");
Assertions.assertTrue(offered1);
boolean offered2 = q.offer("item2");
Assertions.assertFalse(offered2);
}

}

0 comments on commit 815a0b3

Please sign in to comment.