-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an in-memory policy service (#170)
* #167 Removing S3 policy service. * #167 Updating test for outside path delete. * #167 Removing unused class. * #169 Adding in memory policy service.
- Loading branch information
1 parent
8fc8dee
commit 479614f
Showing
7 changed files
with
261 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...s-policies/src/main/java/ai/philterd/phileas/services/policies/InMemoryPolicyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2024 Philterd, LLC @ https://www.philterd.ai | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.philterd.phileas.services.policies; | ||
|
||
import ai.philterd.phileas.model.exceptions.api.BadRequestException; | ||
import ai.philterd.phileas.model.services.AbstractPolicyService; | ||
import ai.philterd.phileas.model.services.PolicyService; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class InMemoryPolicyService extends AbstractPolicyService implements PolicyService { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(InMemoryPolicyService.class); | ||
|
||
private final Map<String, String> policies; | ||
|
||
public InMemoryPolicyService() { | ||
this.policies = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public List<String> get() throws IOException { | ||
|
||
return policies.keySet().stream().toList(); | ||
|
||
} | ||
|
||
@Override | ||
public String get(String policyName) throws IOException { | ||
|
||
return policies.get(policyName); | ||
|
||
} | ||
|
||
@Override | ||
public Map<String, String> getAll() throws IOException { | ||
|
||
return policies; | ||
|
||
} | ||
|
||
@Override | ||
public void save(String policyJson) { | ||
|
||
try { | ||
|
||
final JSONObject object = new JSONObject(policyJson); | ||
final String policyName = object.getString("name"); | ||
|
||
policies.put(policyName, policyJson); | ||
|
||
} catch (JSONException ex) { | ||
|
||
LOGGER.error("The provided policy is not valid.", ex); | ||
throw new BadRequestException("The provided policy is not valid."); | ||
|
||
} | ||
|
||
} | ||
|
||
@Override | ||
public void delete(String policyName) { | ||
|
||
policies.remove(policyName); | ||
|
||
} | ||
|
||
} | ||
|
||
|
110 changes: 0 additions & 110 deletions
110
...ces-policies/src/main/java/ai/philterd/phileas/services/policies/StaticPolicyService.java
This file was deleted.
Oops, something went wrong.
151 changes: 151 additions & 0 deletions
151
...s/src/test/java/ai/philterd/test/phileas/services/policies/InMemoryPolicyServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2024 Philterd, LLC @ https://www.philterd.ai | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ai.philterd.test.phileas.services.policies; | ||
|
||
import ai.philterd.phileas.model.policy.Identifiers; | ||
import ai.philterd.phileas.model.policy.Policy; | ||
import ai.philterd.phileas.model.policy.filters.Age; | ||
import ai.philterd.phileas.model.policy.filters.CreditCard; | ||
import ai.philterd.phileas.model.policy.filters.strategies.rules.AgeFilterStrategy; | ||
import ai.philterd.phileas.model.policy.filters.strategies.rules.CreditCardFilterStrategy; | ||
import ai.philterd.phileas.model.services.PolicyService; | ||
import ai.philterd.phileas.services.policies.InMemoryPolicyService; | ||
import ai.philterd.phileas.services.policies.LocalPolicyService; | ||
import com.google.gson.Gson; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class InMemoryPolicyServiceTest { | ||
|
||
private final Gson gson = new Gson(); | ||
|
||
@Test | ||
public void list() throws IOException { | ||
|
||
final PolicyService policyService = new InMemoryPolicyService(); | ||
|
||
policyService.save(gson.toJson(getPolicy("name1"))); | ||
policyService.save(gson.toJson(getPolicy("name2"))); | ||
|
||
final List<String> names = policyService.get(); | ||
|
||
Assertions.assertEquals(2, names.size()); | ||
Assertions.assertTrue(names.contains("name1")); | ||
Assertions.assertTrue(names.contains("name2")); | ||
|
||
} | ||
|
||
@Test | ||
public void getAll() throws IOException { | ||
|
||
final PolicyService policyService = new InMemoryPolicyService(); | ||
|
||
policyService.save(gson.toJson(getPolicy("name1"))); | ||
policyService.save(gson.toJson(getPolicy("name2"))); | ||
|
||
final Map<String, String> all = policyService.getAll(); | ||
|
||
Assertions.assertEquals(2, all.size()); | ||
Assertions.assertTrue(all.containsKey("name1")); | ||
Assertions.assertTrue(all.containsKey("name2")); | ||
|
||
} | ||
|
||
@Test | ||
public void save() throws IOException { | ||
|
||
final String name = "default"; | ||
|
||
final String policy = gson.toJson(getPolicy(name)); | ||
|
||
final PolicyService policyService = new InMemoryPolicyService(); | ||
|
||
policyService.save(policy); | ||
|
||
final String saved = policyService.get("default"); | ||
|
||
Assertions.assertNotNull(saved); | ||
Assertions.assertEquals(policy, saved); | ||
|
||
} | ||
|
||
@Test | ||
public void get() throws IOException { | ||
|
||
final String name = "default"; | ||
|
||
final String policy = gson.toJson(getPolicy(name)); | ||
|
||
final PolicyService policyService = new InMemoryPolicyService(); | ||
|
||
policyService.save(policy); | ||
|
||
final String policyJson = policyService.get(name); | ||
|
||
Assertions.assertEquals(policy, policyJson); | ||
|
||
} | ||
|
||
@Test | ||
public void delete() throws IOException { | ||
|
||
final String name = "default"; | ||
final String policy = gson.toJson(getPolicy(name)); | ||
|
||
final PolicyService policyService = new InMemoryPolicyService(); | ||
|
||
policyService.save(policy); | ||
|
||
policyService.delete(name); | ||
|
||
Assertions.assertFalse(policyService.getAll().containsKey(name)); | ||
|
||
} | ||
|
||
private Policy getPolicy(String name) { | ||
|
||
AgeFilterStrategy ageFilterStrategy = new AgeFilterStrategy(); | ||
|
||
Age age = new Age(); | ||
age.setAgeFilterStrategies(List.of(ageFilterStrategy)); | ||
|
||
CreditCardFilterStrategy creditCardFilterStrategy = new CreditCardFilterStrategy(); | ||
|
||
CreditCard creditCard = new CreditCard(); | ||
creditCard.setCreditCardFilterStrategies(List.of(creditCardFilterStrategy)); | ||
|
||
Identifiers identifiers = new Identifiers(); | ||
|
||
identifiers.setAge(age); | ||
|
||
Policy policy = new Policy(); | ||
policy.setName(name); | ||
policy.setIdentifiers(identifiers); | ||
|
||
return policy; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.