|
1 | 1 | package de.upb.cs.swt.delphi.instanceregistry
|
2 | 2 |
|
3 |
| -import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers} |
| 3 | +import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.Instance |
| 4 | +import de.upb.cs.swt.delphi.instanceregistry.io.swagger.client.model.InstanceEnums.ComponentType |
| 5 | +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FlatSpec, Matchers} |
4 | 6 |
|
5 |
| -class RequestHandlerTest extends FlatSpec with Matchers with BeforeAndAfterEach{ |
| 7 | +class RequestHandlerTest extends FlatSpec with Matchers with BeforeAndAfterEach with BeforeAndAfterAll{ |
6 | 8 |
|
7 | 9 | val handler : RequestHandler = new RequestHandler(new Configuration())
|
8 | 10 |
|
| 11 | + private def buildInstance(id : Long) : Instance = { |
| 12 | + Instance(Some(id), "https://localhost", 12345, "TestInstance", ComponentType.ElasticSearch) |
| 13 | + } |
| 14 | + |
| 15 | + override protected def beforeAll():Unit = { |
| 16 | + //Delete recovery file for sure |
| 17 | + handler.shutdown() |
| 18 | + } |
| 19 | + |
9 | 20 | override protected def beforeEach(): Unit = {
|
| 21 | + handler.initialize() |
| 22 | + } |
| 23 | + |
| 24 | + "The RequestHandler" must "assign new IDs to instances regardless of their actual id" in { |
| 25 | + val registerNewInstance = handler.registerNewInstance(buildInstance(Long.MaxValue)) |
| 26 | + assert(registerNewInstance.isSuccess) |
| 27 | + assert(registerNewInstance.get != Long.MaxValue) |
10 | 28 |
|
| 29 | + val registerNewInstance2 = handler.registerNewInstance(buildInstance(1L)) |
| 30 | + assert(registerNewInstance2.isSuccess) |
| 31 | + assert(registerNewInstance2.get != 1L) |
11 | 32 | }
|
12 | 33 |
|
13 |
| - override protected def afterEach(): Unit = { |
| 34 | + it must "be able to remove instances that have been added before" in { |
| 35 | + val registerNewInstance = handler.registerNewInstance(buildInstance(-1L)) |
| 36 | + assert(registerNewInstance.isSuccess) |
| 37 | + assert(registerNewInstance.get == 1) |
| 38 | + assert(handler.removeInstance(1).isSuccess) |
| 39 | + } |
| 40 | + |
| 41 | + it must "not add two default ES instances on initializing" in { |
| 42 | + assert(handler.getNumberOfInstances(ComponentType.ElasticSearch) == 1) |
| 43 | + handler.initialize() |
| 44 | + assert(handler.getNumberOfInstances(ComponentType.ElasticSearch) == 1) |
| 45 | + } |
| 46 | + |
| 47 | + it must "match to instance with most consecutive positive matching results" in { |
| 48 | + val esInstance = handler.registerNewInstance(buildInstance(2)) |
| 49 | + assert(esInstance.isSuccess) |
| 50 | + assert(esInstance.get == 1) |
| 51 | + |
| 52 | + assert(handler.applyMatchingResult(0, result = false).isSuccess) |
| 53 | + assert(handler.applyMatchingResult(0, result = true).isSuccess) |
| 54 | + assert(handler.applyMatchingResult(0,result = true).isSuccess) |
| 55 | + |
| 56 | + assert(handler.applyMatchingResult(1,result = true).isSuccess) |
| 57 | + assert(handler.applyMatchingResult(1,result = false).isSuccess) |
14 | 58 |
|
| 59 | + val matchingInstance = handler.getMatchingInstanceOfType(ComponentType.ElasticSearch) |
| 60 | + assert(matchingInstance.isSuccess) |
| 61 | + assert(matchingInstance.get.id.get == 0) |
| 62 | + |
| 63 | + assert(handler.removeInstance(1L).isSuccess) |
| 64 | + } |
| 65 | + |
| 66 | + it must "match to instance with most positive matching results" in { |
| 67 | + val esInstance = handler.registerNewInstance(buildInstance(2)) |
| 68 | + assert(esInstance.isSuccess) |
| 69 | + assert(esInstance.get == 1) |
| 70 | + |
| 71 | + assert(handler.applyMatchingResult(0, result = true).isSuccess) |
| 72 | + assert(handler.applyMatchingResult(0,result = true).isSuccess) |
| 73 | + assert(handler.applyMatchingResult(0, result = false).isSuccess) |
| 74 | + |
| 75 | + assert(handler.applyMatchingResult(1,result = false).isSuccess) |
| 76 | + assert(handler.applyMatchingResult(1,result = false).isSuccess) |
| 77 | + |
| 78 | + val matchingInstance = handler.getMatchingInstanceOfType(ComponentType.ElasticSearch) |
| 79 | + assert(matchingInstance.isSuccess) |
| 80 | + assert(matchingInstance.get.id.get == 0) |
| 81 | + |
| 82 | + assert(handler.removeInstance(1L).isSuccess) |
| 83 | + } |
| 84 | + |
| 85 | + it must "fail to match if no instance of type is present" in { |
| 86 | + assert(handler.getMatchingInstanceOfType(ComponentType.Crawler).isFailure) |
| 87 | + } |
| 88 | + |
| 89 | + override protected def afterEach(): Unit = { |
| 90 | + handler.shutdown() |
15 | 91 | }
|
16 | 92 |
|
17 | 93 | }
|
0 commit comments