public MdsRestFacade getRestFacade(String entityName, String moduleName, String namespace) {
    String restId = ClassName.restId(entityName, moduleName, namespace);

    MdsRestFacade restFacade = null;
    try {
      String filter = String.format("(org.eclipse.gemini.blueprint.bean.name=%s)", restId);
      Collection<ServiceReference<MdsRestFacade>> refs =
          bundleContext.getServiceReferences(MdsRestFacade.class, filter);

      if (refs != null && refs.size() > 1 && LOGGER.isWarnEnabled()) {
        LOGGER.warn(
            "More then one Rest Facade matching for entityName={}, module={}, namespace={}. "
                + "Using first one available.",
            entityName,
            moduleName,
            namespace);
      }

      if (refs != null && refs.size() > 0) {
        ServiceReference<MdsRestFacade> ref = refs.iterator().next();
        restFacade = bundleContext.getService(ref);
      }
    } catch (InvalidSyntaxException e) {
      throw new IllegalArgumentException("Invalid Syntax for Rest Facade retrieval", e);
    }

    if (restFacade == null) {
      throw new RestNotSupportedException(entityName, moduleName, namespace);
    }

    return restFacade;
  }
Example #2
0
 private void addLookupEndpoints(SwaggerModel swaggerModel, Entity entity, Locale locale) {
   for (Lookup lookup : entity.getLookupsExposedByRest()) {
     String lookupUrl =
         ClassName.restLookupUrl(
             entity.getName(), entity.getModule(), entity.getNamespace(), lookup.getMethodName());
     swaggerModel.addPathEntry(lookupUrl, HttpMethod.GET, lookupPathEntry(entity, lookup, locale));
   }
 }
Example #3
0
  private void mockAnotherEntity() {
    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getName()).thenReturn(AnotherSample.class.getSimpleName());

    ServiceReference serviceReferenceForAnotherSample = mock(ServiceReference.class);
    when(bundleContext.getServiceReference(
            ClassName.getInterfaceName(AnotherSample.class.getName())))
        .thenReturn(serviceReferenceForAnotherSample);
    when(bundleContext.getService(serviceReferenceForAnotherSample))
        .thenReturn(serviceForAnotherSample);
  }
Example #4
0
  private void addCrudEndpoints(SwaggerModel swaggerModel, Entity entity, Locale locale) {
    final String entityPath =
        ClassName.restUrl(entity.getName(), entity.getModule(), entity.getNamespace());

    RestOptions restOptions = restOptionsOrDefault(entity);

    if (restOptions.isAllowRead()) {
      // retrieveAll and retrieveById
      swaggerModel.addPathEntry(entityPath, HttpMethod.GET, readPathEntry(entity, locale));
    }
    if (restOptions.isAllowCreate()) {
      // post new item
      swaggerModel.addPathEntry(entityPath, HttpMethod.POST, postPathEntry(entity, locale));
    }
    if (restOptions.isAllowUpdate()) {
      // update an existing item
      swaggerModel.addPathEntry(entityPath, HttpMethod.PUT, putPathEntry(entity, locale));
    }
    if (restOptions.isAllowDelete()) {
      // delete an item
      swaggerModel.addPathEntry(
          entityPath + ID_PATHVAR, HttpMethod.DELETE, deletePathEntry(entity, locale));
    }
  }
Example #5
0
 private void mockDataService(Class<?> entityClass, MotechDataService motechDataService) {
   ServiceReference serviceReference = mock(ServiceReference.class);
   when(bundleContext.getServiceReference(ClassName.getInterfaceName(entityClass.getName())))
       .thenReturn(serviceReference);
   when(bundleContext.getService(serviceReference)).thenReturn(motechDataService);
 }
Example #6
0
  @Test
  public void shouldUpdateRelatedFields() {
    TestSample test1 = new TestSample("someString", 4);
    TestSample test2 = new TestSample("otherString", 5);
    TestSample test3 = new TestSample("sample", 6);

    RelationshipsUpdate oneToOneUpdate = new RelationshipsUpdate();
    oneToOneUpdate.getAddedIds().add(6L);
    RelationshipsUpdate oneToManyUpdate = buildRelationshipUpdate();

    List<FieldRecord> fieldRecords =
        asList(
            FieldTestHelper.fieldRecord("title", String.class.getName(), "String field", "Default"),
            FieldTestHelper.fieldRecord(
                TypeDto.ONE_TO_MANY_RELATIONSHIP, "testSamples", "Related field", oneToManyUpdate),
            FieldTestHelper.fieldRecord(
                TypeDto.ONE_TO_ONE_RELATIONSHIP,
                "testSample",
                "Other Related field",
                oneToOneUpdate));

    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, fieldRecords);

    mockSampleFields();
    mockDataService();
    mockEntity();

    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getId()).thenReturn(ENTITY_ID + 1);

    ServiceReference serviceReferenceForClassWithRelatedField = mock(ServiceReference.class);
    MotechDataService serviceForClassWithRelatedField = mock(MotechDataService.class);
    when(bundleContext.getServiceReference(
            ClassName.getInterfaceName(AnotherSample.class.getName())))
        .thenReturn(serviceReferenceForClassWithRelatedField);
    when(bundleContext.getService(serviceReferenceForClassWithRelatedField))
        .thenReturn(serviceForClassWithRelatedField);
    when(motechDataService.findById(4L)).thenReturn(test1);
    when(motechDataService.findById(5L)).thenReturn(test2);
    when(motechDataService.findById(6L)).thenReturn(test3);
    when(motechDataService.findByIds(oneToManyUpdate.getAddedIds()))
        .thenReturn(Arrays.asList(test1, test2));

    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(
                    5L, "title", String.class.getName(), "String field", "Default"),
                FieldTestHelper.fieldDto(
                    6L,
                    "testSamples",
                    TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass(),
                    "Related field",
                    null)));

    ArgumentCaptor<AnotherSample> captor = ArgumentCaptor.forClass(AnotherSample.class);
    instanceService.saveInstance(entityRecord, null);

    verify(serviceForClassWithRelatedField).create(captor.capture());
    AnotherSample capturedValue = captor.getValue();
    assertEquals(capturedValue.getTestSample(), test3);
    assertEquals(capturedValue.getTestSamples().size(), 3);
    assertEquals(capturedValue.getTitle(), "Default");
    assertTrue(capturedValue.getTestSamples().contains(test1));
    assertFalse(capturedValue.getTestSamples().contains(test3));
    assertTrue(capturedValue.getTestSamples().contains(test2));
  }
Example #7
0
 private boolean isHistoricalObject(Object object) {
   return ClassName.isHistoryClassName(object.getClass().getName());
 }