Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Commit 0dd0d7e

Browse files
committed
Implemented MicroServiceScope
1 parent be30550 commit 0dd0d7e

9 files changed

Lines changed: 294 additions & 7 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Some special features:
2727
- Implementations with and without restrictions
2828
- BasicScope (no restrictions)
2929
- MicroServiceScope
30-
- microService.topic.mode.scope.extra
30+
- microService.topic.mode.limitation.extra
3131
- Merging (and de-merging)
3232
- user.email.read+write+delete.all+beta
33-
- Pre-defined scopes
33+
- Pre-defined limitations
3434
- none (default)
3535
- own
3636
- self (only applicable to topics regarding a user itself)

scope/src/main/java/com/gewia/common/scope/Scope.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public Scope addScopePart(String scopePart) {
7474
*/
7575
public abstract Scope addScopePart(ScopePart scopePart);
7676

77+
/**
78+
* Adds the given <i>scopePart</i> at the given <i>index</i>.
79+
*
80+
* @param scopePart the scope part to add
81+
* @param index the index to add the scope part at
82+
*
83+
* @return this
84+
*
85+
* @since 1.0
86+
*/
87+
public abstract Scope setScopePart(ScopePart scopePart, int index);
88+
7789
/**
7890
* Removes the scope part at the given <i>index</i>.
7991
*

scope/src/main/java/com/gewia/common/scope/impl/BasicScope.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @since 1.0
1313
*/
14-
@RequiredArgsConstructor(access = AccessLevel.MODULE)
14+
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
1515
public class BasicScope extends Scope {
1616

1717
/**
@@ -52,5 +52,31 @@ public int getScopePartsSize() {
5252
return this.scopeParts.size();
5353
}
5454

55+
@Override
56+
public Scope setScopePart(ScopePart scopePart, int index) {
57+
this.scopeParts.set(index, scopePart);
58+
return this;
59+
}
60+
61+
@Override
62+
public boolean equals(Object obj) {
63+
if (obj == this) return true;
64+
if (obj == null) return false;
65+
if (!(obj instanceof Scope)) return false;
66+
67+
Scope scope = (Scope) obj;
68+
List<ScopePart> parts = scope.getScopeParts();
69+
for (int i = 0, partsSize = parts.size(); i < partsSize; i++) {
70+
ScopePart scopePart = parts.get(i);
71+
if (!scopePart.equals(this.getScopePart(i))) return false;
72+
}
73+
74+
return true;
75+
}
76+
77+
@Override
78+
public int hashCode() {
79+
return super.hashCode();
80+
}
5581

5682
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.gewia.common.scope.impl;
2+
3+
import com.gewia.common.scope.Scope;
4+
import com.gewia.common.scope.ScopePart;
5+
import com.gewia.common.scope.impl.util.Limitation;
6+
import com.gewia.common.scope.impl.util.Mode;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
* MicroServiceScope is specified by the following schema: 'microService.topic.mode.limitation.(extra)' .
12+
* Example: user.user.write.all - lets the given application/user write/create <i>all</i> users.
13+
*/
14+
public class MicroServiceScope extends BasicScope {
15+
16+
public MicroServiceScope(ScopePart microService, ScopePart topic, Mode mode, Limitation limitation, ScopePart... extra) {
17+
super();
18+
19+
this.addScopePart(microService);
20+
this.addScopePart(topic);
21+
this.addScopePart(mode.getScopePart());
22+
this.addScopePart(limitation.getScopePart());
23+
24+
for (ScopePart scopePart : extra) this.addScopePart(scopePart);
25+
}
26+
27+
@Override
28+
public Scope removeScopePart(int index) {
29+
if (index <= 3) throw new UnsupportedOperationException("MicroServiceScopes have to fit the schema.");
30+
return super.removeScopePart(index);
31+
}
32+
33+
/**
34+
* Gets the currently available scope parts.
35+
*
36+
* <p>
37+
* The returned list is an unmodifiable list, because of the restrictions of {@link MicroServiceScope}.
38+
* </p>
39+
*
40+
* @return all scope parts
41+
*
42+
* @since 1.0
43+
*/
44+
@Override
45+
public List<ScopePart> getScopeParts() {
46+
return Collections.unmodifiableList(super.getScopeParts());
47+
}
48+
49+
50+
/**
51+
* Adds the given <i>scopePart</i> at the given <i>index</i>.
52+
*
53+
* @param scopePart the scope part to add
54+
* @param index the index to add the scope part at
55+
*
56+
* @return this
57+
*
58+
* @throws IllegalArgumentException if the schema isn't fulfilled (e.g. invalid mode or limitation)
59+
*
60+
* @since 1.0
61+
*/
62+
@Override
63+
public Scope setScopePart(ScopePart scopePart, int index) {
64+
if (index == 2 && !Mode.isMode(scopePart)) throw new IllegalArgumentException("Third index has to be a valid mode.");
65+
if (index == 3 && !Limitation.isLimitation(scopePart)) throw new IllegalArgumentException("Fourth index has to be a valid limitation");
66+
67+
return this.setScopePart(scopePart, index);
68+
}
69+
70+
public ScopePart getMicroService() {
71+
return this.getScopePart(0);
72+
}
73+
74+
public ScopePart getTopic() {
75+
return this.getScopePart(1);
76+
}
77+
78+
public Mode getMode() {
79+
return Mode.find(this.getScopePart(2).getContent());
80+
}
81+
82+
public Limitation getLimitation() {
83+
return Limitation.find(this.getScopePart(3).getContent());
84+
}
85+
86+
public MicroServiceScope setMicroService(ScopePart microService) {
87+
this.setScopePart(microService, 0);
88+
return this;
89+
}
90+
91+
public MicroServiceScope setTopic(ScopePart topic) {
92+
this.setScopePart(topic, 1);
93+
return this;
94+
}
95+
96+
public MicroServiceScope setMode(Mode mode) {
97+
this.setScopePart(mode.getScopePart(), 2);
98+
return this;
99+
}
100+
101+
public MicroServiceScope setLimitation(Limitation limitation) {
102+
this.setScopePart(limitation.getScopePart(), 3);
103+
return this;
104+
}
105+
106+
}
Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,57 @@
11
package com.gewia.common.scope.impl;
22

33
import com.gewia.common.scope.Scope;
4+
import com.gewia.common.scope.ScopePart;
5+
import com.gewia.common.scope.impl.util.Limitation;
6+
import com.gewia.common.scope.impl.util.Mode;
47

58
public class ScopeFactory {
69

710
/**
8-
* Creates a {@link BasicScope} with not further restrictions.
11+
* Creates a {@link BasicScope} with no further restrictions.
912
*
10-
* @return a {@link BasicScope}
13+
* @return a new {@link BasicScope}
1114
*
1215
* @since 1.0
1316
*/
1417
public static Scope createBasicScope() {
1518
return new BasicScope();
1619
}
1720

21+
/**
22+
* Creates a {@link MicroServiceScope} with it's specified restrictions.
23+
*
24+
* @param microService the micro service the scope belongs to
25+
* @param topic the topic of the scope
26+
* @param mode the mode of the scope
27+
* @param limitation the limitation of the scope
28+
* @param extra extra information
29+
*
30+
* @return a new {@link MicroServiceScope}
31+
*
32+
* @see MicroServiceScope
33+
* @since 1.0
34+
*/
35+
public static MicroServiceScope createMicroServiceScope(String microService, String topic, Mode mode, Limitation limitation, ScopePart... extra) {
36+
return createMicroServiceScope(new ScopePart(microService), new ScopePart(topic), mode, limitation, extra);
37+
}
38+
39+
/**
40+
* Creates a {@link MicroServiceScope} with it's specified restrictions.
41+
*
42+
* @param microService the micro service the scope belongs to
43+
* @param topic the topic of the scope
44+
* @param mode the mode of the scope
45+
* @param limitation the limitation of the scope
46+
* @param extra extra information
47+
*
48+
* @return a new {@link MicroServiceScope}
49+
*
50+
* @see MicroServiceScope
51+
* @since 1.0
52+
*/
53+
public static MicroServiceScope createMicroServiceScope(ScopePart microService, ScopePart topic, Mode mode, Limitation limitation, ScopePart... extra) {
54+
return new MicroServiceScope(microService, topic, mode, limitation, extra);
55+
}
56+
1857
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.gewia.common.scope.impl.util;
2+
3+
import com.gewia.common.scope.ScopePart;
4+
import lombok.Getter;
5+
6+
public enum Limitation {
7+
8+
NONE,
9+
OWN,
10+
SELF,
11+
ALL,
12+
BETA;
13+
14+
private static final Limitation[] VALUES = Limitation.values(); // #values() always creates a new copy of the array
15+
16+
@Getter private final ScopePart scopePart = new ScopePart(this.name().toLowerCase());
17+
18+
public static Limitation find(String name) {
19+
for (Limitation limitation : VALUES)
20+
if (limitation.name().equalsIgnoreCase(name)) return limitation;
21+
22+
return null;
23+
}
24+
25+
public static boolean isLimitation(ScopePart scopePart) {
26+
return find(scopePart.getContent()) != null;
27+
}
28+
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.gewia.common.scope.impl.util;
2+
3+
import com.gewia.common.scope.ScopePart;
4+
import lombok.Getter;
5+
6+
public enum Mode {
7+
8+
READ,
9+
WRITE,
10+
DELETE;
11+
12+
private static final Mode[] VALUES = Mode.values(); // #values() always creates a new copy of the array
13+
14+
@Getter private final ScopePart scopePart = new ScopePart(this.name().toLowerCase());
15+
16+
public static Mode find(String name) {
17+
for (Mode mode : VALUES)
18+
if (mode.name().equalsIgnoreCase(name)) return mode;
19+
20+
return null;
21+
}
22+
23+
public static boolean isMode(ScopePart scopePart) {
24+
return find(scopePart.getContent()) != null;
25+
}
26+
27+
}

scope/src/test/java/com/gewia/common/scope/test/BasicScopeTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ public void basicCreateAndReadAndDeleteTest() {
1515
Assert.assertEquals("Scope parts size not zero", 0, scope.getScopePartsSize());
1616
Assert.assertEquals("Output not empty", 0, scope.toString().length());
1717

18+
scope.addScopeParts("user", "email", "read");
19+
1820
Assert.assertEquals(
19-
"Returned scope not the same",
21+
"Scope does not fulfill expectation",
2022
scope,
21-
scope.addScopeParts("user", "email", "read")
23+
ScopeFactory.createBasicScope().addScopeParts("user", "email", "read")
2224
);
2325

2426
Assert.assertEquals("Scope output doesn't match", "user.email.read", scope.toString());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.gewia.common.scope.test;
2+
3+
import com.gewia.common.scope.impl.ScopeFactory;
4+
import com.gewia.common.scope.impl.MicroServiceScope;
5+
import com.gewia.common.scope.impl.util.Limitation;
6+
import com.gewia.common.scope.impl.util.Mode;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class MicroServiceScopeTest {
11+
12+
@Test
13+
public void microServiceCreateAndReadAndDeleteTest() {
14+
MicroServiceScope scope = ScopeFactory.createMicroServiceScope(
15+
"testMicroService",
16+
"test",
17+
Mode.WRITE,
18+
Limitation.ALL
19+
);
20+
21+
Assert.assertFalse("Scope parts empty", scope.getScopeParts().isEmpty());
22+
Assert.assertEquals("Scope parts size not zero", 4, scope.getScopePartsSize());
23+
24+
Assert.assertEquals(
25+
"Scope does not fulfill expectation",
26+
scope,
27+
ScopeFactory.createBasicScope().addScopeParts("testMicroService", "test", "write", "all")
28+
);
29+
30+
Assert.assertEquals("Scope output doesn't match", "testMicroService.test.write.all", scope.toString());
31+
Assert.assertEquals("Scope parts do not match size (list access)", 4, scope.getScopeParts().size());
32+
Assert.assertEquals("Scope parts do not match size", 4, scope.getScopePartsSize());
33+
34+
Assert.assertThrows("Scope didn't catch out of bound remove", IndexOutOfBoundsException.class,() -> scope.removeScopePart(4));
35+
36+
for (int index = 0; index < 3; index++) {
37+
int finalIndex = index;
38+
Assert.assertThrows("Scope didn't catch not schema-compliant remove", UnsupportedOperationException.class,() -> scope.removeScopePart(finalIndex));
39+
}
40+
41+
Assert.assertEquals("Scope output doesn't match", "testMicroService.test.write.all", scope.toString());
42+
Assert.assertEquals("Scope parts do not match size (list access)", 4, scope.getScopeParts().size());
43+
Assert.assertEquals("Scope parts do not match size", 4, scope.getScopePartsSize());
44+
}
45+
46+
}

0 commit comments

Comments
 (0)