Search in sources :

Example 6 with NullPropertiesProvider

use of io.crnk.core.engine.properties.NullPropertiesProvider in project crnk-framework by crnk-project.

the class QueryParamsAdapterTest method test.

@Test
public void test() {
    ModuleRegistry moduleRegistry = new ModuleRegistry();
    moduleRegistry.getHttpRequestContextProvider().setServiceUrlProvider(new ConstantServiceUrlProvider("http://localhost"));
    ResourceRegistry resourceRegistry = new ResourceRegistryImpl(new DefaultResourceRegistryPart(), moduleRegistry);
    QueryParams params = new QueryParams();
    DefaultResourceInformationProvider builder = new DefaultResourceInformationProvider(new NullPropertiesProvider(), new OffsetLimitPagingBehavior(), new DefaultResourceFieldInformationProvider(), new JacksonResourceFieldInformationProvider());
    builder.init(new DefaultResourceInformationProviderContext(builder, new DefaultInformationBuilder(moduleRegistry.getTypeParser()), moduleRegistry.getTypeParser(), new ObjectMapper()));
    ResourceInformation info = builder.build(Task.class);
    QueryParamsAdapter adapter = new QueryParamsAdapter(info, params, moduleRegistry);
    Assert.assertEquals(Task.class, adapter.getResourceInformation().getResourceClass());
    Assert.assertEquals(resourceRegistry, adapter.getResourceRegistry());
}
Also used : OffsetLimitPagingBehavior(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ModuleRegistry(io.crnk.core.module.ModuleRegistry) ResourceRegistryImpl(io.crnk.core.engine.internal.registry.ResourceRegistryImpl) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) DefaultResourceInformationProviderContext(io.crnk.legacy.registry.DefaultResourceInformationProviderContext) DefaultResourceInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JacksonResourceFieldInformationProvider(io.crnk.core.engine.internal.jackson.JacksonResourceFieldInformationProvider) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) DefaultResourceRegistryPart(io.crnk.core.engine.registry.DefaultResourceRegistryPart) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultResourceFieldInformationProvider(io.crnk.core.engine.internal.information.resource.DefaultResourceFieldInformationProvider) Test(org.junit.Test)

Example 7 with NullPropertiesProvider

use of io.crnk.core.engine.properties.NullPropertiesProvider in project crnk-framework by crnk-project.

the class CustomResourceFieldTest method setupModule.

@Override
protected void setupModule(final JpaModule module, boolean server) {
    super.setupModule(module, server);
    if (server) {
        module.setResourceInformationProvider(new JpaResourceInformationProvider(new NullPropertiesProvider()) {

            @Override
            protected List<ResourceField> getResourceFields(Class clazz) {
                List<ResourceField> fields = super.getResourceFields(clazz);
                if (clazz == CountryEntity.class) {
                    List<String> languages = Arrays.asList("en", "de");
                    for (final String language : languages) {
                        ResourceFieldType resourceFieldType = ResourceFieldType.ATTRIBUTE;
                        String name = language + "Text";
                        Class<?> type = String.class;
                        ResourceFieldAccess access = new ResourceFieldAccess(true, true, true, false, false);
                        ResourceFieldImpl field = new ResourceFieldImpl(name, name, resourceFieldType, type, type, null, null, SerializeType.LAZY, LookupIncludeBehavior.NONE, access, null, null, null, RelationshipRepositoryBehavior.DEFAULT);
                        field.setAccessor(new ResourceFieldAccessor() {

                            @Override
                            public String getValue(Object resource) {
                                CountryEntity country = (CountryEntity) resource;
                                List<CountryTranslationEntity> translations = country.getTranslations();
                                CountryTranslationEntity translation = getTranslation(translations, language);
                                return translation != null ? translation.getTxt() : null;
                            }

                            @Override
                            public void setValue(Object resource, Object fieldValue) {
                                CountryEntity country = (CountryEntity) resource;
                                List<CountryTranslationEntity> translations = country.getTranslations();
                                CountryTranslationEntity translation = getTranslation(translations, language);
                                if (translation == null) {
                                    EntityManager em = module.getEntityManager();
                                    LangEntity langEntity = em.find(LangEntity.class, language);
                                    if (langEntity == null) {
                                        throw new IllegalStateException("language not found: " + language);
                                    }
                                    translation = new CountryTranslationEntity();
                                    CountryTranslationPK pk = new CountryTranslationPK();
                                    pk.setCountry(country);
                                    pk.setLang(langEntity);
                                    translation.setCountryTranslationPk(pk);
                                    translations.add(translation);
                                }
                                translation.setTxt((String) fieldValue);
                            }

                            private CountryTranslationEntity getTranslation(List<CountryTranslationEntity> translations, String language) {
                                for (CountryTranslationEntity translation : translations) {
                                    CountryTranslationPK translationPk = translation.getCountryTranslationPk();
                                    String langCd = translationPk.getLang().getLangCd();
                                    if (langCd.equals(language)) {
                                        return translation;
                                    }
                                }
                                return null;
                            }
                        });
                        fields.add(field);
                    }
                }
                return fields;
            }
        });
    }
}
Also used : ResourceFieldImpl(io.crnk.core.engine.internal.information.resource.ResourceFieldImpl) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) CountryTranslationPK(io.crnk.jpa.model.CountryTranslationPK) ResourceFieldAccess(io.crnk.core.engine.information.resource.ResourceFieldAccess) LangEntity(io.crnk.jpa.model.LangEntity) JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceFieldType(io.crnk.core.engine.information.resource.ResourceFieldType) ResourceFieldAccessor(io.crnk.core.engine.information.resource.ResourceFieldAccessor) CountryTranslationEntity(io.crnk.jpa.model.CountryTranslationEntity) EntityManager(javax.persistence.EntityManager) List(java.util.List) CountryEntity(io.crnk.jpa.model.CountryEntity)

Example 8 with NullPropertiesProvider

use of io.crnk.core.engine.properties.NullPropertiesProvider in project crnk-framework by crnk-project.

the class JpaResourceInformationProviderTest method setup.

@Before
public void setup() {
    jpaMetaProvider = new JpaMetaProvider(Collections.<Class>emptySet());
    lookup = new MetaLookup();
    lookup.addProvider(jpaMetaProvider);
    builder = new JpaResourceInformationProvider(new NullPropertiesProvider());
    builder.init(new DefaultResourceInformationProviderContext(builder, new DefaultInformationBuilder(new TypeParser()), new TypeParser(), new ObjectMapper()));
}
Also used : JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) MetaLookup(io.crnk.meta.MetaLookup) DefaultInformationBuilder(io.crnk.core.engine.internal.information.DefaultInformationBuilder) TypeParser(io.crnk.core.engine.parser.TypeParser) JpaMetaProvider(io.crnk.jpa.meta.JpaMetaProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) DefaultResourceInformationProviderContext(io.crnk.legacy.registry.DefaultResourceInformationProviderContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 9 with NullPropertiesProvider

use of io.crnk.core.engine.properties.NullPropertiesProvider in project crnk-framework by crnk-project.

the class ResourceIdControllerTest method checkPost.

private Resource checkPost() throws IOException {
    // GIVEN
    Resource resource = createResource();
    resource.getRelationships().put("testLookupAlways", new Relationship(schedule3Id));
    Document newDocument = createDocument(resource);
    JsonPath postPath = pathBuilder.build("/relationIdTest");
    // WHEN POST
    ResourcePost sut = new ResourcePost(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = sut.handle(postPath, emptyTaskQuery, null, newDocument);
    // THEN POST
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    Resource createdResource = taskResponse.getDocument().getSingleData().get();
    Assert.assertEquals(resource.getType(), createdResource.getType());
    Assert.assertEquals(resource.getId(), createdResource.getId());
    Assert.assertEquals(resource.getAttributes().get("name"), createdResource.getAttributes().get("name"));
    Relationship relationship = createdResource.getRelationships().get("testLookupAlways");
    Assert.assertTrue(relationship.getData().isPresent());
    Assert.assertEquals(schedule3Id, relationship.getData().get());
    // validate relationship not accessed, only id set => performance
    RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
    Assert.assertEquals(schedule3.getId(), entity.getTestLookupAlwaysId());
    Assert.assertNull(entity.getTestLookupAlways());
    return createdResource;
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource)

Example 10 with NullPropertiesProvider

use of io.crnk.core.engine.properties.NullPropertiesProvider in project crnk-framework by crnk-project.

the class ClientResourceUpsertTest method setup.

@Before
public void setup() {
    boot = new CrnkBoot();
    boot.addModule(new TestModule());
    boot.boot();
    PropertiesProvider propertiesProvider = new NullPropertiesProvider();
    ClientProxyFactory proxyFactory = Mockito.mock(ClientProxyFactory.class);
    upsert = new ClientResourceUpsert(boot.getResourceRegistry(), propertiesProvider, boot.getModuleRegistry().getTypeParser(), boot.getObjectMapper(), boot.getDocumentMapper(), proxyFactory);
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) CrnkBoot(io.crnk.core.boot.CrnkBoot) ClientProxyFactory(io.crnk.client.internal.proxy.ClientProxyFactory) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) TestModule(io.crnk.test.mock.TestModule) Before(org.junit.Before)

Aggregations

NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)11 Before (org.junit.Before)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)4 DefaultInformationBuilder (io.crnk.core.engine.internal.information.DefaultInformationBuilder)3 DefaultResourceFieldInformationProvider (io.crnk.core.engine.internal.information.resource.DefaultResourceFieldInformationProvider)3 DefaultResourceInformationProvider (io.crnk.core.engine.internal.information.resource.DefaultResourceInformationProvider)3 JacksonResourceFieldInformationProvider (io.crnk.core.engine.internal.jackson.JacksonResourceFieldInformationProvider)3 JpaResourceInformationProvider (io.crnk.jpa.internal.JpaResourceInformationProvider)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Response (io.crnk.core.engine.dispatcher.Response)2 Document (io.crnk.core.engine.document.Document)2 Relationship (io.crnk.core.engine.document.Relationship)2 Resource (io.crnk.core.engine.document.Resource)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)2 ResourceFieldAccess (io.crnk.core.engine.information.resource.ResourceFieldAccess)2 ResourceFieldAccessor (io.crnk.core.engine.information.resource.ResourceFieldAccessor)2 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 ResourceInformationProvider (io.crnk.core.engine.information.resource.ResourceInformationProvider)2 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)2 TypeParser (io.crnk.core.engine.parser.TypeParser)2