adobe / aem-component-generator

AEM Component Generator is a java project that enables developers to generate the base structure of an AEM component using a JSON configuration file specifying component and dialog properties and other configuration options.
Apache License 2.0
113 stars 46 forks source link

Error while trying to use component on page #41

Closed Harshitha942 closed 4 years ago

Harshitha942 commented 4 years ago

Hi ,

I have created a basic component , but then when i try to use it on the page , shows the following error

image My Bundle is active

Interface

package com.aem.geeks.core.models;

public interface Component {

String getFirstName();

String getLastName();

String getGender();

Integer getPhoneNo();

}

Java class

package com.aem.geeks.core.models.impl;

import javax.annotation.PostConstruct;

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Via; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.aem.geeks.core.models.Component;

@Model(adaptables = SlingHttpServletRequest.class, adapters = Component.class, defaultInjectionStrategy = DefaultInjectionStrategy.REQUIRED) public class Componentimpl implements Component {

@ValueMapValue
@Via("resource") @Default(values = "****")
String fname;

@ValueMapValue
@Default(values = "####")
String lname;

@ValueMapValue
String gender;

@ValueMapValue
Integer phno;

@PostConstruct
public void init() {
    // init
}

@Override
public String getFirstName() {
    return fname;
}

@Override
public String getLastName() {
    return lname;
}

@Override
public String getGender() {
    return gender;
}

@Override
public Integer getPhoneNo() {

    return phno;
}

}

HitmanInWis commented 4 years ago

Looks like you added code beyond the standard generated code, such as defaultInjectionStrategy = DefaultInjectionStrategy.REQUIRED.

For your component that is experiencing the error, do you have fname, lname, and phno, and gender values all set on the component? (otherwise the ones with default will fail, due to your injection strategy of "required")

Also, why the @Via("resource") for fname String property?

HitmanInWis commented 4 years ago

if you can't debug the issue based on my comments above, I recommend first testing with the default sling model output by the generator, and then adding your customizations to the model one by one until it breaks so that you can determine which customization breaks it.

Harshitha942 commented 4 years ago

Thanks for the quick response,

I just resolved the issue by changing the injection strategy to "optional" before seeing your response , and yes you were right the values for the fields were not set , so it was throwing an error

This @Via("resource") for fname String property , i was just referring to a Sling Model were they used it , so I did not remove that.

Also, the phone number field isn;t working , can you tell me what sling properties to be used

HitmanInWis commented 4 years ago

Though a phone number is numeric, it's generally best to represent it as a String because it's not a "number" in how we use it (i.e. we dont do math on it, it's not a counter, etc.)

It's probably failing b/c the java max for Integer is 2147483647 so if your phone number has an area code higher than 214 it can't fit into an Integer. If you insist on keeping this a numeric value, you'll at least need to use a Long.

Harshitha942 commented 4 years ago

Hi ,for the same component above , I am trying to write down a testcase with help of Junit , but there are errors throwing up

image image this is the error im getting

This is my test class. package com.aem.geeks.core.models.impl;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;

import com.aem.geeks.core.models.Component;

import io.wcm.testing.mock.aem.junit5.AemContext; import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith(AemContextExtension.class) class ComponentimplTest { private final AemContext ctx = new AemContext();

@BeforeEach
void setUp() throws Exception {
    ctx.addModelsForClasses(Componentimpl.class);
    ctx.load().json("/com/aem/geeks/core/models/ComponentimplTest.json", "/content");

}

@Test
void testGetFirstName() {

    final String expected = "Jane";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getFirstName();

        assertEquals(expected, actual);

}

@Test
void testGetLastName() {
    final String expected = "Doe";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getLastName();

        assertEquals(expected, actual);
}

@Test
void testGetGender() {
    final String expected = "Female";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getGender();

        assertEquals(expected, actual);
}

@Test
void testGetPhoneNo() {
    fail("Not yet implemented");
}

}

Mock Json class { "component": { "jcr:primaryType": "nt:unstructured", "sling:resourceType": "aemgeeks/components/content/component", "firstname": "Jane", "lastname": "Doe", "gender": "Female"

}

}