3838import org .assertj .core .api .AtomicIntegerArrayAssert ;
3939import org .junit .jupiter .api .BeforeAll ;
4040import org .junit .jupiter .api .Test ;
41+ import org .junit .platform .commons .JUnitException ;
4142import org .mockito .Mockito ;
4243
4344import java .util .ArrayList ;
@@ -532,7 +533,7 @@ void testUsingAttributeMappedAtNamespace() {
532533
533534 @ Test
534535 void testUsingAttributeMappedAtMultiplePlaces () {
535- var attributes = new Value [] { mockValueFor (mp2uns2uns ), mockValueFor (mp2uns2mp ) };
536+ var attributes = new Value []{ mockValueFor (mp2uns2uns ), mockValueFor (mp2uns2mp )};
536537 Granter granter = Autoconfigure .newGranterFromAttributes (new KASKeyCache (), attributes );
537538 var counter = new AtomicInteger (0 );
538539 var splitPlan = granter .getSplits (Collections .emptyList (), () -> Integer .toString (counter .getAndIncrement ()), () -> Optional .empty ());
@@ -756,7 +757,7 @@ private static class ReasonerTestCase {
756757 private final List <KeySplitStep > plan ;
757758
758759 ReasonerTestCase (String name , List <AttributeValueFQN > policy , List <String > defaults , String ats , String keyed ,
759- String reduced , List <KeySplitStep > plan ) {
760+ String reduced , List <KeySplitStep > plan ) {
760761 this .name = name ;
761762 this .policy = policy ;
762763 this .defaults = defaults ;
@@ -809,8 +810,8 @@ public List<KeySplitStep> getPlan() {
809810 void testStoreKeysToCache_NoKeys () {
810811 KASKeyCache keyCache = Mockito .mock (KASKeyCache .class );
811812 KeyAccessServer kas1 = KeyAccessServer .newBuilder ().setPublicKey (
812- PublicKey .newBuilder ().setCached (
813- KasPublicKeySet .newBuilder ()))
813+ PublicKey .newBuilder ().setCached (
814+ KasPublicKeySet .newBuilder ()))
814815 .build ();
815816
816817 Autoconfigure .storeKeysToCache (List .of (kas1 ), Collections .emptyList (), keyCache );
@@ -906,7 +907,7 @@ void testStoreKeysToCache_MultipleKasEntries() {
906907 }
907908
908909 GetAttributeValuesByFqnsResponse getResponseWithGrants (GetAttributeValuesByFqnsRequest req ,
909- List <KeyAccessServer > grants ) {
910+ List <KeyAccessServer > grants ) {
910911 GetAttributeValuesByFqnsResponse .Builder builder = GetAttributeValuesByFqnsResponse .newBuilder ();
911912
912913 for (String v : req .getFqnsList ()) {
@@ -958,13 +959,15 @@ void testKeyCacheFromGrants() {
958959
959960 AttributesServiceClient attributesServiceClient = mock (AttributesServiceClient .class );
960961 when (attributesServiceClient .getAttributeValuesByFqnsBlocking (any (), any ())).thenAnswer (invocation -> {
961- var request = (GetAttributeValuesByFqnsRequest )invocation .getArgument (0 );
962- return new UnaryBlockingCall <GetAttributeValuesByFqnsResponse >(){
962+ var request = (GetAttributeValuesByFqnsRequest ) invocation .getArgument (0 );
963+ return new UnaryBlockingCall <GetAttributeValuesByFqnsResponse >() {
963964 @ Override
964965 public ResponseMessage <GetAttributeValuesByFqnsResponse > execute () {
965966 return new ResponseMessage .Success <>(getResponseWithGrants (request , List .of (kas1 )), Collections .emptyMap (), Collections .emptyMap ());
966967 }
967- @ Override public void cancel () {
968+
969+ @ Override
970+ public void cancel () {
968971 // not really calling anything
969972 }
970973 };
@@ -996,17 +999,19 @@ public ResponseMessage<GetAttributeValuesByFqnsResponse> execute() {
996999 void testUsingBaseKeyWhenNoMappedKeysOrGrants () {
9971000 Autoconfigure .Granter granter = Autoconfigure .newGranterFromAttributes (null );
9981001 SimpleKasKey key = SimpleKasKey .newBuilder ()
999- .setKasUri ("https://example.com/kas" )
1000- .setPublicKey (
1001- SimpleKasPublicKey .newBuilder ()
1002- .setKid ("thenewkid" )
1003- .setPem ("anotherpem" )
1004- .setAlgorithm (Algorithm .ALGORITHM_EC_P521 )
1005- ).build ();
1002+ .setKasUri ("https://example.com/kas" )
1003+ .setPublicKey (
1004+ SimpleKasPublicKey .newBuilder ()
1005+ .setKid ("thenewkid" )
1006+ .setPem ("anotherpem" )
1007+ .setAlgorithm (Algorithm .ALGORITHM_EC_P521 )
1008+ ).build ();
10061009
10071010 var splits = granter .getSplits (
10081011 List .of ("https://example.org/kas2" ),
1009- () -> { throw new IllegalStateException ("the plan should have a single element" ); },
1012+ () -> {
1013+ throw new IllegalStateException ("the plan should have a single element" );
1014+ },
10101015 () -> Optional .of (key ));
10111016 assertThat (splits ).hasSize (1 );
10121017 assertThat (splits .get (0 )).isEqualTo (new KeySplitStep ("https://example.com/kas" , "" , "thenewkid" ));
@@ -1029,4 +1034,85 @@ void testUsingDefaultKasesWhenNothingElseProvided() {
10291034 new KeySplitStep ("https://example.org/kas2" , "1" , null )
10301035 );
10311036 }
1037+
1038+ @ Test
1039+ void createsGranterFromAttributeValues () {
1040+ // Arrange
1041+ Config .TDFConfig config = new Config .TDFConfig ();
1042+ config .attributeValues = List .of (mockValueFor (spk2spk ), mockValueFor (rel2gbr ));
1043+
1044+ SDK .Services services = mock (SDK .Services .class );
1045+ SDK .KAS kas = mock (SDK .KAS .class );
1046+ when (services .kas ()).thenReturn (kas );
1047+ when (services .attributes ()).thenThrow (new IllegalStateException ("should never use the attribute service when attributes are provided" ));
1048+ when (kas .getKeyCache ()).thenReturn (null ); // No cache needed for this test
1049+
1050+ // Act
1051+ Autoconfigure .Granter granter = Autoconfigure .createGranter (services , config );
1052+
1053+ // Assert
1054+ assertThat (granter ).isNotNull ();
1055+ assertThat (granter .getPolicy ()).hasSize (2 );
1056+ assertThat (granter .getPolicy ()).containsExactlyInAnyOrder (
1057+ new AttributeValueFQN ("https://other.com/attr/specified/value/specked" ),
1058+ new AttributeValueFQN ("https://virtru.com/attr/Releasable%20To/value/GBR" )
1059+ );
1060+ }
1061+
1062+ @ Test
1063+ void createsGranterFromService () {
1064+ // Arrange
1065+ SDK .Services services = mock (SDK .Services .class );
1066+ SDK .KAS kas = mock (SDK .KAS .class );
1067+ AttributesServiceClient attributesServiceClient = mock (AttributesServiceClient .class );
1068+
1069+ // Prepare a request and a mocked response
1070+ List <AttributeValueFQN > policy = List .of (
1071+ new AttributeValueFQN ("https://other.com/attr/specified/value/specked" ),
1072+ new AttributeValueFQN ("https://virtru.com/attr/Releasable%20To/value/GBR" )
1073+ );
1074+ // GetAttributeValuesByFqnsRequest request = GetAttributeValuesByFqnsRequest.newBuilder()
1075+ // .addAllFqns(policy.stream().map(AttributeValueFQN::toString).collect(Collectors.toList()))
1076+ // .build();
1077+
1078+ when (services .kas ()).thenReturn (kas );
1079+ when (services .attributes ()).thenReturn (attributesServiceClient );
1080+
1081+ // Mock the attribute service to return a response with the expected values
1082+ when (attributesServiceClient .getAttributeValuesByFqnsBlocking (any (), any ())).thenAnswer (invocation -> {
1083+ return new UnaryBlockingCall <GetAttributeValuesByFqnsResponse >() {
1084+ @ Override
1085+ public ResponseMessage <GetAttributeValuesByFqnsResponse > execute () {
1086+ GetAttributeValuesByFqnsResponse .Builder builder = GetAttributeValuesByFqnsResponse .newBuilder ();
1087+ for (AttributeValueFQN fqn : policy ) {
1088+ Value value = Value .newBuilder ()
1089+ .setId (fqn .toString ())
1090+ .setFqn (fqn .toString ())
1091+ .build ();
1092+ builder .putFqnAttributeValues (fqn .toString (),
1093+ GetAttributeValuesByFqnsResponse .AttributeAndValue .newBuilder ()
1094+ .setValue (value )
1095+ .build ());
1096+ }
1097+ return new ResponseMessage .Success <>(builder .build (), Collections .emptyMap (), Collections .emptyMap ());
1098+ }
1099+
1100+ @ Override
1101+ public void cancel () {
1102+ }
1103+ };
1104+ });
1105+
1106+ // Act
1107+ Autoconfigure .Granter granter = Autoconfigure .createGranter (services , new Config .TDFConfig () {{
1108+ attributeValues = null ; // force use of service
1109+ attributes = policy ;
1110+ }});
1111+
1112+ // Assert
1113+ assertThat (granter ).isNotNull ();
1114+ // The policy should be empty because attributeValues is null, but the test ensures the service is called
1115+ // If you want to check the service call, verify it:
1116+ verify (services ).attributes ();
1117+ }
10321118}
0 commit comments