Skip to content

Commit 05aafad

Browse files
author
muencseb
committed
Add Tests
1 parent a10e2ce commit 05aafad

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@
3535
<artifactId>spark-mllib_2.10</artifactId>
3636
<version>1.6.1</version>
3737
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>4.12</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.hamcrest</groupId>
52+
<artifactId>hamcrest-all</artifactId>
53+
<version>1.3</version>
54+
<scope>test</scope>
55+
</dependency>
56+
3857
</dependencies>
3958

4059

src/main/java/recommendation/model/RecommendationMlModel.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ public class RecommendationMlModel {
3030
private ALS als = new ALS();
3131
private MatrixFactorizationModel model;
3232
private ReentrantLock trainingLock = new ReentrantLock();
33+
private JavaSparkContext javaSparkContext = new JavaSparkContext(SPARK_MASTER, SPARK_APP_NAME);
3334

3435

35-
private RDDHelper rddHelper = new RDDHelper(new JavaSparkContext(SPARK_MASTER, SPARK_APP_NAME));
36+
private RDDHelper rddHelper = new RDDHelper(javaSparkContext);
3637

3738
private ReentrantReadWriteLock mutex = new ReentrantReadWriteLock();
3839

39-
private boolean modelIsReady = false;
40+
private volatile boolean modelIsReady = false;
4041

4142

4243
public RecommendationMlModel(Callable<Collection<InputRating>> inputRatings) {
@@ -61,6 +62,10 @@ public Double getInterestPrediction(Integer userId, Integer eventId) throws Mode
6162
return prediction;
6263
}
6364

65+
public void close() {
66+
javaSparkContext.close();
67+
}
68+
6469
private void asyncTrainModel(Callable<Collection<InputRating>> inputRatings) {
6570
try {
6671
asyncTrainModel(inputRatings.call());
@@ -89,6 +94,7 @@ private void trainModel(Collection<InputRating> ratings) {
8994
model = als.setRank(10).setIterations(10).run(ratingRDD);
9095
mutex.writeLock().unlock();
9196
modelIsReady = true;
97+
9298
}
9399

94100

src/main/java/recommendation/service/RecommendationService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ public Double getPrediction(Integer userId, Integer productId) throws ModelNotRe
2727
return recommendationMlModel.getInterestPrediction(userId, productId);
2828
}
2929

30+
public void close() {
31+
recommendationMlModel.close();
32+
}
33+
3034

3135
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package data;
2+
3+
import org.apache.commons.collections.CollectionUtils;
4+
import org.apache.spark.api.java.JavaRDD;
5+
import org.apache.spark.api.java.JavaSparkContext;
6+
import org.junit.AfterClass;
7+
import org.junit.Test;
8+
import recommendation.data.RDDHelper;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
14+
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
17+
import static org.junit.Assert.assertTrue;
18+
19+
public class RDDHelperTest {
20+
21+
private static JavaSparkContext jsc = new JavaSparkContext("local", "testjsc");
22+
private RDDHelper rddHelper = new RDDHelper(jsc);
23+
24+
@AfterClass
25+
public static void close() {
26+
jsc.close();
27+
}
28+
29+
@Test
30+
public void shouldCreateRDDFromCollection() {
31+
List<Integer> intList = IntStream.range(0, 100).boxed().collect(Collectors.toList());
32+
JavaRDD intRDD = rddHelper.getRddFromCollection(intList);
33+
assertTrue(CollectionUtils.isEqualCollection(intRDD.collect(), intList));
34+
assertThat(intRDD, instanceOf(JavaRDD.class));
35+
}
36+
37+
@Test
38+
public void shouldCreateRDDFromCollectionWithConverter() {
39+
List<Integer> intList = IntStream.range(0, 3).boxed().collect(Collectors.toList());
40+
JavaRDD stringRDD = rddHelper.getRddFromCollection(intList, integer -> integer.toString());
41+
assertThat(stringRDD, instanceOf(JavaRDD.class));
42+
assertTrue(CollectionUtils.isEqualCollection(stringRDD.collect(), Arrays.asList("0", "1", "2")));
43+
assertThat(stringRDD, instanceOf(JavaRDD.class));
44+
}
45+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package service;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
import recommendation.data.InputRating;
6+
import recommendation.exceptions.ModelNotReadyException;
7+
import recommendation.service.RecommendationService;
8+
9+
import java.util.ArrayList;
10+
11+
import static org.hamcrest.MatcherAssert.assertThat;
12+
import static org.hamcrest.Matchers.greaterThan;
13+
import static org.hamcrest.Matchers.lessThan;
14+
import static org.junit.Assert.assertNotEquals;
15+
16+
/**
17+
* The testing was keepen simple, as sparkml is already tested. The testing focus is on the new features.
18+
* This test just verifies, that sparkml was correctly called
19+
*/
20+
public class RecommendationServiceTest {
21+
22+
23+
static RecommendationService recommendationService;
24+
25+
@BeforeClass
26+
public static void initModel() {
27+
ArrayList<InputRating> inputRatings = new ArrayList<>();
28+
inputRatings.add(new InputRating(1, 1, 1));
29+
inputRatings.add(new InputRating(1, 2, 0));
30+
inputRatings.add(new InputRating(1, 3, 2));
31+
32+
inputRatings.add(new InputRating(2, 1, 1));
33+
recommendationService = new RecommendationService(inputRatings);
34+
while (!recommendationService.isModelReady()) {
35+
}
36+
}
37+
38+
@Test
39+
public void shouldNotForgetKnownRating() throws ModelNotReadyException {
40+
assertThat(recommendationService.getPrediction(1, 2), greaterThan(recommendationService.getPrediction(1, 1)));
41+
assertNotEquals(0.0, recommendationService.getPrediction(1, 1));
42+
}
43+
44+
@Test
45+
public void shouldPredictFromOtherUser() throws ModelNotReadyException {
46+
assertThat(recommendationService.getPrediction(2, 2), lessThan(recommendationService.getPrediction(1, 3)));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)