Search in sources :

Example 1 with PreferencesDetail

use of io.cdap.cdap.proto.PreferencesDetail in project cdap by caskdata.

the class PreferencesHttpHandlerInternalTest method testApplication.

@Test
public void testApplication() throws Exception {
    String appName = AllProgramsApp.NAME;
    String namespace1 = TEST_NAMESPACE1;
    String uriInstance = getPreferenceURI();
    String uriNamespace1 = getPreferenceURI(namespace1);
    String uriApp = getPreferenceURI(namespace1, appName);
    PreferencesDetail detail;
    Map<String, String> combinedProperties = Maps.newHashMap();
    // Application not created yet. Get preferences should succeed and get back one with empty properties.
    detail = getPreferencesInternal(getPreferenceURI(namespace1, "some_non_existing_app"), false, HttpResponseStatus.OK);
    Assert.assertTrue(detail.getProperties().isEmpty());
    Assert.assertFalse(detail.getResolved());
    Assert.assertEquals(0, detail.getSeqId());
    // Create the app.
    addApplication(namespace1, new AllProgramsApp());
    Map<String, String> propMap = Maps.newHashMap();
    Assert.assertEquals(propMap, getPreferences(uriApp, false, 200));
    Assert.assertEquals(propMap, getPreferences(uriApp, true, 200));
    getPreferences(getPreferenceURI(namespace1, "InvalidAppName"), false, 404);
    // Application created but no preferences created yet. API call still succeeds but result is empty.
    detail = getPreferencesInternal(uriApp, false, HttpResponseStatus.OK);
    Assert.assertEquals(Collections.emptyMap(), detail.getProperties());
    // For entity without any references, seqId is set to default 0, otherwise it should be always > 0.
    Assert.assertEquals(0, detail.getSeqId());
    // Set the preference
    Map<String, String> instanceProperties = ImmutableMap.of("key0", "instance-val0", "instance-key1", "instance-val1");
    Map<String, String> namespace1Properties = ImmutableMap.of("key0", "namespace-val0", "namespace1-key1", "namespace1-val1");
    Map<String, String> appProperties = ImmutableMap.of("key0", "app-val0", "app-key1", "app-val1");
    setPreferences(uriInstance, instanceProperties, 200);
    setPreferences(uriNamespace1, namespace1Properties, 200);
    setPreferences(uriApp, appProperties, 200);
    // Get and verify preferences on the application
    detail = getPreferencesInternal(uriApp, false, HttpResponseStatus.OK);
    Assert.assertEquals(appProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertFalse(detail.getResolved());
    // Get and verify resolved preferences on the application
    detail = getPreferencesInternal(uriApp, true, HttpResponseStatus.OK);
    combinedProperties.clear();
    combinedProperties.putAll(instanceProperties);
    combinedProperties.putAll(namespace1Properties);
    combinedProperties.putAll(appProperties);
    Assert.assertEquals(combinedProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
    // Delete preferences on the application and verify resolved
    deletePreferences(uriApp, 200);
    detail = getPreferencesInternal(uriApp, true, HttpResponseStatus.OK);
    combinedProperties.clear();
    combinedProperties.putAll(instanceProperties);
    combinedProperties.putAll(namespace1Properties);
    Assert.assertEquals(combinedProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
    // Delete preferences on the namespace and verify.
    deletePreferences(uriNamespace1, 200);
    detail = getPreferencesInternal(uriApp, true, HttpResponseStatus.OK);
    combinedProperties.clear();
    combinedProperties.putAll(instanceProperties);
    Assert.assertEquals(combinedProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
    // Delete preferences on the instance and verify.
    deletePreferences(uriInstance, 200);
    detail = getPreferencesInternal(uriApp, true, HttpResponseStatus.OK);
    combinedProperties.clear();
    Assert.assertEquals(combinedProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) AllProgramsApp(io.cdap.cdap.AllProgramsApp) Test(org.junit.Test)

Example 2 with PreferencesDetail

use of io.cdap.cdap.proto.PreferencesDetail in project cdap by caskdata.

the class PreferencesHttpHandlerInternalTest method testInstance.

@Test
public void testInstance() throws Exception {
    String uriInstance = "";
    // Verify preferences are unset.
    Map<String, String> properties = Maps.newHashMap();
    Assert.assertEquals(properties, getPreferences(uriInstance, false, 200));
    Assert.assertEquals(properties, getPreferences(uriInstance, true, 200));
    // Set preferences.
    properties.put("key1", "val1");
    properties.put("key2", "val2");
    setPreferences(uriInstance, properties, 200);
    // Get preferences via internal REST APIs and validate.
    PreferencesDetail detail1 = null;
    detail1 = getPreferencesInternal(uriInstance, false, HttpResponseStatus.OK);
    Assert.assertEquals(properties, detail1.getProperties());
    Assert.assertFalse(detail1.getResolved());
    Assert.assertTrue(detail1.getSeqId() > 0);
    // Update preferences.
    properties.put("key3", "val3");
    setPreferences(uriInstance, properties, 200);
    // Get preferences via internal REST APIs and validate.
    PreferencesDetail detail2 = null;
    detail2 = getPreferencesInternal(uriInstance, false, HttpResponseStatus.OK);
    Assert.assertEquals(properties, detail2.getProperties());
    Assert.assertFalse(detail2.getResolved());
    Assert.assertTrue(detail2.getSeqId() > 0);
    Assert.assertTrue(detail2.getSeqId() > detail1.getSeqId());
    // "Resolved" should be ignored at instance level, as instance is the top level.
    detail2 = getPreferencesInternal(uriInstance, true, HttpResponseStatus.OK);
    Assert.assertEquals(properties, detail2.getProperties());
    Assert.assertFalse(detail2.getResolved());
    Assert.assertTrue(detail2.getSeqId() > 0);
    Assert.assertTrue(detail2.getSeqId() > detail1.getSeqId());
    // Delete preferences
    properties.clear();
    deletePreferences(uriInstance, 200);
    Assert.assertEquals(properties, getPreferences(uriInstance, false, 200));
    Assert.assertEquals(properties, getPreferences(uriInstance, true, 200));
    // Deleting preferences just set preferences to empty, the record row still exists and seqId should be there.
    PreferencesDetail detail3 = null;
    detail3 = getPreferencesInternal(uriInstance, false, HttpResponseStatus.OK);
    Assert.assertEquals(properties, detail3.getProperties());
    Assert.assertFalse(detail3.getResolved());
    Assert.assertTrue(detail3.getSeqId() > 0);
    Assert.assertTrue(detail3.getSeqId() > detail2.getSeqId());
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) Test(org.junit.Test)

Example 3 with PreferencesDetail

use of io.cdap.cdap.proto.PreferencesDetail in project cdap by caskdata.

the class PreferencesHttpHandlerInternalTest method testProgram.

@Test
public void testProgram() throws Exception {
    String uriInstance = getPreferenceURI();
    String namespace2 = TEST_NAMESPACE2;
    String appName = AllProgramsApp.NAME;
    String uriNamespace2Service = getPreferenceURI(namespace2, appName, "services", AllProgramsApp.NoOpService.NAME);
    PreferencesDetail detail;
    Map<String, String> programProperties = Maps.newHashMap();
    // Create application.
    addApplication(namespace2, new AllProgramsApp());
    // Get preferences on invalid program type
    getPreferencesInternal(getPreferenceURI(namespace2, appName, "invalidType", "somename"), false, HttpResponseStatus.BAD_REQUEST);
    // Get preferences on non-existing program id. Should succeed and get back a PreferencesDetail with empty properites
    detail = getPreferencesInternal(getPreferenceURI(namespace2, appName, "services", "somename"), false, HttpResponseStatus.OK);
    Assert.assertTrue(detail.getProperties().isEmpty());
    Assert.assertFalse(detail.getResolved());
    Assert.assertEquals(0, detail.getSeqId());
    // Set preferences on the program
    programProperties.clear();
    programProperties.put("key0", "program-val0");
    programProperties.put("program-key1", "program-val1");
    setPreferences(uriNamespace2Service, programProperties, 200);
    // Get and verify preferences
    detail = getPreferencesInternal(uriNamespace2Service, false, HttpResponseStatus.OK);
    Assert.assertEquals(programProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertFalse(detail.getResolved());
    // Set preferences on the instance and verify.
    Map<String, String> instanceProperties = ImmutableMap.of("key0", "instance-key0", "instance-key1", "instance-val1");
    setPreferences(uriInstance, instanceProperties, 200);
    // Get resolved preferences on the program
    detail = getPreferencesInternal(uriNamespace2Service, true, HttpResponseStatus.OK);
    Map<String, String> combinedProperties = Maps.newHashMap();
    combinedProperties.putAll(instanceProperties);
    combinedProperties.putAll(programProperties);
    Assert.assertEquals(combinedProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
    // Delete preferences on the program
    deletePreferences(uriNamespace2Service, 200);
    detail = getPreferencesInternal(uriNamespace2Service, true, HttpResponseStatus.OK);
    Assert.assertEquals(instanceProperties, detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
    // Delete preferences on the instance
    deletePreferences(uriInstance, 200);
    detail = getPreferencesInternal(uriNamespace2Service, true, HttpResponseStatus.OK);
    Assert.assertEquals(Collections.emptyMap(), detail.getProperties());
    Assert.assertTrue(detail.getSeqId() > 0);
    Assert.assertTrue(detail.getResolved());
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) AllProgramsApp(io.cdap.cdap.AllProgramsApp) Test(org.junit.Test)

Example 4 with PreferencesDetail

use of io.cdap.cdap.proto.PreferencesDetail in project cdap by caskdata.

the class LocalPreferencesFetcherInternal method get.

/**
 * Get preferences for the given identify
 */
public PreferencesDetail get(EntityId entityId, boolean resolved) {
    final PreferencesService service = preferencesService;
    PreferencesDetail detail = null;
    switch(entityId.getEntityType()) {
        case INSTANCE:
            detail = resolved ? service.getResolvedPreferences() : service.getPreferences();
            break;
        case NAMESPACE:
            NamespaceId namespaceId = (NamespaceId) entityId;
            detail = resolved ? service.getResolvedPreferences(namespaceId) : service.getPreferences(namespaceId);
            break;
        case APPLICATION:
            ApplicationId appId = (ApplicationId) entityId;
            detail = resolved ? service.getResolvedPreferences(appId) : service.getPreferences(appId);
            break;
        case PROGRAM:
            ProgramId programId = (ProgramId) entityId;
            detail = resolved ? service.getResolvedPreferences(programId) : service.getPreferences(programId);
            break;
        default:
            throw new UnsupportedOperationException(String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType()));
    }
    return detail;
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) PreferencesService(io.cdap.cdap.config.PreferencesService)

Example 5 with PreferencesDetail

use of io.cdap.cdap.proto.PreferencesDetail in project cdap by caskdata.

the class PropertiesResolver method getUserProperties.

public Map<String, String> getUserProperties(ProgramId id) throws IOException, NotFoundException, UnauthorizedException {
    PreferencesDetail preferencesDetail = preferencesFetcher.get(id, true);
    Map<String, String> userArgs = new HashMap<>(preferencesDetail.getProperties());
    userArgs.put(ProgramOptionConstants.LOGICAL_START_TIME, Long.toString(System.currentTimeMillis()));
    return userArgs;
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) HashMap(java.util.HashMap)

Aggregations

PreferencesDetail (io.cdap.cdap.proto.PreferencesDetail)14 Test (org.junit.Test)6 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 ProgramId (io.cdap.cdap.proto.id.ProgramId)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 HashMap (java.util.HashMap)3 AllProgramsApp (io.cdap.cdap.AllProgramsApp)2 InstanceId (io.cdap.cdap.proto.id.InstanceId)2 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)1 PreviewMessage (io.cdap.cdap.app.preview.PreviewMessage)1 PreviewRequest (io.cdap.cdap.app.preview.PreviewRequest)1 PreviewStatus (io.cdap.cdap.app.preview.PreviewStatus)1 ProgramController (io.cdap.cdap.app.runtime.ProgramController)1 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)1 PreferencesService (io.cdap.cdap.config.PreferencesService)1 AbstractListener (io.cdap.cdap.internal.app.runtime.AbstractListener)1 BasicThrowable (io.cdap.cdap.proto.BasicThrowable)1 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)1