public class NoSqlKeyCreatorTest {

  @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES);

  @Mock private OidMarshaller mockOidMarshaller;
  @Mock private SpecificationLoaderSpi mockSpecificationLoader;
  @Mock private ObjectSpecification mockSpecification;

  private final RootOidDefault oid = RootOidDefault.create(ObjectSpecId.of("ERP"), "3");
  private final String oidStr = oid.enString(new OidMarshaller());

  private KeyCreatorDefault keyCreatorDefault;

  @Before
  public void setUp() throws Exception {
    keyCreatorDefault =
        new KeyCreatorDefault() {
          @Override
          protected OidMarshaller getOidMarshaller() {
            return mockOidMarshaller;
          }

          @Override
          protected SpecificationLoaderSpi getSpecificationLoader() {
            return mockSpecificationLoader;
          }
        };
  }

  @Test
  public void unmarshal() throws Exception {
    context.checking(
        new Expectations() {

          {
            one(mockOidMarshaller).unmarshal(oidStr, RootOid.class);
            will(returnValue(oid));
          }
        });
    assertEquals(oid, keyCreatorDefault.unmarshal(oidStr));
  }

  @Test
  public void specification() throws Exception {
    context.checking(
        new Expectations() {
          {
            one(mockOidMarshaller).unmarshal(oidStr, TypedOid.class);
            will(returnValue(oid));
            one(mockSpecificationLoader).lookupBySpecId(oid.getObjectSpecId());
            will(returnValue(mockSpecification));
          }
        });
    final ObjectSpecification spec = keyCreatorDefault.specificationFromOidStr(oidStr);
    assertEquals(mockSpecification, spec);
  }
}
  @Test
  public void writeFields() throws Exception {
    // writer.writeObjectType(SPEC_NAME);
    writer.writeOid(RootOidDefault.deString(SPEC_NAME + ":" + "D01", new OidMarshaller()));
    writer.writeField("number", 1023);
    writer.writeField("string", "testing");
    writer.flush();

    final DBCollection instances = testDb.getCollection(SPEC_NAME);
    assertEquals(1, instances.getCount());
    final DBObject object = instances.findOne();
    assertEquals(SPEC_NAME + ":" + "D01", object.get("_oid"));
    assertEquals("1023", object.get("number"));
    assertEquals("testing", object.get("string"));
  }
  @Test
  public void serialNumberNotStored() throws Exception {
    // writer.writeId("D01");
    writer.writeOid(RootOidDefault.deString(SPEC_NAME + ":" + "D01", new OidMarshaller()));
    writer.flush();

    final DBCollection instances = testDb.getCollection(SPEC_NAME);
    assertEquals(1, instances.getCount());
    final DBObject object = instances.findOne();

    assertEquals(SPEC_NAME + ":" + "D01", object.get("_oid"));
    assertEquals("D01", object.get("_id"));

    assertEquals(2, object.keySet().size());
  }