Skip to content

Instantly share code, notes, and snippets.

@punchouty
Last active May 21, 2021 11:03
Show Gist options
  • Save punchouty/fc2e58bf974baf0a2d709295201de13e to your computer and use it in GitHub Desktop.
Save punchouty/fc2e58bf974baf0a2d709295201de13e to your computer and use it in GitHub Desktop.
Symbolic with use of Collection

Collection

Identity for collection colors

BrainCollectionConfig

{
   "id" : 123456,
   "key" : {
      "vertical" : "common",
      "type" : "collection",
      "name" : "colors",
      "key" : "/common/collection/colors"
   },
   "item_list" : [
      {
         "id" : 23456,
         "key" : {
            "vertical" : "common",
            "type" : "collection_item",
            "name" : "red",
            "key" : "/common/collection_item/colors/red"
         }
      },
      {
         "id" : 34567,
         "key" : {
            "vertical" : "common",
            "type" : "collection_item",
            "name" : "blue",
            "key" : "/common/collection_item/colors/blue"
         }
      },
      {
         "id" : 45678,
         "key" : {
            "vertical" : "common",
            "type" : "collection_item",
            "name" : "green",
            "key" : "/common/collection_item/colors/green"
         }
      }
   ]
}

BrainSymbolicQuantity

for item blue

{
   "token" : {
      "index" : 34567
   }
}

BrainQuantityConfig

{
   "identity" : {
      "id" : 54321,
      "key" : {
         "vertical" : "common",
         "type" : "quantity",
         "name" : "colors",
         "key" : "/common/quantity/primary_colors"
      },
      "symbolic" : {
         "collection" : "/common/collection/colors"
      }
   }
}

BrainQuantitySchema

{
   "identity" : {
      "id" : 54321,
      "key" : {
         "vertical" : "common",
         "type" : "quantity",
         "name" : "colors",
         "key" : "/common/quantity/primary_colors"
      },
      "symbolic" : {
         "collection" : {
            "item" : {
               "item_map" : {
                  "item2index" : {
                     "red" : 23456,
                     "blue" : 34567,
                     "green" : 45678
                  },
                  "index2item" : {
                     "23456" : "red",
                     "34567" : "blue",
                     "45678" : "green"
                  }
               }
            }
         }
      }
   }
}

Implementation Class

public class SymbolicQuantityServiceImpl implements SymbolicQuantityService {
   @Override
   public BrainQuantity createQuantity(BrainSymbolicQuantitySchema schema, String item) {
      BrainCollectionSchema collection = schema.getCollection();
      try {
         if (collection.getItem().getItemMap().getItem2IndexMap().get(item) >= 0) {
            BrainToken token = BrainToken.newBuilder().setIndex(collection.getItem().getItemMap().getItem2IndexMap().get(item)).build();
            BrainSymbolicQuantity brainSymbolicQuantity = BrainSymbolicQuantity.newBuilder().setToken(token).build();
            return BrainQuantity.newBuilder().setSymbolic(brainSymbolicQuantity).build();
         }
      } catch (NullPointerException n) {
         throw new QuantityException(QuantityException.QuantityExceptionCode.DATA_NOT_PRESENT, "Data Not Present");
      }
      return null;
   }

}

Test Case

public class SymbolicQuantityServiceImplTest {
    private SymbolicQuantityService quantityService = new SymbolicQuantityServiceImpl();
    private BrainSymbolicQuantitySchema brainSymbolicQuantitySchema;

    public void setUp() {
        BrainItemMap brainItemMap = BrainItemMap.newBuilder().
                putItem2Index("red", 23456).putItem2Index("green", 34567).putItem2Index("blue", 45678).
                putIndex2Item(23456, "red").putIndex2Item(34567, "green").putIndex2Item(45678, "blue").build();
        BrainItemCollectionSchema brainItemCollectionSchema = BrainItemCollectionSchema.
                newBuilder().setItemMap(brainItemMap).build();
        BrainCollectionSchema brainCollectionSchema = BrainCollectionSchema.
                newBuilder().setItem(brainItemCollectionSchema).build();
        brainSymbolicQuantitySchema = BrainSymbolicQuantitySchema.
                newBuilder().setCollection(brainCollectionSchema).build();
    }

    @Test
    void createQuantitySymbolicSuccess() {
        setUp();
        BrainQuantity quantity = quantityService.createQuantity(brainSymbolicQuantitySchema, "RED");
        Assertions.assertEquals(23456, quantity.getSymbolic().getToken().getIndex());
    }

    @Test
    void createQuantitySymbolicFailure() {
        setUp();
        QuantityException exception = Assertions.assertThrows(QuantityException.class, () -> {
            BrainQuantity quantity = quantityService.createQuantity(brainSymbolicQuantitySchema, "red");
        });
        Assertions.assertEquals("Data Not Present", exception.getMessage());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment