Compare commits
32 Commits
all-contri
...
all-contri
Author | SHA1 | Date | |
---|---|---|---|
|
c18282ad5d | ||
|
7411ea86bf | ||
|
ef033b66a6 | ||
|
e65b65257e | ||
|
3d9afbaeec | ||
|
61a819aab8 | ||
|
61b95c294b | ||
|
4068d1fead | ||
|
b284230ecf | ||
|
0529b77abb | ||
|
a2ae5d1324 | ||
|
25159ed9a8 | ||
|
7f09cd5b2d | ||
|
b388020fc0 | ||
|
0c552effc3 | ||
|
905b5dc6d8 | ||
|
885c8a6765 | ||
|
6b5b2ac1e8 | ||
|
c35b98b4d7 | ||
|
f5fddeb7f0 | ||
|
292ec5b8e5 | ||
|
60d87789a6 | ||
|
860453b46b | ||
|
6921b0dce0 | ||
|
847585334c | ||
|
9bb0b6fe6f | ||
|
31881f500d | ||
|
06f20570b6 | ||
|
017ee23793 | ||
|
57e45a329f | ||
|
133ef52898 | ||
|
31471acb69 |
@@ -1128,7 +1128,8 @@
|
||||
"avatar_url": "https://avatars0.githubusercontent.com/u/13291222?v=4",
|
||||
"profile": "http://subho.xyz",
|
||||
"contributions": [
|
||||
"code"
|
||||
"code",
|
||||
"review"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1139,6 +1140,33 @@
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "ToxicDreamz",
|
||||
"name": "Toxic Dreamz",
|
||||
"avatar_url": "https://avatars0.githubusercontent.com/u/45225562?v=4",
|
||||
"profile": "https://github.com/ToxicDreamz",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "edycutjong",
|
||||
"name": "Edy Cu Tjong",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/1098102?v=4",
|
||||
"profile": "http://www.edycutjong.com",
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "mkrzywanski",
|
||||
"name": "Michał Krzywański",
|
||||
"avatar_url": "https://avatars0.githubusercontent.com/u/15279585?v=4",
|
||||
"profile": "https://github.com/mkrzywanski",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 4,
|
||||
|
2
.github/workflows/maven-ci.yml
vendored
2
.github/workflows/maven-ci.yml
vendored
@@ -60,5 +60,5 @@ jobs:
|
||||
run: xvfb-run mvn clean verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
|
||||
env:
|
||||
# These two env variables are needed for sonar analysis
|
||||
GITHUB_TOKEN: ${{ secrets.REPOSITORY_ACCESS_TOKEN }}
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
|
10
README.md
10
README.md
@@ -6,10 +6,11 @@
|
||||
|
||||

|
||||
[](https://raw.githubusercontent.com/iluwatar/java-design-patterns/master/LICENSE.md)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
[](#contributors-)
|
||||
[](#contributors-)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
|
||||
# Introduction
|
||||
@@ -252,10 +253,13 @@ This project is licensed under the terms of the MIT license.
|
||||
<td align="center"><a href="https://www.linkedin.com/in/ashish-trivedi-218379135/"><img src="https://avatars3.githubusercontent.com/u/23194128?v=4" width="100px;" alt=""/><br /><sub><b>Ashish Trivedi</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ashishtrivedi16" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://rayyounghong.com"><img src="https://avatars1.githubusercontent.com/u/41055099?v=4" width="100px;" alt=""/><br /><sub><b>洪月阳</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=RayYH" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://xdvrx1.github.io/"><img src="https://avatars0.githubusercontent.com/u/47092464?v=4" width="100px;" alt=""/><br /><sub><b>xdvrx1</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Axdvrx1" title="Reviewed Pull Requests">👀</a> <a href="#ideas-xdvrx1" title="Ideas, Planning, & Feedback">🤔</a></td>
|
||||
<td align="center"><a href="http://subho.xyz"><img src="https://avatars0.githubusercontent.com/u/13291222?v=4" width="100px;" alt=""/><br /><sub><b>Subhrodip Mohanta</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ohbus" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://subho.xyz"><img src="https://avatars0.githubusercontent.com/u/13291222?v=4" width="100px;" alt=""/><br /><sub><b>Subhrodip Mohanta</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ohbus" title="Code">💻</a> <a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3Aohbus" title="Reviewed Pull Requests">👀</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/nahteb"><img src="https://avatars3.githubusercontent.com/u/13121570?v=4" width="100px;" alt=""/><br /><sub><b>Bethan Palmer</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=nahteb" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/ToxicDreamz"><img src="https://avatars0.githubusercontent.com/u/45225562?v=4" width="100px;" alt=""/><br /><sub><b>Toxic Dreamz</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ToxicDreamz" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://www.edycutjong.com"><img src="https://avatars1.githubusercontent.com/u/1098102?v=4" width="100px;" alt=""/><br /><sub><b>Edy Cu Tjong</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=edycutjong" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/mkrzywanski"><img src="https://avatars0.githubusercontent.com/u/15279585?v=4" width="100px;" alt=""/><br /><sub><b>Michał Krzywański</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mkrzywanski" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
|
@@ -25,14 +25,23 @@ package com.iluwatar.abstractdocument;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Simple App test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void shouldExecuteAppWithoutException() {
|
||||
App.main(null);
|
||||
void shouldExecuteAppWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(null));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,12 +25,23 @@ package com.iluwatar.abstractfactory;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Abstract Factory example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,23 @@ package com.iluwatar.acyclicvisitor;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that the Acyclic Visitor example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
@@ -25,12 +25,23 @@ package com.iluwatar.adapter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Adapter example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -62,7 +62,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
*
|
||||
* @param value integer value to be multiplied.
|
||||
* @return if waitTime is less than {@link RemoteService#THRESHOLD}, it returns value * 10,
|
||||
* otherwise {@link RemoteServiceInterface#FAILURE}.
|
||||
* otherwise {@link RemoteServiceStatus#FAILURE}.
|
||||
*/
|
||||
@Override
|
||||
public long doRemoteFunction(int value) {
|
||||
@@ -74,6 +74,7 @@ public class RemoteService implements RemoteServiceInterface {
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Thread sleep state interrupted", e);
|
||||
}
|
||||
return waitTime <= THRESHOLD ? value * 10 : FAILURE;
|
||||
return waitTime <= THRESHOLD ? value * 10
|
||||
: RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
}
|
||||
|
@@ -27,7 +27,6 @@ package com.iluwatar.ambassador;
|
||||
* Interface shared by ({@link RemoteService}) and ({@link ServiceAmbassador}).
|
||||
*/
|
||||
interface RemoteServiceInterface {
|
||||
int FAILURE = -1;
|
||||
|
||||
long doRemoteFunction(int value);
|
||||
}
|
||||
|
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
/**
|
||||
* Holds information regarding the status of the Remote Service.
|
||||
*
|
||||
* <p> This Enum replaces the integer value previously
|
||||
* stored in {@link RemoteServiceInterface} as SonarCloud was identifying
|
||||
* it as an issue. All test cases have been checked after changes,
|
||||
* without failures. </p>
|
||||
*/
|
||||
|
||||
public enum RemoteServiceStatus {
|
||||
FAILURE(-1)
|
||||
;
|
||||
|
||||
private final long remoteServiceStatusValue;
|
||||
|
||||
RemoteServiceStatus(long remoteServiceStatusValue) {
|
||||
this.remoteServiceStatusValue = remoteServiceStatusValue;
|
||||
}
|
||||
|
||||
public long getRemoteServiceStatusValue() {
|
||||
return remoteServiceStatusValue;
|
||||
}
|
||||
}
|
@@ -23,6 +23,7 @@
|
||||
|
||||
package com.iluwatar.ambassador;
|
||||
|
||||
import static com.iluwatar.ambassador.RemoteServiceStatus.FAILURE;
|
||||
import static java.lang.Thread.sleep;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -58,14 +59,14 @@ public class ServiceAmbassador implements RemoteServiceInterface {
|
||||
|
||||
private long safeCall(int value) {
|
||||
var retries = 0;
|
||||
var result = (long) FAILURE;
|
||||
var result = FAILURE.getRemoteServiceStatusValue();
|
||||
|
||||
for (int i = 0; i < RETRIES; i++) {
|
||||
if (retries >= RETRIES) {
|
||||
return FAILURE;
|
||||
return FAILURE.getRemoteServiceStatusValue();
|
||||
}
|
||||
|
||||
if ((result = checkLatency(value)) == FAILURE) {
|
||||
if ((result = checkLatency(value)) == FAILURE.getRemoteServiceStatusValue()) {
|
||||
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
|
||||
retries++;
|
||||
try {
|
||||
|
@@ -25,13 +25,23 @@ package com.iluwatar.ambassador;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -37,6 +37,6 @@ class ClientTest {
|
||||
Client client = new Client();
|
||||
var result = client.useService(10);
|
||||
|
||||
assertTrue(result == 100 || result == RemoteService.FAILURE);
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ class RemoteServiceTest {
|
||||
void testFailedCall() {
|
||||
var remoteService = new RemoteService(new StaticRandomProvider(0.21));
|
||||
var result = remoteService.doRemoteFunction(10);
|
||||
assertEquals(RemoteServiceInterface.FAILURE, result);
|
||||
assertEquals(RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue(), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -35,6 +35,6 @@ class ServiceAmbassadorTest {
|
||||
@Test
|
||||
void test() {
|
||||
long result = new ServiceAmbassador().doRemoteFunction(10);
|
||||
assertTrue(result == 100 || result == RemoteServiceInterface.FAILURE);
|
||||
assertTrue(result == 100 || result == RemoteServiceStatus.FAILURE.getRemoteServiceStatusValue());
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,24 @@ package com.iluwatar.async.method.invocation;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void test() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -24,15 +24,26 @@
|
||||
package com.iluwatar.balking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void main() {
|
||||
App.main();
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow((Executable) App::main);
|
||||
}
|
||||
|
||||
}
|
@@ -25,12 +25,22 @@ package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,23 @@ package com.iluwatar.builder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -27,13 +27,23 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Business Delegate example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -58,12 +58,14 @@ public class App {
|
||||
var vm = new VirtualMachine();
|
||||
vm.getWizards()[0] = wizard;
|
||||
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
String literal = "LITERAL 0";
|
||||
|
||||
interpretInstruction(literal, vm);
|
||||
interpretInstruction(literal, vm);
|
||||
interpretInstruction("GET_HEALTH", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction(literal, vm);
|
||||
interpretInstruction("GET_AGILITY", vm);
|
||||
interpretInstruction("LITERAL 0", vm);
|
||||
interpretInstruction(literal, vm);
|
||||
interpretInstruction("GET_WISDOM ", vm);
|
||||
interpretInstruction("ADD", vm);
|
||||
interpretInstruction("LITERAL 2", vm);
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.bytecode;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -27,12 +27,23 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Caching example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,23 @@ package com.iluwatar.callback;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Callback example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,23 @@ package com.iluwatar.chain;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -39,6 +39,12 @@
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@@ -25,12 +25,19 @@ package com.iluwatar.combinator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
public class CombinatorAppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link CombinatorApp#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void main() {
|
||||
CombinatorApp.main(new String[]{});
|
||||
public void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> CombinatorApp.main(new String[]{}));
|
||||
}
|
||||
}
|
@@ -25,12 +25,21 @@ package com.iluwatar.command;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Command example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -36,9 +36,11 @@ import com.iluwatar.commander.queue.QueueDatabase;
|
||||
import com.iluwatar.commander.queue.QueueTask;
|
||||
import com.iluwatar.commander.queue.QueueTask.TaskType;
|
||||
import com.iluwatar.commander.shippingservice.ShippingService;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* <p>Commander pattern is used to handle all issues that can come up while making a
|
||||
* distributed transaction. The idea is to have a commander, which coordinates the execution of all
|
||||
@@ -159,8 +161,8 @@ public class Commander {
|
||||
|
||||
private void sendPaymentRequest(Order order) {
|
||||
if (System.currentTimeMillis() - order.createdTime >= this.paymentTime) {
|
||||
if (order.paid.equals(PaymentStatus.Trying)) {
|
||||
order.paid = PaymentStatus.NotDone;
|
||||
if (order.paid.equals(PaymentStatus.TRYING)) {
|
||||
order.paid = PaymentStatus.NOT_DONE;
|
||||
sendPaymentFailureMessage(order);
|
||||
LOG.error("Order " + order.id + ": Payment time for order over, failed and returning..");
|
||||
} //if succeeded or failed, would have been dequeued, no attempt to make payment
|
||||
@@ -172,15 +174,15 @@ public class Commander {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
|
||||
+ " trying again..");
|
||||
+ " trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating payment request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.Trying)) {
|
||||
if (order.paid.equals(PaymentStatus.TRYING)) {
|
||||
var transactionId = paymentService.receiveRequest(order.price);
|
||||
order.paid = PaymentStatus.Done;
|
||||
order.paid = PaymentStatus.DONE;
|
||||
LOG.info("Order " + order.id + ": Payment successful, transaction Id: " + transactionId);
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("Payment made successfully, thank you for shopping with us!!");
|
||||
@@ -193,26 +195,26 @@ public class Commander {
|
||||
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("There was an error in payment. Your account/card details "
|
||||
+ "may have been incorrect. "
|
||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||
+ "may have been incorrect. "
|
||||
+ "Meanwhile, your order has been converted to COD and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
|
||||
o.paid = PaymentStatus.NotDone;
|
||||
o.paid = PaymentStatus.NOT_DONE;
|
||||
sendPaymentFailureMessage(o);
|
||||
} else {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent)) {
|
||||
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
|
||||
if (!finalSiteMsgShown) {
|
||||
LOG.info("There was an error in payment. We are on it, and will get back to you "
|
||||
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
||||
+ "asap. Don't worry, your order has been placed and will be shipped.");
|
||||
finalSiteMsgShown = true;
|
||||
}
|
||||
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
|
||||
sendPaymentPossibleErrorMsg(o);
|
||||
}
|
||||
if (o.paid.equals(PaymentStatus.Trying) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
var qt = new QueueTask(o, TaskType.Payment, -1);
|
||||
if (o.paid.equals(PaymentStatus.TRYING) && System
|
||||
.currentTimeMillis() - o.createdTime < paymentTime) {
|
||||
var qt = new QueueTask(o, TaskType.PAYMENT, -1);
|
||||
updateQueue(qt);
|
||||
}
|
||||
}
|
||||
@@ -234,12 +236,12 @@ public class Commander {
|
||||
// additional check not needed
|
||||
LOG.trace("Order " + qt.order.id + ": Queue time for order over, failed..");
|
||||
return;
|
||||
} else if (qt.taskType.equals(TaskType.Payment) && !qt.order.paid.equals(PaymentStatus.Trying)
|
||||
|| qt.taskType.equals(TaskType.Messaging) && (qt.messageType == 1
|
||||
&& !qt.order.messageSent.equals(MessageSent.NoneSent)
|
||||
|| qt.order.messageSent.equals(MessageSent.PaymentFail)
|
||||
|| qt.order.messageSent.equals(MessageSent.PaymentSuccessful))
|
||||
|| qt.taskType.equals(TaskType.EmployeeDb) && qt.order.addedToEmployeeHandle) {
|
||||
} else if (qt.taskType.equals(TaskType.PAYMENT) && !qt.order.paid.equals(PaymentStatus.TRYING)
|
||||
|| qt.taskType.equals(TaskType.MESSAGING) && (qt.messageType == 1
|
||||
&& !qt.order.messageSent.equals(MessageSent.NONE_SENT)
|
||||
|| qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL))
|
||||
|| qt.taskType.equals(TaskType.EMPLOYEE_DB) && qt.order.addedToEmployeeHandle) {
|
||||
LOG.trace("Order " + qt.order.id + ": Not queueing task since task already done..");
|
||||
return;
|
||||
}
|
||||
@@ -256,8 +258,8 @@ public class Commander {
|
||||
tryDoingTasksInQueue();
|
||||
};
|
||||
Retry.HandleErrorIssue<QueueTask> handleError = (qt1, err) -> {
|
||||
if (qt1.taskType.equals(TaskType.Payment)) {
|
||||
qt1.order.paid = PaymentStatus.NotDone;
|
||||
if (qt1.taskType.equals(TaskType.PAYMENT)) {
|
||||
qt1.order.paid = PaymentStatus.NOT_DONE;
|
||||
sendPaymentFailureMessage(qt1.order);
|
||||
LOG.error("Order " + qt1.order.id + ": Unable to enqueue payment task,"
|
||||
+ " payment failed..");
|
||||
@@ -331,35 +333,9 @@ public class Commander {
|
||||
}
|
||||
var list = messagingService.exceptionsList;
|
||||
Thread t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Success msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
var requestId = messagingService.receiveRequest(2);
|
||||
order.messageSent = MessageSent.PaymentSuccessful;
|
||||
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
};
|
||||
Retry.Operation op = handleSuccessMessageRetryOperation(order);
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.Messaging, 2);
|
||||
updateQueue(qt);
|
||||
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
|
||||
+ " queue task and add to employee handle..");
|
||||
employeeHandleIssue(order);
|
||||
}
|
||||
handleSuccessMessageErrorIssue(order, o);
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
@@ -372,6 +348,40 @@ public class Commander {
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void handleSuccessMessageErrorIssue(Order order, Order o) {
|
||||
if ((o.messageSent.equals(MessageSent.NONE_SENT) || o.messageSent
|
||||
.equals(MessageSent.PAYMENT_TRYING))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.MESSAGING, 2);
|
||||
updateQueue(qt);
|
||||
LOG.info("Order " + order.id + ": Error in sending Payment Success message, trying to"
|
||||
+ " queue task and add to employee handle..");
|
||||
employeeHandleIssue(order);
|
||||
}
|
||||
}
|
||||
|
||||
private Retry.Operation handleSuccessMessageRetryOperation(Order order) {
|
||||
return (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Success msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Success"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
&& !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
var requestId = messagingService.receiveRequest(2);
|
||||
order.messageSent = MessageSent.PAYMENT_SUCCESSFUL;
|
||||
LOG.info("Order " + order.id + ": Payment Success message sent,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private void sendPaymentFailureMessage(Order order) {
|
||||
if (System.currentTimeMillis() - order.createdTime >= this.messageTime) {
|
||||
LOG.trace("Order " + order.id + ": Message time for order over, returning..");
|
||||
@@ -380,34 +390,10 @@ public class Commander {
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Failure msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
|
||||
+ " message request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PaymentFail)
|
||||
&& !order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
var requestId = messagingService.receiveRequest(0);
|
||||
order.messageSent = MessageSent.PaymentFail;
|
||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
handlePaymentFailureRetryOperation(order, l);
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if ((o.messageSent.equals(MessageSent.NoneSent) || o.messageSent
|
||||
.equals(MessageSent.PaymentTrying))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.Messaging, 0);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
handlePaymentErrorIssue(order, o);
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
@@ -420,6 +406,38 @@ public class Commander {
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void handlePaymentErrorIssue(Order order, Order o) {
|
||||
if ((o.messageSent.equals(MessageSent.NONE_SENT) || o.messageSent
|
||||
.equals(MessageSent.PAYMENT_TRYING))
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.MESSAGING, 0);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Failure message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePaymentFailureRetryOperation(Order order, List<Exception> l) throws Exception {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Failure msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Failure"
|
||||
+ " message request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (!order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
&& !order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
var requestId = messagingService.receiveRequest(0);
|
||||
order.messageSent = MessageSent.PAYMENT_FAIL;
|
||||
LOG.info("Order " + order.id + ": Payment Failure message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPaymentPossibleErrorMsg(Order order) {
|
||||
if (System.currentTimeMillis() - order.createdTime >= this.messageTime) {
|
||||
LOG.trace("Message time for order over, returning..");
|
||||
@@ -428,34 +446,10 @@ public class Commander {
|
||||
var list = messagingService.exceptionsList;
|
||||
var t = new Thread(() -> {
|
||||
Retry.Operation op = (l) -> {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Error msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.Trying) && order.messageSent
|
||||
.equals(MessageSent.NoneSent)) {
|
||||
var requestId = messagingService.receiveRequest(1);
|
||||
order.messageSent = MessageSent.PaymentTrying;
|
||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
handlePaymentPossibleErrorMsgRetryOperation(order, l);
|
||||
};
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (o.messageSent.equals(MessageSent.NoneSent) && order.paid
|
||||
.equals(PaymentStatus.Trying)
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.Messaging, 1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
handlePaymentPossibleErrorMsgErrorIssue(order, o);
|
||||
};
|
||||
var r = new Retry<>(op, handleError, numOfRetries, retryDuration,
|
||||
e -> DatabaseUnavailableException.class.isAssignableFrom(e.getClass()));
|
||||
@@ -468,6 +462,39 @@ public class Commander {
|
||||
t.start();
|
||||
}
|
||||
|
||||
private void handlePaymentPossibleErrorMsgErrorIssue(Order order, Order o) {
|
||||
if (o.messageSent.equals(MessageSent.NONE_SENT) && order.paid
|
||||
.equals(PaymentStatus.TRYING)
|
||||
&& System.currentTimeMillis() - o.createdTime < messageTime) {
|
||||
var qt = new QueueTask(order, TaskType.MESSAGING, 1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in sending Payment Error message, "
|
||||
+ "trying to queue task and add to employee handle..");
|
||||
employeeHandleIssue(o);
|
||||
}
|
||||
}
|
||||
|
||||
private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List<Exception> l)
|
||||
throws Exception {
|
||||
if (!l.isEmpty()) {
|
||||
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
|
||||
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
|
||||
+ "(Payment Error msg), trying again..");
|
||||
} else {
|
||||
LOG.debug("Order " + order.id + ": Error in creating Payment Error"
|
||||
+ " messaging request..");
|
||||
}
|
||||
throw l.remove(0);
|
||||
}
|
||||
if (order.paid.equals(PaymentStatus.TRYING) && order.messageSent
|
||||
.equals(MessageSent.NONE_SENT)) {
|
||||
var requestId = messagingService.receiveRequest(1);
|
||||
order.messageSent = MessageSent.PAYMENT_TRYING;
|
||||
LOG.info("Order " + order.id + ": Payment Error message sent successfully,"
|
||||
+ " request Id: " + requestId);
|
||||
}
|
||||
}
|
||||
|
||||
private void employeeHandleIssue(Order order) {
|
||||
if (System.currentTimeMillis() - order.createdTime >= this.employeeTime) {
|
||||
LOG.trace("Order " + order.id + ": Employee handle time for order over, returning..");
|
||||
@@ -490,7 +517,7 @@ public class Commander {
|
||||
Retry.HandleErrorIssue<Order> handleError = (o, err) -> {
|
||||
if (!o.addedToEmployeeHandle && System
|
||||
.currentTimeMillis() - order.createdTime < employeeTime) {
|
||||
var qt = new QueueTask(order, TaskType.EmployeeDb, -1);
|
||||
var qt = new QueueTask(order, TaskType.EMPLOYEE_DB, -1);
|
||||
updateQueue(qt);
|
||||
LOG.warn("Order " + order.id + ": Error in adding to employee db,"
|
||||
+ " trying to queue task..");
|
||||
@@ -520,21 +547,21 @@ public class Commander {
|
||||
LOG.trace("Order " + qt.order.id + ": This queue task of type " + qt.getType()
|
||||
+ " does not need to be done anymore (timeout), dequeue..");
|
||||
} else {
|
||||
if (qt.taskType.equals(TaskType.Payment)) {
|
||||
if (!qt.order.paid.equals(PaymentStatus.Trying)) {
|
||||
if (qt.taskType.equals(TaskType.PAYMENT)) {
|
||||
if (!qt.order.paid.equals(PaymentStatus.TRYING)) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This payment task already done, dequeueing..");
|
||||
} else {
|
||||
sendPaymentRequest(qt.order);
|
||||
LOG.debug("Order " + qt.order.id + ": Trying to connect to payment service..");
|
||||
}
|
||||
} else if (qt.taskType.equals(TaskType.Messaging)) {
|
||||
if (qt.order.messageSent.equals(MessageSent.PaymentFail)
|
||||
|| qt.order.messageSent.equals(MessageSent.PaymentSuccessful)) {
|
||||
} else if (qt.taskType.equals(TaskType.MESSAGING)) {
|
||||
if (qt.order.messageSent.equals(MessageSent.PAYMENT_FAIL)
|
||||
|| qt.order.messageSent.equals(MessageSent.PAYMENT_SUCCESSFUL)) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This messaging task already done, dequeue..");
|
||||
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NoneSent)
|
||||
|| !qt.order.paid.equals(PaymentStatus.Trying))) {
|
||||
} else if (qt.messageType == 1 && (!qt.order.messageSent.equals(MessageSent.NONE_SENT)
|
||||
|| !qt.order.paid.equals(PaymentStatus.TRYING))) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This messaging task does not need to be done,"
|
||||
+ " dequeue..");
|
||||
@@ -548,7 +575,7 @@ public class Commander {
|
||||
sendSuccessMessage(qt.order);
|
||||
LOG.debug("Order " + qt.order.id + ": Trying to connect to messaging service..");
|
||||
}
|
||||
} else if (qt.taskType.equals(TaskType.EmployeeDb)) {
|
||||
} else if (qt.taskType.equals(TaskType.EMPLOYEE_DB)) {
|
||||
if (qt.order.addedToEmployeeHandle) {
|
||||
tryDequeue();
|
||||
LOG.trace("Order " + qt.order.id + ": This employee handle task already done,"
|
||||
|
@@ -33,11 +33,11 @@ import java.util.Random;
|
||||
public class Order { //can store all transactions ids also
|
||||
|
||||
enum PaymentStatus {
|
||||
NotDone, Trying, Done
|
||||
NOT_DONE, TRYING, DONE
|
||||
}
|
||||
|
||||
enum MessageSent {
|
||||
NoneSent, PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
NONE_SENT, PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
|
||||
}
|
||||
|
||||
final User user;
|
||||
@@ -65,8 +65,8 @@ public class Order { //can store all transactions ids also
|
||||
}
|
||||
this.id = id;
|
||||
USED_IDS.put(this.id, true);
|
||||
this.paid = PaymentStatus.Trying;
|
||||
this.messageSent = MessageSent.NoneSent;
|
||||
this.paid = PaymentStatus.TRYING;
|
||||
this.messageSent = MessageSent.NONE_SENT;
|
||||
this.addedToEmployeeHandle = false;
|
||||
}
|
||||
|
||||
|
@@ -38,7 +38,7 @@ public class MessagingService extends Service {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MessagingService.class);
|
||||
|
||||
enum MessageToSend {
|
||||
PaymentFail, PaymentTrying, PaymentSuccessful
|
||||
PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
|
||||
}
|
||||
|
||||
class MessageRequest {
|
||||
@@ -63,11 +63,11 @@ public class MessagingService extends Service {
|
||||
var id = generateId();
|
||||
MessageToSend msg;
|
||||
if (messageToSend == 0) {
|
||||
msg = MessageToSend.PaymentFail;
|
||||
msg = MessageToSend.PAYMENT_FAIL;
|
||||
} else if (messageToSend == 1) {
|
||||
msg = MessageToSend.PaymentTrying;
|
||||
msg = MessageToSend.PAYMENT_TRYING;
|
||||
} else { //messageToSend == 2
|
||||
msg = MessageToSend.PaymentSuccessful;
|
||||
msg = MessageToSend.PAYMENT_SUCCESSFUL;
|
||||
}
|
||||
var req = new MessageRequest(id, msg);
|
||||
return updateDb(req);
|
||||
@@ -84,10 +84,10 @@ public class MessagingService extends Service {
|
||||
}
|
||||
|
||||
String sendMessage(MessageToSend m) {
|
||||
if (m.equals(MessageToSend.PaymentSuccessful)) {
|
||||
if (m.equals(MessageToSend.PAYMENT_SUCCESSFUL)) {
|
||||
return "Msg: Your order has been placed and paid for successfully!"
|
||||
+ " Thank you for shopping with us!";
|
||||
} else if (m.equals(MessageToSend.PaymentTrying)) {
|
||||
} else if (m.equals(MessageToSend.PAYMENT_TRYING)) {
|
||||
return "Msg: There was an error in your payment process,"
|
||||
+ " we are working on it and will return back to you shortly."
|
||||
+ " Meanwhile, your order has been placed and will be shipped.";
|
||||
|
@@ -36,7 +36,7 @@ public class QueueTask {
|
||||
*/
|
||||
|
||||
public enum TaskType {
|
||||
Messaging, Payment, EmployeeDb
|
||||
MESSAGING, PAYMENT, EMPLOYEE_DB
|
||||
}
|
||||
|
||||
public Order order;
|
||||
@@ -68,7 +68,7 @@ public class QueueTask {
|
||||
* @return String representing type of task
|
||||
*/
|
||||
public String getType() {
|
||||
if (!this.taskType.equals(TaskType.Messaging)) {
|
||||
if (!this.taskType.equals(TaskType.MESSAGING)) {
|
||||
return this.taskType.toString();
|
||||
} else {
|
||||
if (this.messageType == 0) {
|
||||
|
@@ -23,15 +23,23 @@
|
||||
|
||||
package com.iluwatar.composite;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
Assertions.assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,14 +25,24 @@ package com.iluwatar.converter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* App running test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,12 +25,22 @@ package com.iluwatar.dao;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that DAO example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteDaoWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -43,6 +43,6 @@ public class AiComponent implements Component {
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
|
||||
// Do Nothing.
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ public class AiComponentManager {
|
||||
|
||||
private final int numEntities;
|
||||
|
||||
private static final Component[] AI_COMPONENTS = new AiComponent[MAX_ENTITIES];
|
||||
private final Component[] aiComponents = new AiComponent[MAX_ENTITIES];
|
||||
|
||||
public AiComponentManager(int numEntities) {
|
||||
this.numEntities = numEntities;
|
||||
@@ -51,7 +51,7 @@ public class AiComponentManager {
|
||||
*/
|
||||
public void start() {
|
||||
LOGGER.info("Start AI Game Component");
|
||||
IntStream.range(0, numEntities).forEach(i -> AI_COMPONENTS[i] = new AiComponent());
|
||||
IntStream.range(0, numEntities).forEach(i -> aiComponents[i] = new AiComponent());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +60,7 @@ public class AiComponentManager {
|
||||
public void update() {
|
||||
LOGGER.info("Update AI Game Component");
|
||||
IntStream.range(0, numEntities)
|
||||
.filter(i -> AI_COMPONENTS.length > i && AI_COMPONENTS[i] != null)
|
||||
.forEach(i -> AI_COMPONENTS[i].update());
|
||||
.filter(i -> aiComponents.length > i && aiComponents[i] != null)
|
||||
.forEach(i -> aiComponents[i].update());
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ public class PhysicsComponentManager {
|
||||
|
||||
private final int numEntities;
|
||||
|
||||
private static final Component[] PHYSICS_COMPONENTS = new PhysicsComponent[MAX_ENTITIES];
|
||||
private final Component[] physicsComponents = new PhysicsComponent[MAX_ENTITIES];
|
||||
|
||||
public PhysicsComponentManager(int numEntities) {
|
||||
this.numEntities = numEntities;
|
||||
@@ -51,7 +51,7 @@ public class PhysicsComponentManager {
|
||||
*/
|
||||
public void start() {
|
||||
LOGGER.info("Start Physics Game Component ");
|
||||
IntStream.range(0, numEntities).forEach(i -> PHYSICS_COMPONENTS[i] = new PhysicsComponent());
|
||||
IntStream.range(0, numEntities).forEach(i -> physicsComponents[i] = new PhysicsComponent());
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public class PhysicsComponentManager {
|
||||
LOGGER.info("Update Physics Game Component ");
|
||||
// Process physics.
|
||||
IntStream.range(0, numEntities)
|
||||
.filter(i -> PHYSICS_COMPONENTS.length > i && PHYSICS_COMPONENTS[i] != null)
|
||||
.forEach(i -> PHYSICS_COMPONENTS[i].update());
|
||||
.filter(i -> physicsComponents.length > i && physicsComponents[i] != null)
|
||||
.forEach(i -> physicsComponents[i].update());
|
||||
}
|
||||
}
|
||||
|
@@ -40,7 +40,7 @@ public class RenderComponentManager {
|
||||
|
||||
private final int numEntities;
|
||||
|
||||
private static final Component[] RENDER_COMPONENTS = new RenderComponent[MAX_ENTITIES];
|
||||
private final Component[] renderComponents = new RenderComponent[MAX_ENTITIES];
|
||||
|
||||
public RenderComponentManager(int numEntities) {
|
||||
this.numEntities = numEntities;
|
||||
@@ -51,7 +51,7 @@ public class RenderComponentManager {
|
||||
*/
|
||||
public void start() {
|
||||
LOGGER.info("Start Render Game Component ");
|
||||
IntStream.range(0, numEntities).forEach(i -> RENDER_COMPONENTS[i] = new RenderComponent());
|
||||
IntStream.range(0, numEntities).forEach(i -> renderComponents[i] = new RenderComponent());
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public class RenderComponentManager {
|
||||
LOGGER.info("Update Render Game Component ");
|
||||
// Process Render.
|
||||
IntStream.range(0, numEntities)
|
||||
.filter(i -> RENDER_COMPONENTS.length > i && RENDER_COMPONENTS[i] != null)
|
||||
.forEach(i -> RENDER_COMPONENTS[i].render());
|
||||
.filter(i -> renderComponents.length > i && renderComponents[i] != null)
|
||||
.forEach(i -> renderComponents[i].render());
|
||||
}
|
||||
}
|
||||
|
@@ -26,16 +26,22 @@ package com.iluwatar.data.locality;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Test Game Application
|
||||
*/
|
||||
class ApplicationTest {
|
||||
|
||||
/**
|
||||
* Test run
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link Application#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
void main() {
|
||||
Application.main(new String[] {});
|
||||
void shouldExecuteGameApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> Application.main(new String[] {}));
|
||||
}
|
||||
}
|
@@ -24,14 +24,25 @@
|
||||
package com.iluwatar.datamapper;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Data-Mapper example runs without errors.
|
||||
*/
|
||||
public final class AppTest {
|
||||
final class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main();
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
|
||||
assertDoesNotThrow((Executable) App::main);
|
||||
}
|
||||
}
|
||||
|
@@ -25,9 +25,19 @@ package com.iluwatar.datatransfer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AppTest {
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.decorator;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,14 +25,23 @@ package com.iluwatar.delegation.simple;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test Entry
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.dependency.injection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -31,7 +31,6 @@
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.23.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>dirty-flag</artifactId>
|
||||
<version>1.23.0-SNAPSHOT</version>
|
||||
<name>dirty-flag</name>
|
||||
@@ -45,6 +44,12 @@
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@@ -26,12 +26,22 @@ package org.dirty.flag;
|
||||
import com.iluwatar.dirtyflag.App;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Dirty-Flag example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -23,29 +23,27 @@
|
||||
|
||||
package org.dirty.flag;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.iluwatar.dirtyflag.DataFetcher;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class DirtyFlagTest {
|
||||
class DirtyFlagTest {
|
||||
|
||||
@Test
|
||||
public void testIsDirty() {
|
||||
void testIsDirty() {
|
||||
var df = new DataFetcher();
|
||||
var countries = df.fetch();
|
||||
assertFalse(countries.isEmpty());
|
||||
Assertions.assertFalse(countries.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotDirty() {
|
||||
void testIsNotDirty() {
|
||||
var df = new DataFetcher();
|
||||
df.fetch();
|
||||
var countries = df.fetch();
|
||||
assertTrue(countries.isEmpty());
|
||||
Assertions.assertTrue(countries.isEmpty());
|
||||
}
|
||||
}
|
||||
|
@@ -44,6 +44,11 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@@ -25,14 +25,23 @@ package com.iluwatar.doublebuffer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* App unit test.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
App.main(new String[]{});
|
||||
public void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,13 +25,23 @@ package com.iluwatar.doublechecked.locking;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,23 @@ package com.iluwatar.doubledispatch;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.eip.aggregator;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Test for App class
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMain() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.eip.message.channel;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.eip.publish.subscribe;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.eip.splitter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Test for App class
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMain() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.eip.wiretap;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Test for App class
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testMain() throws Exception {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,22 @@ package com.iluwatar.event.aggregator;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,22 @@ package com.iluwatar.event.asynchronous;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that EventAsynchronous example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,12 +25,22 @@ package com.iluwatar.eda;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Event Driven Architecture example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
/**
|
||||
* Issue: Add at least one assertion to this test case.
|
||||
*
|
||||
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||
* throws an exception.
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -29,19 +29,21 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests execute-around example.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
@AfterEach
|
||||
public void cleanup() {
|
||||
void cleanup() {
|
||||
var file = new File("testfile.txt");
|
||||
file.delete();
|
||||
}
|
||||
|
@@ -45,4 +45,8 @@ public class Commander implements CommanderExtension {
|
||||
public void commanderReady() {
|
||||
LOGGER.info("[Commander] " + unit.getName() + " is ready!");
|
||||
}
|
||||
|
||||
public CommanderUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
@@ -43,6 +43,10 @@ public class Sergeant implements SergeantExtension {
|
||||
|
||||
@Override
|
||||
public void sergeantReady() {
|
||||
LOGGER.info("[Sergeant] " + unit.getName() + " is ready! ");
|
||||
LOGGER.info("[Sergeant] " + unit.getName() + " is ready!");
|
||||
}
|
||||
|
||||
public SergeantUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
@@ -42,6 +42,10 @@ public class Soldier implements SoldierExtension {
|
||||
|
||||
@Override
|
||||
public void soldierReady() {
|
||||
LOGGER.info("[Solider] " + unit.getName() + " is ready!");
|
||||
LOGGER.info("[Soldier] " + unit.getName() + " is ready!");
|
||||
}
|
||||
|
||||
public SoldierUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
@@ -23,13 +23,16 @@
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Created by Srdjan on 03-May-17.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void main() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
@@ -23,17 +23,43 @@
|
||||
|
||||
package concreteextensions;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import units.CommanderUnit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Created by Srdjan on 03-May-17.
|
||||
*
|
||||
* Modified by ToxicDreamz on 15-Aug-20
|
||||
*/
|
||||
public class CommanderTest {
|
||||
class CommanderTest {
|
||||
|
||||
@Test
|
||||
public void commanderReady() {
|
||||
void shouldExecuteCommanderReady() {
|
||||
|
||||
Logger commanderLogger = (Logger) LoggerFactory.getLogger(Commander.class);
|
||||
|
||||
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
|
||||
listAppender.start();
|
||||
|
||||
commanderLogger.addAppender(listAppender);
|
||||
|
||||
final var commander = new Commander(new CommanderUnit("CommanderUnitTest"));
|
||||
commander.commanderReady();
|
||||
|
||||
List<ILoggingEvent> logsList = listAppender.list;
|
||||
assertEquals("[Commander] " + commander.getUnit().getName() + " is ready!", logsList.get(0)
|
||||
.getMessage());
|
||||
assertEquals(Level.INFO, logsList.get(0)
|
||||
.getLevel());
|
||||
}
|
||||
|
||||
}
|
@@ -23,17 +23,41 @@
|
||||
|
||||
package concreteextensions;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import units.SergeantUnit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Created by Srdjan on 03-May-17.
|
||||
*/
|
||||
public class SergeantTest {
|
||||
class SergeantTest {
|
||||
|
||||
@Test
|
||||
public void sergeantReady() {
|
||||
void sergeantReady() {
|
||||
|
||||
Logger sergeantLogger = (Logger) LoggerFactory.getLogger(Sergeant.class);
|
||||
|
||||
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
|
||||
listAppender.start();
|
||||
|
||||
sergeantLogger.addAppender(listAppender);
|
||||
|
||||
final var sergeant = new Sergeant(new SergeantUnit("SergeantUnitTest"));
|
||||
sergeant.sergeantReady();
|
||||
|
||||
List<ILoggingEvent> logsList = listAppender.list;
|
||||
assertEquals("[Sergeant] " + sergeant.getUnit().getName() + " is ready!", logsList.get(0)
|
||||
.getMessage());
|
||||
assertEquals(Level.INFO, logsList.get(0)
|
||||
.getLevel());
|
||||
}
|
||||
|
||||
}
|
@@ -23,17 +23,41 @@
|
||||
|
||||
package concreteextensions;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import units.SoldierUnit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Created by Srdjan on 03-May-17.
|
||||
*/
|
||||
public class SoldierTest {
|
||||
class SoldierTest {
|
||||
|
||||
@Test
|
||||
public void soldierReady() {
|
||||
void soldierReady() {
|
||||
|
||||
Logger soldierLogger = (Logger) LoggerFactory.getLogger(Soldier.class);
|
||||
|
||||
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
|
||||
listAppender.start();
|
||||
|
||||
soldierLogger.addAppender(listAppender);
|
||||
|
||||
final var soldier = new Soldier(new SoldierUnit("SoldierUnitTest"));
|
||||
soldier.soldierReady();
|
||||
|
||||
List<ILoggingEvent> logsList = listAppender.list;
|
||||
assertEquals("[Soldier] " + soldier.getUnit().getName() + " is ready!", logsList.get(0)
|
||||
.getMessage());
|
||||
assertEquals(Level.INFO, logsList.get(0)
|
||||
.getLevel());
|
||||
}
|
||||
|
||||
}
|
@@ -25,13 +25,15 @@ package com.iluwatar.facade;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -26,14 +26,16 @@ package com.iluwatar.factorykit.app;
|
||||
import com.iluwatar.factorykit.App;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test Entrypoint
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -25,12 +25,15 @@ package com.iluwatar.factory.method;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Tests that Factory Method example runs without errors.
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
204
filterer/README.MD
Normal file
204
filterer/README.MD
Normal file
@@ -0,0 +1,204 @@
|
||||
--- # this is so called 'Yaml Front Matter', read up on it here: http://jekyllrb.com/docs/frontmatter/
|
||||
layout: pattern
|
||||
title: Filterer
|
||||
folder: filterer
|
||||
permalink: /patterns/filterer/
|
||||
description: Design pattern that helps container-like objects to return filtered version of themselves.# short meta description that shows in Google search results
|
||||
categories:
|
||||
- Functional
|
||||
tags:
|
||||
- Extensibility
|
||||
---
|
||||
|
||||
## Name / classification
|
||||
Filterer
|
||||
|
||||
## Intent
|
||||
The intent of this design pattern is to introduce a functional interface that will add a functionality for container-like objects to easily return filtered versions of themselves.
|
||||
|
||||
## Explanation
|
||||
Real world example
|
||||
|
||||
> We are designing a threat(malware) detection system. We can have different types of threats and systems. We have a requirement that
|
||||
> system should be aware of threats that are present in it. In the design we have to take into consideration that new Threat types can be
|
||||
> added later. Also there is a requirement that a system can filter itself based on the threats that it possesses (system acts as container-like object for threats).
|
||||
>
|
||||
|
||||
In plain words
|
||||
|
||||
> We need to be able to filter different types of systems(container-like objects) based on properties of Threats that they contain.
|
||||
> Adding new properties for Threats should be easy (we still need the ability to filter by those new properties).
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
To model the threat detection example presented above we introduce `Threat` and `ThreatAwareSystem` interfaces.
|
||||
|
||||
```java
|
||||
public interface Threat {
|
||||
String name();
|
||||
int id();
|
||||
ThreatType type();
|
||||
}
|
||||
|
||||
public interface ThreatAwareSystem {
|
||||
String systemId();
|
||||
List<? extends Threat> threats();
|
||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||
|
||||
}
|
||||
```
|
||||
Notice the `filtered` method that returns instance of `Filterer` interface which is defined as :
|
||||
```java
|
||||
@FunctionalInterface
|
||||
public interface Filterer<G, E> {
|
||||
G by(Predicate<? super E> predicate);
|
||||
}
|
||||
```
|
||||
it is used to fulfill the requirement for system to be able to filter itself based on threat properties.
|
||||
The container-like object (`ThreatAwareSystem` in our case) needs to have a method that returns an instance of `Filterer`. This helper interface gives
|
||||
ability to covariantly specify a lower bound of contravariant `Predicate` in the subinterfaces of interfaces representing the container-like objects.
|
||||
|
||||
In our example we will be able to pass a predicate that takes `? extends Threat` object and return `? extends ThreatAwareSystem`
|
||||
from `Filtered::by` method. A simple implementation of `ThreatAwareSystem` :
|
||||
```java
|
||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<Threat> issues;
|
||||
|
||||
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
|
||||
this.systemId = systemId;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
|
||||
return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
```
|
||||
the `filtered` method is overridden to filter the threats list by given predicate.
|
||||
|
||||
Now if we introduce a new subtype of `Threat` interface that adds probability with which given threat can appear :
|
||||
```java
|
||||
public interface ProbableThreat extends Threat {
|
||||
double probability();
|
||||
}
|
||||
```
|
||||
we can also introduce a new interface that represents a system that is aware of threats with their probabilities :
|
||||
````java
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
@Override
|
||||
List<? extends ProbableThreat> threats();
|
||||
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
````
|
||||
Notice how we override the `filtered` method in `ProbabilisticThreatAwareSystem` and specify different return covariant type
|
||||
by specifying different generic types. Our interfaces are clean and not cluttered by default implementations. We
|
||||
we will be able to filter `ProbabilisticThreatAwareSystem` by `ProbableThreat` properties :
|
||||
```java
|
||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<ProbableThreat> threats;
|
||||
|
||||
public SimpleProbabilisticThreatAwareSystem(final String systemId, final List<ProbableThreat> threats) {
|
||||
this.systemId = systemId;
|
||||
this.threats = ImmutableList.copyOf(threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ProbabilisticThreatAwareSystem filteredGroup(final Predicate<? super ProbableThreat> predicate) {
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<ProbableThreat> filteredItems(final Predicate<? super ProbableThreat> predicate) {
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Now if we want filter `ThreatAwareSystem` by threat type we can do :
|
||||
```java
|
||||
Threat rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
Threat trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
ThreatAwareSystem threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
ThreatAwareSystem rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
```
|
||||
or if we want to filter `ProbabilisticThreatAwareSystem` :
|
||||
```java
|
||||
ProbableThreat malwareTroyan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
ProbableThreat rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
|
||||
List<ProbableThreat> probableThreats = List.of(malwareTroyan, rootkit);
|
||||
|
||||
ProbabilisticThreatAwareSystem simpleProbabilisticThreatAwareSystem =new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
ProbabilisticThreatAwareSystem filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
```
|
||||
## Class diagram
|
||||

|
||||
|
||||
## Applicability
|
||||
Pattern can be used when working with container-like objects that use subtyping, instead of parametrizing(generics) for extensible class structure.
|
||||
It enables you to easily extend filtering ability of container-like objects as business requirements change.
|
||||
|
||||
## Tutorials
|
||||
* [Article about Filterer pattern posted on it's author's blog](https://blog.tlinkowski.pl/2018/filterer-pattern/)
|
||||
* [Application of Filterer pattern in domain of text analysis](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html)
|
||||
|
||||
## Known uses
|
||||
One of the uses is present on the blog presented in [this](https://www.javacodegeeks.com/2019/02/filterer-pattern-10-steps.html) link.
|
||||
It presents how to use `Filterer` pattern to create text issue analyzer with support for test cases used for unit testing.
|
||||
|
||||
## Consequences
|
||||
Pros :
|
||||
* you can easily introduce new subtypes for container-like objects and subtypes for objects that are contained within them and still be able to filter easily be new properties of those new subtypes.
|
||||
|
||||
Cons :
|
||||
* covariant return types mixed with generics can be sometimes tricky
|
||||
|
||||
## Credits
|
||||
* Author of the pattern : [Tomasz Linkowski](https://tlinkowski.pl/)
|
BIN
filterer/etc/filterer.png
Normal file
BIN
filterer/etc/filterer.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 128 KiB |
96
filterer/etc/filterer.urm.puml
Normal file
96
filterer/etc/filterer.urm.puml
Normal file
@@ -0,0 +1,96 @@
|
||||
@startuml
|
||||
package com.iluwatar.filterer.domain {
|
||||
interface Filterer<G, E> {
|
||||
+ by(Predicate<? super E>) : G {abstract}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.filterer {
|
||||
class App {
|
||||
- LOGGER : Logger {static}
|
||||
+ App()
|
||||
- filteringSimpleProbableThreats() {static}
|
||||
- filteringSimpleThreats() {static}
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
}
|
||||
package com.iluwatar.filterer.threat {
|
||||
interface ProbabilisticThreatAwareSystem {
|
||||
+ filtered() : Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> {abstract}
|
||||
+ threats() : List<? extends ProbableThreat> {abstract}
|
||||
}
|
||||
interface ProbableThreat {
|
||||
+ probability() : double {abstract}
|
||||
}
|
||||
class SimpleProbabilisticThreatAwareSystem {
|
||||
- systemId : String
|
||||
- threats : ImmutableList<ProbableThreat>
|
||||
+ SimpleProbabilisticThreatAwareSystem(systemId : String, threats : List<ProbableThreat>)
|
||||
+ equals(o : Object) : boolean
|
||||
+ filtered() : Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat>
|
||||
- filteredGroup(predicate : Predicate<? super ProbableThreat>) : ProbabilisticThreatAwareSystem
|
||||
- filteredItems(predicate : Predicate<? super ProbableThreat>) : List<ProbableThreat>
|
||||
+ hashCode() : int
|
||||
+ systemId() : String
|
||||
+ threats() : List<? extends ProbableThreat>
|
||||
+ toString() : String
|
||||
}
|
||||
class SimpleProbableThreat {
|
||||
- probability : double
|
||||
+ SimpleProbableThreat(name : String, id : int, threatType : ThreatType, probability : double)
|
||||
+ equals(o : Object) : boolean
|
||||
+ hashCode() : int
|
||||
+ probability() : double
|
||||
+ toString() : String
|
||||
}
|
||||
class SimpleThreat {
|
||||
- id : int
|
||||
- name : String
|
||||
- threatType : ThreatType
|
||||
+ SimpleThreat(threatType : ThreatType, id : int, name : String)
|
||||
+ id() : int
|
||||
+ name() : String
|
||||
+ toString() : String
|
||||
+ type() : ThreatType
|
||||
}
|
||||
class SimpleThreatAwareSystem {
|
||||
- issues : ImmutableList<Threat>
|
||||
- systemId : String
|
||||
+ SimpleThreatAwareSystem(systemId : String, issues : List<Threat>)
|
||||
+ equals(o : Object) : boolean
|
||||
+ filtered() : Filterer<? extends ThreatAwareSystem, ? extends Threat>
|
||||
- filteredGroup(predicate : Predicate<? super Threat>) : ThreatAwareSystem
|
||||
- filteredItems(predicate : Predicate<? super Threat>) : List<Threat>
|
||||
+ hashCode() : int
|
||||
+ systemId() : String
|
||||
+ threats() : List<? extends Threat>
|
||||
+ toString() : String
|
||||
}
|
||||
interface Threat {
|
||||
+ id() : int {abstract}
|
||||
+ name() : String {abstract}
|
||||
+ type() : ThreatType {abstract}
|
||||
}
|
||||
interface ThreatAwareSystem {
|
||||
+ filtered() : Filterer<? extends ThreatAwareSystem, ? extends Threat> {abstract}
|
||||
+ systemId() : String {abstract}
|
||||
+ threats() : List<? extends Threat> {abstract}
|
||||
}
|
||||
enum ThreatType {
|
||||
+ ROOTKIT {static}
|
||||
+ TROJAN {static}
|
||||
+ WORM {static}
|
||||
+ valueOf(name : String) : ThreatType {static}
|
||||
+ values() : ThreatType[] {static}
|
||||
}
|
||||
}
|
||||
SimpleThreatAwareSystem --> "-issues" Threat
|
||||
SimpleThreat --> "-threatType" ThreatType
|
||||
SimpleProbabilisticThreatAwareSystem --> "-threats" ProbableThreat
|
||||
ProbabilisticThreatAwareSystem --|> ThreatAwareSystem
|
||||
ProbableThreat --|> Threat
|
||||
SimpleProbabilisticThreatAwareSystem ..|> ProbabilisticThreatAwareSystem
|
||||
SimpleProbableThreat ..|> ProbableThreat
|
||||
SimpleProbableThreat --|> SimpleThreat
|
||||
SimpleThreat ..|> Threat
|
||||
SimpleThreatAwareSystem ..|> ThreatAwareSystem
|
||||
@enduml
|
73
filterer/pom.xml
Normal file
73
filterer/pom.xml
Normal file
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright © 2014-2019 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<version>1.23.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>filterer</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.iluwatar.filterer.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
108
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
108
filterer/src/main/java/com/iluwatar/filterer/App.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer;
|
||||
|
||||
import com.iluwatar.filterer.threat.ProbableThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleProbabilisticThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.SimpleProbableThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleThreat;
|
||||
import com.iluwatar.filterer.threat.SimpleThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.Threat;
|
||||
import com.iluwatar.filterer.threat.ThreatAwareSystem;
|
||||
import com.iluwatar.filterer.threat.ThreatType;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This demo class represent how {@link com.iluwatar.filterer.domain.Filterer} pattern is used to
|
||||
* filter container-like objects to return filtered versions of themselves. The container like
|
||||
* objects are systems that are aware of threats that they can be vulnerable to. We would like
|
||||
* to have a way to create copy of different system objects but with filtered threats.
|
||||
* The thing is to keep it simple if we add new subtype of {@link Threat}
|
||||
* (for example {@link ProbableThreat}) - we still need to be able to filter by it's properties.
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
filteringSimpleThreats();
|
||||
filteringSimpleProbableThreats();
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrates how to filter {@link com.iluwatar.filterer.threat.ProbabilisticThreatAwareSystem}
|
||||
* based on probability property. The @{@link com.iluwatar.filterer.domain.Filterer#by(Predicate)}
|
||||
* method is able to use {@link com.iluwatar.filterer.threat.ProbableThreat}
|
||||
* as predicate argument.
|
||||
*/
|
||||
private static void filteringSimpleProbableThreats() {
|
||||
LOGGER.info(" ### Filtering ProbabilisticThreatAwareSystem by probability ###");
|
||||
|
||||
var trojanArcBomb = new SimpleProbableThreat("Trojan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
var rootkit = new SimpleProbableThreat("Rootkit-Kernel", 2, ThreatType.ROOTKIT, 0.8);
|
||||
|
||||
List<ProbableThreat> probableThreats = List.of(trojanArcBomb, rootkit);
|
||||
|
||||
var probabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("Sys-1", probableThreats);
|
||||
|
||||
LOGGER.info("Filtering ProbabilisticThreatAwareSystem. Initial : "
|
||||
+ probabilisticThreatAwareSystem);
|
||||
|
||||
//Filtering using filterer
|
||||
var filteredThreatAwareSystem = probabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
LOGGER.info("Filtered by probability = 0.99 : " + filteredThreatAwareSystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Demonstrates how to filter {@link ThreatAwareSystem} based on startingOffset property
|
||||
* of {@link SimpleThreat}. The @{@link com.iluwatar.filterer.domain.Filterer#by(Predicate)}
|
||||
* method is able to use {@link Threat} as predicate argument.
|
||||
*/
|
||||
private static void filteringSimpleThreats() {
|
||||
LOGGER.info("### Filtering ThreatAwareSystem by ThreatType ###");
|
||||
|
||||
var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
var threatAwareSystem = new SimpleThreatAwareSystem("Sys-1", threats);
|
||||
|
||||
LOGGER.info("Filtering ThreatAwareSystem. Initial : " + threatAwareSystem);
|
||||
|
||||
//Filtering using Filterer
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
LOGGER.info("Filtered by threatType = ROOTKIT : " + rootkitThreatAwareSystem);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.domain;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Filterer helper interface.
|
||||
* @param <G> type of the container-like object.
|
||||
* @param <E> type of the elements contained within this container-like object.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Filterer<G, E> {
|
||||
G by(Predicate<? super E> predicate);
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of it's threats with given probability of their occurrence.
|
||||
*/
|
||||
public interface ProbabilisticThreatAwareSystem extends ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
List<? extends ProbableThreat> threats();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered();
|
||||
}
|
||||
|
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents threat that might be a threat with given probability.
|
||||
*/
|
||||
public interface ProbableThreat extends Threat {
|
||||
/**
|
||||
* Returns probability of occurrence of given threat.
|
||||
* @return probability of occurrence of given threat.
|
||||
*/
|
||||
double probability();
|
||||
}
|
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleProbabilisticThreatAwareSystem implements ProbabilisticThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<ProbableThreat> threats;
|
||||
|
||||
public SimpleProbabilisticThreatAwareSystem(
|
||||
final String systemId,
|
||||
final List<ProbableThreat> threats
|
||||
) {
|
||||
this.systemId = systemId;
|
||||
this.threats = ImmutableList.copyOf(threats);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends ProbableThreat> threats() {
|
||||
return threats;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends ProbabilisticThreatAwareSystem, ? extends ProbableThreat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ProbabilisticThreatAwareSystem filteredGroup(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return new SimpleProbabilisticThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<ProbableThreat> filteredItems(
|
||||
final Predicate<? super ProbableThreat> predicate
|
||||
) {
|
||||
return this.threats.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleProbabilisticThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& threats.equals(that.threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(systemId, threats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbabilisticThreatAwareSystem{"
|
||||
+ "systemId='" + systemId + '\''
|
||||
+ ", threats=" + threats
|
||||
+ '}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleProbableThreat extends SimpleThreat implements ProbableThreat {
|
||||
|
||||
private final double probability;
|
||||
|
||||
public SimpleProbableThreat(final String name,
|
||||
final int id,
|
||||
final ThreatType threatType,
|
||||
final double probability
|
||||
) {
|
||||
super(threatType, id, name);
|
||||
this.probability = probability;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public double probability() {
|
||||
return probability;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleProbableThreat) o;
|
||||
return Double.compare(that.probability, probability) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), probability);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleProbableThreat{"
|
||||
+ "probability=" + probability
|
||||
+ "} "
|
||||
+ super.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a simple threat.
|
||||
*/
|
||||
public class SimpleThreat implements Threat {
|
||||
|
||||
private final ThreatType threatType;
|
||||
private final int id;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param threatType {@link ThreatType}.
|
||||
* @param id threat id.
|
||||
* @param name threat name.
|
||||
*/
|
||||
public SimpleThreat(final ThreatType threatType, final int id, String name) {
|
||||
this.threatType = threatType;
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String name() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ThreatType type() {
|
||||
return threatType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleThreat) o;
|
||||
return id == that.id
|
||||
&& threatType == that.threatType
|
||||
&& Objects.equals(name, that.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(threatType, id, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreat{"
|
||||
+ "threatType=" + threatType
|
||||
+ ", id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public class SimpleThreatAwareSystem implements ThreatAwareSystem {
|
||||
|
||||
private final String systemId;
|
||||
private final ImmutableList<Threat> issues;
|
||||
|
||||
public SimpleThreatAwareSystem(final String systemId, final List<Threat> issues) {
|
||||
this.systemId = systemId;
|
||||
this.issues = ImmutableList.copyOf(issues);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String systemId() {
|
||||
return systemId;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public List<? extends Threat> threats() {
|
||||
return new ArrayList<>(issues);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered() {
|
||||
return this::filteredGroup;
|
||||
}
|
||||
|
||||
private ThreatAwareSystem filteredGroup(Predicate<? super Threat> predicate) {
|
||||
return new SimpleThreatAwareSystem(this.systemId, filteredItems(predicate));
|
||||
}
|
||||
|
||||
private List<Threat> filteredItems(Predicate<? super Threat> predicate) {
|
||||
return this.issues.stream()
|
||||
.filter(predicate)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
var that = (SimpleThreatAwareSystem) o;
|
||||
return systemId.equals(that.systemId)
|
||||
&& issues.equals(that.issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(systemId, issues);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleThreatAwareSystem{"
|
||||
+ "systemId='" + systemId
|
||||
+ '\'' + ", issues=" + issues
|
||||
+ '}';
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
/**
|
||||
* Represents a threat that can be detected in given system.
|
||||
*/
|
||||
public interface Threat {
|
||||
/**
|
||||
* Returns name of the threat.
|
||||
*
|
||||
* @return value representing name of the threat.
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* Returns unique id of the threat.
|
||||
*
|
||||
* @return value representing threat id.
|
||||
*/
|
||||
int id();
|
||||
|
||||
/**
|
||||
* Returns threat type.
|
||||
* @return {@link ThreatType}
|
||||
*/
|
||||
ThreatType type();
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import com.iluwatar.filterer.domain.Filterer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents system that is aware of threats that are present in it.
|
||||
*/
|
||||
public interface ThreatAwareSystem {
|
||||
|
||||
/**
|
||||
* Returns the system id.
|
||||
*
|
||||
* @return system id.
|
||||
*/
|
||||
String systemId();
|
||||
|
||||
/**
|
||||
* Returns list of threats for this system.
|
||||
* @return list of threats for this system.
|
||||
*/
|
||||
List<? extends Threat> threats();
|
||||
|
||||
/**
|
||||
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
|
||||
* specify lower bound for predicate that we want to filter by.
|
||||
* @return an instance of {@link Filterer} helper interface.
|
||||
*/
|
||||
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
public enum ThreatType { TROJAN, WORM, ROOTKIT }
|
33
filterer/src/test/java/com/iluwatar/filterer/AppTest.java
Normal file
33
filterer/src/test/java/com/iluwatar/filterer/AppTest.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AppTest {
|
||||
@Test
|
||||
void shouldLaunchApp() {
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SimpleProbabilisticThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByProbability() {
|
||||
//given
|
||||
var trojan = new SimpleProbableThreat("Troyan-ArcBomb", 1, ThreatType.TROJAN, 0.99);
|
||||
var rootkit = new SimpleProbableThreat("Rootkit-System", 2, ThreatType.ROOTKIT, 0.8);
|
||||
List<ProbableThreat> probableThreats = List.of(trojan, rootkit);
|
||||
|
||||
var simpleProbabilisticThreatAwareSystem =
|
||||
new SimpleProbabilisticThreatAwareSystem("System-1", probableThreats);
|
||||
|
||||
//when
|
||||
var filtered = simpleProbabilisticThreatAwareSystem.filtered()
|
||||
.by(probableThreat -> Double.compare(probableThreat.probability(), 0.99) == 0);
|
||||
|
||||
//then
|
||||
assertEquals(filtered.threats().size(), 1);
|
||||
assertEquals(filtered.threats().get(0), trojan);
|
||||
}
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.filterer.threat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SimpleThreatAwareSystemTest {
|
||||
@Test
|
||||
void shouldFilterByThreatType() {
|
||||
//given
|
||||
var rootkit = new SimpleThreat(ThreatType.ROOTKIT, 1, "Simple-Rootkit");
|
||||
var trojan = new SimpleThreat(ThreatType.TROJAN, 2, "Simple-Trojan");
|
||||
List<Threat> threats = List.of(rootkit, trojan);
|
||||
|
||||
var threatAwareSystem = new SimpleThreatAwareSystem("System-1", threats);
|
||||
|
||||
//when
|
||||
var rootkitThreatAwareSystem = threatAwareSystem.filtered()
|
||||
.by(threat -> threat.type() == ThreatType.ROOTKIT);
|
||||
|
||||
//then
|
||||
assertEquals(rootkitThreatAwareSystem.threats().size(), 1);
|
||||
assertEquals(rootkitThreatAwareSystem.threats().get(0), rootkit);
|
||||
}
|
||||
}
|
@@ -25,13 +25,15 @@ package com.iluwatar.fluentinterface.app;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application Test Entry
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -57,15 +57,10 @@ public final class Dispatcher {
|
||||
*/
|
||||
public void menuItemSelected(MenuItem menuItem) {
|
||||
dispatchAction(new MenuAction(menuItem));
|
||||
switch (menuItem) {
|
||||
case HOME:
|
||||
case PRODUCTS:
|
||||
default:
|
||||
dispatchAction(new ContentAction(Content.PRODUCTS));
|
||||
break;
|
||||
case COMPANY:
|
||||
dispatchAction(new ContentAction(Content.COMPANY));
|
||||
break;
|
||||
if (menuItem == MenuItem.COMPANY) {
|
||||
dispatchAction(new ContentAction(Content.COMPANY));
|
||||
} else {
|
||||
dispatchAction(new ContentAction(Content.PRODUCTS));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -25,13 +25,15 @@ package com.iluwatar.flux.app;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,15 @@ package com.iluwatar.flyweight;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -25,13 +25,15 @@ package com.iluwatar.front.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -39,6 +39,12 @@
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@@ -25,14 +25,16 @@ package com.iluwatar.gameloop;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* App unit test class.
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
new App().main(new String[]{});
|
||||
public void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,13 +25,15 @@ package com.iluwatar.halfsynchalfasync;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Application test
|
||||
*/
|
||||
public class AppTest {
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(null);
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(null));
|
||||
}
|
||||
}
|
||||
|
@@ -46,6 +46,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
private static final String DEFAULT_DB = "lotteryDB";
|
||||
private static final String DEFAULT_TICKETS_COLLECTION = "lotteryTickets";
|
||||
private static final String DEFAULT_COUNTERS_COLLECTION = "counters";
|
||||
private static final String TICKET_ID = "ticketId";
|
||||
|
||||
private MongoClient mongoClient;
|
||||
private MongoDatabase database;
|
||||
@@ -93,7 +94,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
}
|
||||
|
||||
private void initCounters() {
|
||||
var doc = new Document("_id", "ticketId").append("seq", 1);
|
||||
var doc = new Document("_id", TICKET_ID).append("seq", 1);
|
||||
countersCollection.insertOne(doc);
|
||||
}
|
||||
|
||||
@@ -103,7 +104,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
* @return next ticket id
|
||||
*/
|
||||
public int getNextId() {
|
||||
var find = new Document("_id", "ticketId");
|
||||
var find = new Document("_id", TICKET_ID);
|
||||
var increase = new Document("seq", 1);
|
||||
var update = new Document("$inc", increase);
|
||||
var result = countersCollection.findOneAndUpdate(find, update);
|
||||
@@ -131,7 +132,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
@Override
|
||||
public Optional<LotteryTicket> findById(LotteryTicketId id) {
|
||||
return ticketsCollection
|
||||
.find(new Document("ticketId", id.getId()))
|
||||
.find(new Document(TICKET_ID, id.getId()))
|
||||
.limit(1)
|
||||
.into(new ArrayList<>())
|
||||
.stream()
|
||||
@@ -142,7 +143,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
@Override
|
||||
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
|
||||
var ticketId = getNextId();
|
||||
var doc = new Document("ticketId", ticketId);
|
||||
var doc = new Document(TICKET_ID, ticketId);
|
||||
doc.put("email", ticket.getPlayerDetails().getEmail());
|
||||
doc.put("bank", ticket.getPlayerDetails().getBankAccount());
|
||||
doc.put("phone", ticket.getPlayerDetails().getPhoneNumber());
|
||||
@@ -173,7 +174,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
.map(Integer::parseInt)
|
||||
.collect(Collectors.toSet());
|
||||
var lotteryNumbers = LotteryNumbers.create(numbers);
|
||||
var ticketId = new LotteryTicketId(doc.getInteger("ticketId"));
|
||||
var ticketId = new LotteryTicketId(doc.getInteger(TICKET_ID));
|
||||
return new LotteryTicket(ticketId, playerDetails, lotteryNumbers);
|
||||
}
|
||||
}
|
||||
|
@@ -36,6 +36,9 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
private static final String DEFAULT_DB = "lotteryDB";
|
||||
private static final String DEFAULT_EVENTS_COLLECTION = "events";
|
||||
private static final String EMAIL = "email";
|
||||
private static final String PHONE = "phone";
|
||||
public static final String MESSAGE = "message";
|
||||
|
||||
private MongoClient mongoClient;
|
||||
private MongoDatabase database;
|
||||
@@ -107,41 +110,41 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void ticketSubmitted(PlayerDetails details) {
|
||||
var document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
document
|
||||
.put("message", "Lottery ticket was submitted and bank account was charged for 3 credits.");
|
||||
.put(MESSAGE, "Lottery ticket was submitted and bank account was charged for 3 credits.");
|
||||
eventsCollection.insertOne(document);
|
||||
stdOutEventLog.ticketSubmitted(details);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketSubmitError(PlayerDetails details) {
|
||||
var document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
document.put("message", "Lottery ticket could not be submitted because lack of funds.");
|
||||
document.put(MESSAGE, "Lottery ticket could not be submitted because lack of funds.");
|
||||
eventsCollection.insertOne(document);
|
||||
stdOutEventLog.ticketSubmitError(details);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketDidNotWin(PlayerDetails details) {
|
||||
var document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
document.put("message", "Lottery ticket was checked and unfortunately did not win this time.");
|
||||
document.put(MESSAGE, "Lottery ticket was checked and unfortunately did not win this time.");
|
||||
eventsCollection.insertOne(document);
|
||||
stdOutEventLog.ticketDidNotWin(details);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketWon(PlayerDetails details, int prizeAmount) {
|
||||
var document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
document.put("message", String
|
||||
document.put(MESSAGE, String
|
||||
.format("Lottery ticket won! The bank account was deposited with %d credits.",
|
||||
prizeAmount));
|
||||
eventsCollection.insertOne(document);
|
||||
@@ -150,10 +153,10 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void prizeError(PlayerDetails details, int prizeAmount) {
|
||||
var document = new Document("email", details.getEmail());
|
||||
document.put("phone", details.getPhoneNumber());
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
document.put("message", String
|
||||
document.put(MESSAGE, String
|
||||
.format("Lottery ticket won! Unfortunately the bank credit transfer of %d failed.",
|
||||
prizeAmount));
|
||||
eventsCollection.insertOne(document);
|
||||
|
@@ -25,13 +25,16 @@ package com.iluwatar.hexagonal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void testApp() {
|
||||
App.main(new String[]{});
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user