MongoDetailsAdapter { public static RyaDetails toRyaDetails(final Document doc) throws MalformedRyaDetailsException { requireNonNull(doc); try { final RyaDetails.Builder builder = RyaDetails.builder() .setRyaInstanceName(doc.getString(INSTANCE_KEY)) .setRyaVersion(doc.getString(VERSION_KEY)) .setEntityCentricIndexDetails(new EntityCentricIndexDetails(doc.getBoolean(ENTITY_DETAILS_KEY))) .setPCJIndexDetails(getPCJIndexDetails(doc)) .setTemporalIndexDetails(new TemporalIndexDetails(doc.getBoolean(TEMPORAL_DETAILS_KEY))) .setFreeTextDetails(new FreeTextIndexDetails(doc.getBoolean(FREETEXT_DETAILS_KEY))) .setProspectorDetails(new ProspectorDetails(Optional.<Date>fromNullable(doc.getDate(PROSPECTOR_DETAILS_KEY)))) .setJoinSelectivityDetails(new JoinSelectivityDetails(Optional.<Date>fromNullable(doc.getDate(JOIN_SELECTIVITY_DETAILS_KEY)))); if(doc.containsKey(RYA_STREAMS_DETAILS_KEY)) { final Document streamsObject = doc.get(RYA_STREAMS_DETAILS_KEY, Document.class); final String hostname = streamsObject.getString(RYA_STREAMS_HOSTNAME_KEY); final int port = streamsObject.getInteger(RYA_STREAMS_PORT_KEY); builder.setRyaStreamsDetails(new RyaStreamsDetails(hostname, port)); } return builder.build(); } catch(final Exception e) { throw new MalformedRyaDetailsException("Failed to make RyaDetail from Mongo Object, it is malformed.", e); } }  static Document toDocument(final RyaDetails details); static RyaDetails toRyaDetails(final Document doc); static final String INSTANCE_KEY; static final String VERSION_KEY; static final String ENTITY_DETAILS_KEY; static final String GEO_DETAILS_KEY; static final String PCJ_DETAILS_KEY; static final String PCJ_ENABLED_KEY; static final String PCJ_FLUO_KEY; static final String PCJ_PCJS_KEY; static final String PCJ_ID_KEY; static final String PCJ_UPDATE_STRAT_KEY; static final String PCJ_LAST_UPDATE_KEY; static final String TEMPORAL_DETAILS_KEY; static final String FREETEXT_DETAILS_KEY; static final String PROSPECTOR_DETAILS_KEY; static final String JOIN_SELECTIVITY_DETAILS_KEY; static final String RYA_STREAMS_DETAILS_KEY; static final String RYA_STREAMS_HOSTNAME_KEY; static final String RYA_STREAMS_PORT_KEY; }
@Test public void mongoToRyaDetailsTest() throws MalformedRyaDetailsException { final Document mongo = Document.parse( "{ " + "instanceName : \"test\"," + "version : \"1\"," + "entityCentricDetails : true," + "pcjDetails : {" + "enabled : true ," + "fluoName : \"fluo\"," + "pcjs : [ " + "{" + "id : \"pcj_0\"," + "updateStrategy : \"BATCH\"," + "lastUpdate : { $date : \"1970-01-01T00:00:00.000Z\"}" + "}," + "{" + "id : \"pcj_1\"," + "updateStrategy : \"BATCH\"," + "lastUpdate : { $date : \"1970-01-01T00:00:00.001Z\"}" + "}]" + "}," + "temporalDetails : true," + "freeTextDetails : true," + "prospectorDetails : { $date : \"1970-01-01T00:00:00.000Z\"}," + "joinSelectivitiyDetails : { $date : \"1970-01-01T00:00:00.001Z\"}," + "ryaStreamsDetails : { hostname : \"localhost\" , port : 6}" + "}" ); final RyaDetails actual = MongoDetailsAdapter.toRyaDetails(mongo); final RyaDetails expected = RyaDetails.builder() .setRyaInstanceName("test") .setRyaVersion("1") .setEntityCentricIndexDetails(new EntityCentricIndexDetails(true)) .setPCJIndexDetails( PCJIndexDetails.builder() .setEnabled(true) .addPCJDetails( PCJDetails.builder() .setId("pcj_0") .setUpdateStrategy(PCJUpdateStrategy.BATCH) .setLastUpdateTime(new Date(0L))) .addPCJDetails( PCJDetails.builder() .setId("pcj_1") .setUpdateStrategy(PCJUpdateStrategy.BATCH) .setLastUpdateTime(new Date(1L)))) .setTemporalIndexDetails(new TemporalIndexDetails(true)) .setFreeTextDetails(new FreeTextIndexDetails(true)) .setProspectorDetails(new ProspectorDetails(Optional.<Date>fromNullable(new Date(0L)))) .setJoinSelectivityDetails(new JoinSelectivityDetails(Optional.<Date>fromNullable(new Date(1L)))) .setRyaStreamsDetails(new RyaStreamsDetails("localhost", 6)) .build(); assertEquals(expected, actual); }
@Test public void absentOptionalToRyaDetailsTest() throws MalformedRyaDetailsException { final Document mongo = Document.parse( "{ " + "instanceName : \"test\"," + "version : \"1\"," + "entityCentricDetails : true," + "pcjDetails : {" + "enabled : false," + "pcjs : [ " + "{" + "id : \"pcj_1\"," + "}" + "]" + "}," + "temporalDetails : false," + "freeTextDetails : true," + "prospectorDetails : null," + "joinSelectivitiyDetails : null" + "}" ); final RyaDetails actual = MongoDetailsAdapter.toRyaDetails(mongo); final RyaDetails expected = RyaDetails.builder() .setRyaInstanceName("test") .setRyaVersion("1") .setEntityCentricIndexDetails(new EntityCentricIndexDetails(true)) .setPCJIndexDetails( PCJIndexDetails.builder() .setEnabled(false)) .setTemporalIndexDetails(new TemporalIndexDetails(false)) .setFreeTextDetails(new FreeTextIndexDetails(true)) .setProspectorDetails(new ProspectorDetails(Optional.<Date>absent())) .setJoinSelectivityDetails(new JoinSelectivityDetails(Optional.<Date>absent())) .build(); assertEquals(expected, actual); }