Search in sources :

Example 11 with PreferencesDetail

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

the class PreferencesHttpHandlerInternal method getApplicationPreferences.

/**
 * Get application level preferences
 *
 * Note that if the given application doesn't exist, the return {@link PreferencesDetail} will be empty
 * (i.e. {@link PreferencesDetail#properties} will be an empty map). In the case of requesting resolved preferences,
 * the returned {@link PreferencesDetail} will include preferences from ancestor (i.e. preferences at namespace
 * and instance level)
 *
 * @param request {@link HttpRequest}
 * @param responder the responder used for sending response back to client
 * @param namespace the namespace of the application
 * @param appId the application to get preferences for
 * @param resolved whether to return resolved preferences or not
 */
@Path("/namespaces/{namespace-id}/apps/{application-id}/preferences")
@GET
public void getApplicationPreferences(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("application-id") String appId, @QueryParam("resolved") boolean resolved) {
    ApplicationId applicationId = new ApplicationId(namespace, appId);
    // No need to check if application exists. PreferencesService returns an empty PreferencesDetail when that happens.
    PreferencesDetail detail;
    if (resolved) {
        detail = preferencesService.getResolvedPreferences(applicationId);
    } else {
        detail = preferencesService.getPreferences(applicationId);
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(detail, PreferencesDetail.class));
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 12 with PreferencesDetail

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

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)

Example 13 with PreferencesDetail

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

the class PreferencesHttpHandlerInternalTest method testNamespace.

@Test
public void testNamespace() throws Exception {
    String uriInstance = getPreferenceURI();
    String uriNamespace1 = getPreferenceURI(TEST_NAMESPACE1);
    String uriNamespace2 = getPreferenceURI(TEST_NAMESPACE2);
    PreferencesDetail detail1 = null;
    PreferencesDetail detail2 = null;
    // Verify preferences are empty
    Map<String, String> namespaceProperties = Maps.newHashMap();
    Assert.assertEquals(namespaceProperties, getPreferences(uriNamespace1, false, 200));
    Assert.assertEquals(namespaceProperties, getPreferences(uriNamespace2, false, 200));
    Assert.assertEquals(namespaceProperties, getPreferences(uriNamespace1, true, 200));
    Assert.assertEquals(namespaceProperties, getPreferences(uriNamespace2, true, 200));
    // Set preferences on namespace1
    namespaceProperties.put("key0", "namespace-key0");
    namespaceProperties.put("namespace-key1", "namespace-val1");
    namespaceProperties.put("namespace-key2", "naemspace-val2");
    setPreferences(uriNamespace1, namespaceProperties, 200);
    // Get and verify preferences on namespace1 via internal REST API
    detail1 = getPreferencesInternal(uriNamespace1, true, HttpResponseStatus.OK);
    Assert.assertEquals(namespaceProperties, detail1.getProperties());
    Assert.assertTrue(detail1.getResolved());
    Assert.assertTrue(detail1.getSeqId() > 0);
    detail1 = getPreferencesInternal(uriNamespace1, false, HttpResponseStatus.OK);
    Assert.assertEquals(namespaceProperties, detail1.getProperties());
    Assert.assertFalse(detail1.getResolved());
    Assert.assertTrue(detail1.getSeqId() > 0);
    // Update preferences on namespace1
    namespaceProperties.put("namespace-key3", "namespace-val3");
    setPreferences(uriNamespace1, namespaceProperties, 200);
    // Get and verify seqId has increased via internal REST API
    detail2 = getPreferencesInternal(uriNamespace1, false, HttpResponseStatus.OK);
    Assert.assertEquals(namespaceProperties, detail2.getProperties());
    Assert.assertFalse(detail1.getResolved());
    Assert.assertTrue(detail2.getSeqId() > detail1.getSeqId());
    // Set preferences on top level instance
    Map<String, String> instanceProperties = Maps.newHashMap();
    instanceProperties.put("key0", "instance-val0");
    instanceProperties.put("instance-key1", "instance-val1");
    setPreferences(uriInstance, instanceProperties, 200);
    Assert.assertEquals(instanceProperties, getPreferencesInternal(uriInstance, false, HttpResponseStatus.OK).getProperties());
    // Get preferences on namespace1 via internal REST API and verify it is unchanged.
    Assert.assertEquals(detail2, getPreferencesInternal(uriNamespace1, false, HttpResponseStatus.OK));
    // Get resolved preferences on namespace1 via internal REST API and verify it includes preferences on instance.
    // Also namespace preferences take preferences over instance level for key "key0"
    Map<String, String> resolvedProperties = Maps.newHashMap();
    resolvedProperties.putAll(instanceProperties);
    resolvedProperties.putAll(namespaceProperties);
    detail2 = getPreferencesInternal(uriNamespace1, true, HttpResponseStatus.OK);
    Assert.assertEquals(resolvedProperties, detail2.getProperties());
    Assert.assertTrue(detail2.getResolved());
    // Get preferences on namespace2 via internal rest API.
    detail1 = getPreferencesInternal(uriNamespace2, false, HttpResponseStatus.OK);
    Assert.assertEquals(Collections.emptyMap(), detail1.getProperties());
    Assert.assertFalse(detail1.getResolved());
    // Update the preferences on the instance. Check the resolved for namespace2 via internal REST API,
    // which should reflect the change.
    instanceProperties.put("instance-key2", "instance-val2");
    setPreferences(uriInstance, instanceProperties, 200);
    detail2 = getPreferencesInternal(uriNamespace2, true, HttpResponseStatus.OK);
    Assert.assertEquals(instanceProperties, detail2.getProperties());
    Assert.assertTrue(detail2.getResolved());
    // Delete preferences for both namespace1 and namespace2
    deletePreferences(uriNamespace1, 200);
    deletePreferences(uriNamespace2, 200);
    // Set new preferences on the instance
    instanceProperties.put("instance-key3", "instance-val3");
    setPreferences(uriInstance, instanceProperties, 200);
    // Check that the change on instance shows up in the resolved for both namespaces.
    detail1 = getPreferencesInternal(uriNamespace1, true, HttpResponseStatus.OK);
    detail2 = getPreferencesInternal(uriNamespace2, true, HttpResponseStatus.OK);
    Assert.assertEquals(instanceProperties, detail1.getProperties());
    Assert.assertEquals(instanceProperties, detail2.getProperties());
    Assert.assertEquals(detail1, detail2);
    // Delete preferences for the instance.
    deletePreferences(uriInstance, 200);
    detail1 = getPreferencesInternal(uriNamespace1, true, HttpResponseStatus.OK);
    detail2 = getPreferencesInternal(uriNamespace2, true, HttpResponseStatus.OK);
    Assert.assertEquals(Collections.emptyMap(), detail1.getProperties());
    Assert.assertTrue(detail1.getSeqId() > 0);
    Assert.assertEquals(detail1, detail2);
    // Get preferences on invalid namespace should succeed, but get back a PreferencesDetail with empty property.
    PreferencesDetail detail = getPreferencesInternal(getPreferenceURI("invalidNamespace"), false, HttpResponseStatus.OK);
    Assert.assertTrue(detail.getProperties().isEmpty());
    Assert.assertFalse(detail.getResolved());
    Assert.assertEquals(0, detail.getSeqId());
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) Test(org.junit.Test)

Example 14 with PreferencesDetail

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

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 15 with PreferencesDetail

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

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)

Aggregations

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