@Test
  public void newServiceInstanceBindingCreatedSuccessfully()
      throws ServiceBrokerException, ServiceInstanceBindingExistsException {

    when(admin.getCredentialsFromSensors(
            anyString(),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class)))
        .thenReturn(new AsyncResult<>(Collections.<String, Object>emptyMap()));
    when(admin.hasEffector(anyString(), anyString(), anyString()))
        .thenReturn(new AsyncResult<>(false));
    when(instanceRepository.findOne(anyString(), anyBoolean())).thenReturn(serviceInstance);
    when(serviceDefinition.getMetadata()).thenReturn(ImmutableMap.of());
    when(brooklynCatalogService.getServiceDefinition(anyString())).thenReturn(serviceDefinition);
    CreateServiceInstanceBindingRequest request =
        new CreateServiceInstanceBindingRequest(
            serviceInstance.getServiceDefinitionId(), "planId", "appGuid");
    ServiceInstanceBinding binding =
        bindingService.createServiceInstanceBinding(request.withBindingId(SVC_INST_BIND_ID));

    assertNotNull(binding);
    assertEquals(SVC_INST_BIND_ID, binding.getId());
  }
 @RequestMapping(value = BASE_PATH + "/{bindingId}", method = RequestMethod.DELETE)
 public ResponseEntity<String> deleteServiceInstanceBinding(
     @PathVariable("instanceId") String instanceId,
     @PathVariable("bindingId") String bindingId,
     @RequestParam("service_id") String serviceId,
     @RequestParam("plan_id") String planId)
     throws ServiceBrokerException {
   logger.debug(
       "DELETE: "
           + BASE_PATH
           + "/{bindingId}"
           + ", deleteServiceInstanceBinding(),  serviceInstance.id = "
           + instanceId
           + ", bindingId = "
           + bindingId
           + ", serviceId = "
           + serviceId
           + ", planId = "
           + planId);
   ServiceInstanceBinding binding =
       serviceInstanceBindingService.deleteServiceInstanceBinding(bindingId);
   if (binding == null) {
     return new ResponseEntity<String>("{}", HttpStatus.NOT_FOUND);
   }
   logger.debug("ServiceInstanceBinding Deleted: " + binding.getId());
   return new ResponseEntity<String>("{}", HttpStatus.OK);
 }
 @RequestMapping(value = BASE_PATH + "/{bindingId}", method = RequestMethod.PUT)
 public ResponseEntity<ServiceInstanceBindingResponse> bindServiceInstance(
     @PathVariable("instanceId") String instanceId,
     @PathVariable("bindingId") String bindingId,
     @Valid @RequestBody ServiceInstanceBindingRequest request)
     throws ServiceInstanceDoesNotExistException, ServiceInstanceBindingExistsException,
         ServiceBrokerException {
   logger.debug(
       "PUT: "
           + BASE_PATH
           + "/{bindingId}"
           + ", bindServiceInstance(), serviceInstance.id = "
           + instanceId
           + ", bindingId = "
           + bindingId);
   ServiceInstance instance = serviceInstanceService.getServiceInstance(instanceId);
   if (instance == null) {
     throw new ServiceInstanceDoesNotExistException(instanceId);
   }
   ServiceInstanceBinding binding =
       serviceInstanceBindingService.createServiceInstanceBinding(
           bindingId,
           instance,
           request.getServiceDefinitionId(),
           request.getPlanId(),
           request.getAppGuid());
   logger.debug("ServiceInstanceBinding Created: " + binding.getId());
   return new ResponseEntity<ServiceInstanceBindingResponse>(
       new ServiceInstanceBindingResponse(binding), HttpStatus.CREATED);
 }
  @Test
  public void serviceInstanceBindingRetrievedSuccessfully()
      throws ServiceBrokerException, ServiceInstanceBindingExistsException {

    ServiceInstanceBinding binding = ServiceInstanceBindingFixture.getServiceInstanceBinding();
    when(bindingRepository.findOne(anyString())).thenReturn(binding);

    assertEquals(
        binding.getId(), bindingService.getServiceInstanceBinding(binding.getId()).getId());
  }
  @Test
  public void serviceInstanceBindingDeletedSuccessfully()
      throws ServiceBrokerException, ServiceInstanceBindingExistsException {

    ServiceInstanceBinding binding = ServiceInstanceBindingFixture.getServiceInstanceBinding();
    when(bindingRepository.findOne(anyString())).thenReturn(binding);
    when(instanceRepository.findOne(anyString(), any(Boolean.class))).thenReturn(serviceInstance);
    when(serviceInstance.getServiceDefinitionId()).thenReturn(SVC_DEFN_ID);
    when(admin.invokeEffector(anyString(), anyString(), anyString(), anyString(), anyMap()))
        .thenReturn(new AsyncResult<String>("effector called"));

    DeleteServiceInstanceBindingRequest request =
        new DeleteServiceInstanceBindingRequest(
            binding.getId(), serviceInstance, "serviceId", "planId");
    assertNotNull(bindingService.deleteServiceInstanceBinding(request));
  }
  @Test
  public void
      testCreateServiceInstanceBinding_success_saveAndReturnsNewServiceInstanceBindingWithCredentials()
          throws Exception {

    CreateServiceInstanceBindingRequest request =
        getCreateServiceInstanceBindingRequest("serviceId", "bindingId");
    when(instanceBindingService.createServiceInstanceBinding(request))
        .thenReturn(getServiceInstanceBinding("id"));
    ServiceInstanceBinding instance = service.createServiceInstanceBinding(request);

    assertThat(instance.getCredentials().get("junit"), equalTo(CREDENTIALS.get("junit")));
    assertThat(
        instance.getCredentials().get(HBaseServiceInstanceBindingService.NAMESPACE),
        equalTo(Hashing.sha1().hashString("serviceId").toString()));
  }
  @Test
  public void newServiceInstanceBindingCreatedSuccessfullyWithBindEffector()
      throws ServiceBrokerException, ServiceInstanceBindingExistsException, PollingException {
    when(admin.getRestApi()).thenReturn(brooklynApi);
    when(admin.getCredentialsFromSensors(
            anyString(),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class)))
        .thenReturn(new AsyncResult<>(Collections.<String, Object>emptyMap()));
    when(admin.hasEffector(anyString(), anyString(), anyString()))
        .thenReturn(new AsyncResult<>(true));
    when(admin.invokeEffector(anyString(), anyString(), anyString(), anyString(), anyMap()))
        .thenReturn(new AsyncResult<>(TASK_RESPONSE_INCOMPLETE));
    when(brooklynApi.getActivityApi()).thenReturn(activityApi);
    when(activityApi.get(anyString()))
        .thenReturn(TASK_SUMMARY_INCOMPLETE)
        .thenReturn(TASK_SUMMARY_INCOMPLETE)
        .thenReturn(TASK_SUMMARY_INCOMPLETE)
        .thenReturn(TASK_SUMMARY_INCOMPLETE)
        .thenReturn(TASK_SUMMARY_COMPLETE);
    doCallRealMethod().when(admin).blockUntilTaskCompletes(anyString());
    doCallRealMethod().when(admin).blockUntilTaskCompletes(anyString(), any(Duration.class));
    when(instanceRepository.findOne(anyString(), anyBoolean())).thenReturn(serviceInstance);
    when(serviceDefinition.getMetadata()).thenReturn(ImmutableMap.of());
    when(brooklynCatalogService.getServiceDefinition(anyString())).thenReturn(serviceDefinition);
    CreateServiceInstanceBindingRequest request =
        new CreateServiceInstanceBindingRequest(
            serviceInstance.getServiceDefinitionId(), "planId", "appGuid");
    ServiceInstanceBinding binding =
        bindingService.createServiceInstanceBinding(request.withBindingId(SVC_INST_BIND_ID));

    assertNotNull(binding);
    assertEquals(SVC_INST_BIND_ID, binding.getId());
  }
  @Test
  public void testWhitelistCreatedSuccessfully()
      throws ServiceInstanceBindingExistsException, ServiceBrokerException {

    bindingService =
        new BrooklynServiceInstanceBindingService(
            new BrooklynRestAdmin(brooklynApi, httpClient, config),
            bindingRepository,
            instanceRepository,
            brooklynCatalogService);

    when(admin.getCredentialsFromSensors(
            anyString(),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class),
            any(Predicate.class)))
        .thenCallRealMethod();

    when(brooklynApi.getSensorApi()).thenReturn(sensorApi);
    when(sensorApi.list(anyString(), anyString()))
        .thenReturn(
            ImmutableList.of(
                new SensorSummary(
                    "my.sensor", "my sensor type", "my sensor description", ImmutableMap.of()),
                new SensorSummary(
                    "sensor.one.name",
                    "sensor one type",
                    "sensor one description",
                    ImmutableMap.of())));
    when(brooklynApi.getEntityApi()).thenReturn(entityApi);
    when(entityApi.list(any()))
        .thenReturn(
            ImmutableList.of(
                new EntitySummary(
                    "entityId", "name", "entityType", "catalogItemId", ImmutableMap.of())));
    when(instanceRepository.findOne(anyString(), anyBoolean())).thenReturn(serviceInstance);
    when(brooklynCatalogService.getServiceDefinition(Mockito.anyString()))
        .thenReturn(serviceDefinition);
    when(serviceInstance.getServiceDefinitionId()).thenReturn(SVC_DEFN_ID);
    when(serviceDefinition.getMetadata()).thenReturn(ImmutableMap.of("planYaml", WHITELIST_YAML));
    when(brooklynApi.getEffectorApi()).thenReturn(effectorApi);
    when(effectorApi.invoke(anyString(), anyString(), anyString(), anyString(), anyMap()))
        .thenReturn(bindEffectorResponse);

    CreateServiceInstanceBindingRequest request =
        new CreateServiceInstanceBindingRequest(
            serviceInstance.getServiceDefinitionId(), "planId", "appGuid");
    ServiceInstanceBinding binding =
        bindingService.createServiceInstanceBinding(request.withBindingId(SVC_INST_BIND_ID));

    ServiceInstanceBinding expectedBinding =
        new ServiceInstanceBinding(
            SVC_INST_BIND_ID,
            serviceInstance.getServiceInstanceId(),
            EXPECTED_CREDENTIALS,
            null,
            "appGuid");

    assertEquals(expectedBinding.getAppGuid(), binding.getAppGuid());
    assertEquals(expectedBinding.getCredentials(), binding.getCredentials());
    assertEquals(expectedBinding.getId(), binding.getId());
    assertEquals(expectedBinding.getServiceInstanceId(), binding.getServiceInstanceId());
    assertEquals(expectedBinding.getSyslogDrainUrl(), binding.getSyslogDrainUrl());
  }