Search in sources :

Example 21 with ProvisionerInfo

use of io.cdap.cdap.proto.provisioner.ProvisionerInfo in project cdap by cdapio.

the class ProfileServiceTest method testProfileUpdateDisallowed.

@Test(expected = MethodNotAllowedException.class)
public void testProfileUpdateDisallowed() throws Exception {
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    profileService.saveProfile(profileId, profile);
    CConfiguration cConfWithProfileCreationDisabled = CConfiguration.copy(cConf);
    cConfWithProfileCreationDisabled.setBoolean(Constants.Profile.UPDATE_ALLOWED, false);
    ProfileService service = new ProfileService(cConfWithProfileCreationDisabled, getInjector().getInstance(MetricsSystemClient.class), getInjector().getInstance(TransactionRunner.class));
    Profile newProfile = new Profile("MyProfile", "label", "my new profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // Update the profile
    service.saveProfile(profileId, newProfile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) TransactionRunner(io.cdap.cdap.spi.data.transaction.TransactionRunner) MetricsSystemClient(io.cdap.cdap.api.metrics.MetricsSystemClient) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 22 with ProvisionerInfo

use of io.cdap.cdap.proto.provisioner.ProvisionerInfo in project cdap by cdapio.

the class ProfileServiceTest method testProfileService.

@Test
public void testProfileService() throws Exception {
    // get non-existing profile
    try {
        profileService.getProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    // delete non-existing profile
    try {
        profileService.deleteProfile(NamespaceId.DEFAULT.profile("nonExisting"));
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    ProfileId profileId = NamespaceId.DEFAULT.profile("MyProfile");
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo("defaultProvisioner", PROPERTY_SUMMARIES));
    // add a profile
    profileService.saveProfile(profileId, expected);
    // get the profile
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add a profile which already exists, should succeed and the profile property should be updated
    expected = new Profile("MyProfile", "label", "my 2nd profile for updating", new ProvisionerInfo("anotherProvisioner", Collections.emptyList()));
    profileService.saveProfile(profileId, expected);
    Assert.assertEquals(expected, profileService.getProfile(profileId));
    // add another profile to default namespace
    ProfileId profileId2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile2 = new Profile("MyProfile2", "label", "my 2nd profile for testing", new ProvisionerInfo("anotherProvisioner", PROPERTY_SUMMARIES));
    profileService.saveProfile(profileId2, profile2);
    // add default profile
    profileService.saveProfile(ProfileId.NATIVE, Profile.NATIVE);
    // get all profiles
    List<Profile> profiles = ImmutableList.of(expected, profile2, Profile.NATIVE);
    Assert.assertEquals(profiles, profileService.getProfiles(NamespaceId.DEFAULT, true));
    // by default the profile status should be enabled
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId2).getStatus());
    // by default the profile will be enabled, so enable it will throw a ProfileConflictException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // disable the profile should success
    profileService.disableProfile(profileId);
    // check the profile status to the disabled
    Assert.assertEquals(ProfileStatus.DISABLED, profileService.getProfile(profileId).getStatus());
    // disable again should throw ProfileConflictException
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    // enable should work this time
    profileService.enableProfile(profileId);
    Assert.assertEquals(ProfileStatus.ENABLED, profileService.getProfile(profileId).getStatus());
    // delete the second profile should fail since it is enabled
    try {
        profileService.deleteProfile(profileId2);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    profileService.disableProfile(profileId2);
    profileService.deleteProfile(profileId2);
    Assert.assertEquals(ImmutableList.of(expected), profileService.getProfiles(NamespaceId.DEFAULT, false));
    // add one and delete all profiles
    profileService.saveProfile(profileId2, profile2);
    profileService.disableProfile(profileId);
    profileService.disableProfile(profileId2);
    profileService.deleteAllProfiles(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.EMPTY_LIST, profileService.getProfiles(NamespaceId.DEFAULT, false));
    // try to enable and disable an non-existing profile should throw NotFoundException
    try {
        profileService.enableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
    try {
        profileService.disableProfile(profileId);
        Assert.fail();
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) NotFoundException(io.cdap.cdap.common.NotFoundException) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 23 with ProvisionerInfo

use of io.cdap.cdap.proto.provisioner.ProvisionerInfo in project cdap by cdapio.

the class ProfileHttpHandlerTest method testSystemProfiles.

@Test
public void testSystemProfiles() throws Exception {
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), listSystemProfiles(HttpURLConnection.HTTP_OK));
    Profile p1 = new Profile("p1", "label", "desc", EntityScope.SYSTEM, new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
    Optional<Profile> p1Optional = getSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    Assert.assertTrue(p1Optional.isPresent());
    Assert.assertEquals(p1, p1Optional.get());
    // check list contains both native and p1
    Set<Profile> expected = new HashSet<>();
    expected.add(Profile.NATIVE);
    expected.add(p1);
    Set<Profile> actual = new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK));
    Assert.assertEquals(expected, actual);
    // check that they're both visible to namespaces
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check we can add a profile with the same name in a namespace
    Profile p2 = new Profile(p1.getName(), p1.getLabel(), p1.getDescription(), EntityScope.USER, p1.getProvisioner());
    ProfileId p2Id = NamespaceId.DEFAULT.profile(p2.getName());
    putProfile(p2Id, p2, HttpURLConnection.HTTP_OK);
    // check that all are visible to the namespace
    expected.add(p2);
    Assert.assertEquals(expected, new HashSet<>(listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK)));
    // check that namespaced profile is not visible in system list
    expected.remove(p2);
    Assert.assertEquals(expected, new HashSet<>(listSystemProfiles(HttpURLConnection.HTTP_OK)));
    disableProfile(p2Id, HttpURLConnection.HTTP_OK);
    deleteProfile(p2Id, HttpURLConnection.HTTP_OK);
    disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    doAs(READ_WRITE_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_OK);
    });
    doAs(READ_ONLY_SYSTEM_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_OK);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_OK);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
    doAs(READ_ONLY_USER_NAME, () -> {
        listSystemProfiles(HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_FORBIDDEN);
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getSystemProfile(Profile.NATIVE.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        putSystemProfile(p1.getName(), p1, HttpURLConnection.HTTP_FORBIDDEN);
        disableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        enableSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
        deleteSystemProfile(p1.getName(), HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with ProvisionerInfo

use of io.cdap.cdap.proto.provisioner.ProvisionerInfo in project cdap by cdapio.

the class ProfileHttpHandlerTest method testListAndGetProfiles.

@Test
public void testListAndGetProfiles() throws Exception {
    // no profile should be there in default namespace
    List<Profile> profiles = listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.emptyList(), profiles);
    // try to list all profiles including system namespace before putting a new one, there should only exist a default
    // profile
    profiles = listProfiles(NamespaceId.DEFAULT, true, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(Collections.singletonList(Profile.NATIVE), profiles);
    // test get single profile endpoint
    ProfileId profileId = NamespaceId.DEFAULT.profile("p1");
    Profile expected = new Profile("p1", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
    Profile actual = getProfile(profileId, HttpURLConnection.HTTP_OK).get();
    Assert.assertEquals(expected, actual);
    // get a nonexisting profile should get a not found code
    getProfile(NamespaceId.DEFAULT.profile("nonExisting"), HttpURLConnection.HTTP_NOT_FOUND);
    doAs(READ_ONLY_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_OK);
        getProfile(profileId, HttpURLConnection.HTTP_OK);
    });
    doAs(NO_ACCESS_USER_NAME, () -> {
        listProfiles(NamespaceId.DEFAULT, false, HttpURLConnection.HTTP_FORBIDDEN);
        getProfile(profileId, HttpURLConnection.HTTP_FORBIDDEN);
    });
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 25 with ProvisionerInfo

use of io.cdap.cdap.proto.provisioner.ProvisionerInfo in project cdap by cdapio.

the class ProfileHttpHandlerTest method testEnableDisableProfile.

@Test
public void testEnableDisableProfile() throws Exception {
    Profile expected = new Profile("MyProfile", "label", "my profile for testing", new ProvisionerInfo(MockProvisioner.NAME, PROPERTY_SUMMARIES));
    ProfileId profileId = NamespaceId.DEFAULT.profile(expected.getName());
    // enable and disable a non-existing profile should give a 404
    enableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
    disableProfile(profileId, HttpURLConnection.HTTP_NOT_FOUND);
    // put the profile
    putProfile(profileId, expected, HttpURLConnection.HTTP_OK);
    // by default the status should be enabled
    Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // enable it again should give a 409
    enableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // disable should work
    disableProfile(profileId, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(ProfileStatus.DISABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // disable again should give a 409
    disableProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // enable should work
    enableProfile(profileId, HttpURLConnection.HTTP_OK);
    Assert.assertEquals(ProfileStatus.ENABLED, getProfileStatus(profileId, HttpURLConnection.HTTP_OK).get());
    // now delete should not work since we have the profile enabled
    deleteProfile(profileId, HttpURLConnection.HTTP_CONFLICT);
    // disable and delete
    disableProfile(profileId, HttpURLConnection.HTTP_OK);
    deleteProfile(profileId, HttpURLConnection.HTTP_OK);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProvisionerInfo(io.cdap.cdap.proto.provisioner.ProvisionerInfo) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Aggregations

ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)44 Test (org.junit.Test)40 Profile (io.cdap.cdap.proto.profile.Profile)32 ProfileId (io.cdap.cdap.proto.id.ProfileId)26 ProvisionerPropertyValue (io.cdap.cdap.proto.provisioner.ProvisionerPropertyValue)12 ArrayList (java.util.ArrayList)12 NotFoundException (io.cdap.cdap.common.NotFoundException)6 BootstrapStepResult (io.cdap.cdap.proto.bootstrap.BootstrapStepResult)6 MetricsSystemClient (io.cdap.cdap.api.metrics.MetricsSystemClient)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 TransactionRunner (io.cdap.cdap.spi.data.transaction.TransactionRunner)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)2 MockProvisioner (io.cdap.cdap.internal.provision.MockProvisioner)2 ProgramType (io.cdap.cdap.proto.ProgramType)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 ProvisionerDetail (io.cdap.cdap.proto.provisioner.ProvisionerDetail)2