@Test
 public void testCreateInstance_success_shouldStoreInstanceDataInBrokerStore() throws Exception {
   ServiceInstance instance = getServiceInstance("instanceId2", "fake-bare-plan");
   CreateServiceInstanceRequest request = getCreateInstanceRequest(instance);
   instanceService.createServiceInstance(request);
   ServiceInstance serviceInstance = instanceService.getServiceInstance("instanceId2");
   assertThat(request.getServiceInstanceId(), equalTo(serviceInstance.getServiceInstanceId()));
   // todo: compare all of the fields
 }
  @Test
  public void duplicateServiceInstanceCreationFails() throws Exception {
    ServiceInstance instance = ServiceInstanceFixture.getServiceInstance();

    String url = ServiceInstanceController.BASE_PATH + "/" + instance.getId();
    String body = ServiceInstanceFixture.getCreateServiceInstanceRequestJson();

    serviceInstanceService.createServiceInstance(
        ServiceFixture.getService(),
        instance.getId(),
        instance.getPlanId(),
        instance.getOrganizationGuid(),
        instance.getSpaceGuid());

    mockMvc
        .perform(
            put(url)
                .contentType(MediaType.APPLICATION_JSON)
                .content(body)
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isConflict());

    serviceInstanceService.deleteServiceInstance(
        instance.getId(), instance.getServiceDefinitionId(), instance.getPlanId());
  }
  @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());
  }
  @Test
  public void deleteUnknownServiceInstanceFails() throws Exception {
    ServiceInstance instance = ServiceInstanceFixture.getServiceInstanceTwo();

    String url =
        ServiceInstanceController.BASE_PATH
            + "/"
            + instance.getId()
            + "?service_id="
            + instance.getServiceDefinitionId()
            + "&plan_id="
            + instance.getPlanId();

    mockMvc
        .perform(delete(url).accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isNotFound());
  }
  @Test
  public void serviceInstanceIsCreatedCorrectly() throws Exception {
    ServiceInstance instance = ServiceInstanceFixture.getServiceInstance();

    String url = ServiceInstanceController.BASE_PATH + "/" + instance.getId();
    String body = ServiceInstanceFixture.getCreateServiceInstanceRequestJson();

    mockMvc
        .perform(
            put(url)
                .contentType(MediaType.APPLICATION_JSON)
                .content(body)
                .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isCreated())
        .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON));

    serviceInstanceService.deleteServiceInstance(
        instance.getId(), instance.getServiceDefinitionId(), instance.getPlanId());
  }
  @Test(expected = ServiceInstanceBindingExistsException.class)
  public void serviceInstanceCreationFailsWithExistingInstance()
      throws ServiceBrokerException, ServiceInstanceBindingExistsException {

    when(bindingRepository.findOne(anyString()))
        .thenReturn(ServiceInstanceBindingFixture.getServiceInstanceBinding());
    when(admin.getApplicationSensors(anyString()))
        .thenReturn(new AsyncResult<>(Collections.<String, Object>emptyMap()));
    CreateServiceInstanceBindingRequest request =
        new CreateServiceInstanceBindingRequest(
            serviceInstance.getServiceDefinitionId(), "planId", "appGuid");

    bindingService.createServiceInstanceBinding(request.withBindingId(SVC_INST_BIND_ID));
    bindingService.createServiceInstanceBinding(request.withBindingId(SVC_INST_BIND_ID));
  }
  @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 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());
  }