Skip to content

Commit

Permalink
ISIS-3063: RO: adds listBooks test via an aliased domain object (VM)
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Jun 1, 2022
1 parent f3c7597 commit 16262c9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public enum RepresentationTypeSimplifiedV2 {
*/
OBJECT_COLLECTION("object-collection"),

/**
* The media type used as content-Type header when an object property is rendered.
*/
OBJECT_PROPERTY("object-property"),

/**
* The media type used as content-Type header when a standalone collection is rendered.
*/
Expand Down Expand Up @@ -76,6 +81,7 @@ public enum RepresentationTypeSimplifiedV2 {

public boolean isObject() { return this == OBJECT; }
public boolean isObjectCollection() { return this == OBJECT_COLLECTION; }
public boolean isObjectProperty() { return this == OBJECT_PROPERTY; }
public boolean isList() { return this == LIST; }
public boolean isValue() { return this == VALUE; }
public boolean isValues() { return this == VALUES; }
Expand Down Expand Up @@ -117,10 +123,12 @@ private static String trimQuotesIfAny(final String s) {
}

private static Optional<String> extractReprType(final @NonNull Stream<String> stringStream) {

return stringStream
//.peek(System.out::println)//debug
//.peek(System.err::println)//debug
.map(String::trim)
.filter(_Strings::isNotEmpty)
//.map(s->s.replace("profile=\"urn:org.restfulobjects:repr-types/", "repr-type=\""))
.filter(s->s.startsWith("repr-type"))
.map(s->_Strings.parseKeyValuePair(s, '=').orElse(null))
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ class RestServiceTest {
@LocalServerPort int port; // just for reference (not used)
@Inject RestEndpointService restService;

@Test
void httpSessionInfo() {

val useRequestDebugLogging = false;
val restfulClient = restService.newClient(useRequestDebugLogging);

val digest = restService.getHttpSessionInfo(restfulClient)
.ifFailure(Assertions::fail);

val httpSessionInfo = digest.getValue().orElseThrow();

assertNotNull(httpSessionInfo);

// NB: this works only because we excluded wicket viewer from the app.
assertEquals("no http-session", httpSessionInfo);

}

@Test
void bookOfTheWeek_viaRestEndpoint() {
Expand Down Expand Up @@ -169,26 +186,29 @@ void inventoryAsJaxbVm_viaRestEndpoint() {
assertNotNull(inventoryAsJaxbVm);
assertEquals("Bookstore", inventoryAsJaxbVm.getName());

//TODO test whether we can call an action eg. listBooks() on the VM via REST

}

@Test
void httpSessionInfo() {
void listBooks_fromInventoryAsJaxbVm_viaRestEndpoint() {

assertTrue(restService.getPort()>0);

val useRequestDebugLogging = false;
val restfulClient = restService.newClient(useRequestDebugLogging);

val digest = restService.getHttpSessionInfo(restfulClient)
val digest = restService.getBooksFromInventoryAsJaxbVm(restfulClient)
.ifFailure(Assertions::fail);

val httpSessionInfo = digest.getValue().orElseThrow();
val books = digest.getValue().orElseThrow();

assertNotNull(httpSessionInfo);
val expectedBookTitles = JdoTestFixtures.expectedBookTitles();

// NB: this works only because we excluded wicket viewer from the app.
assertEquals("no http-session", httpSessionInfo);
val multipleBooks = books
.filter(book->expectedBookTitles.contains(book.getName()));

assertEquals(3, multipleBooks.size());

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.isis.testdomain.jdo.JdoInventoryJaxbVm;
import org.apache.isis.testdomain.jdo.JdoTestFixtures;
import org.apache.isis.testdomain.model.valuetypes.composite.CalendarEventJaxbVm;
import org.apache.isis.viewer.restfulobjects.jaxrsresteasy4.IsisModuleViewerRestfulObjectsJaxrsResteasy4;
import org.apache.isis.viewer.wicket.viewer.IsisModuleViewerWicketViewer;

/**
Expand All @@ -54,6 +55,9 @@
// UI (Wicket Viewer)
IsisModuleViewerWicketViewer.class,

IsisModuleViewerRestfulObjectsJaxrsResteasy4.class,


XrayEnable.class // for debugging only
})
public class TestAppJdoWkt extends SpringBootServletInitializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
@XmlAccessorType(XmlAccessType.FIELD)
@Named("testdomain.jdo.JdoInventoryJaxbVm")
@DomainObject(
nature=Nature.VIEW_MODEL)
nature=Nature.VIEW_MODEL,
aliased={
"testdomain.jdo.JdoInventoryJaxbVmAlias"
})
public class JdoInventoryJaxbVm {

@XmlTransient @Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public BookDto recommendedBookOfTheWeekDto() {
public List<JdoBook> multipleBooks(
@ParameterLayout(named = "")
final int nrOfBooks) {
return listBooks();//createMultipleBooks("MultipleBooksTest", nrOfBooks, repository::persist);
return listBooks();
}

@Action //TODO improve the REST client such that the param can be of type Book
Expand Down Expand Up @@ -139,7 +139,7 @@ public List<BookDto> multipleBooksAsDto(
@Action
public JdoInventoryJaxbVm inventoryAsJaxbVm() {
val inventoryJaxbVm = factoryService.viewModel(new JdoInventoryJaxbVm());
val books = listBooks();//createMultipleBooks("InventoryAsJaxbVmTest", 3, repository::persist);
val books = listBooks();
if(_NullSafe.size(books)>0) {
inventoryJaxbVm.setName("Bookstore");
inventoryJaxbVm.setBooks(books);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.isis.applib.events.metamodel.MetamodelListener;
import org.apache.isis.applib.services.bookmark.Bookmark;
import org.apache.isis.applib.services.bookmark.BookmarkService;
import org.apache.isis.applib.services.factory.FactoryService;
import org.apache.isis.applib.services.iactnlayer.InteractionService;
Expand Down Expand Up @@ -118,6 +119,10 @@ public void addABookTo(final JdoInventory inventory) {
"Sample Publisher"));
}

public Bookmark getJdoInventoryJaxbVmAsBookmark() {
return bookmarkService.bookmarkForElseFail(setUpViewmodelWith3Books());
}

public JdoInventoryJaxbVm setUpViewmodelWith3Books() {
val inventoryJaxbVm = factoryService.viewModel(new JdoInventoryJaxbVm());
val books = inventoryJaxbVm.listBooks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import org.springframework.stereotype.Service;

import org.apache.isis.applib.client.SuppressionType;
import org.apache.isis.applib.services.iactnlayer.InteractionService;
import org.apache.isis.commons.collections.Can;
import org.apache.isis.commons.functional.Try;
import org.apache.isis.core.config.RestEasyConfiguration;
import org.apache.isis.core.config.viewer.web.WebAppContextPath;
import org.apache.isis.testdomain.jdo.JdoInventoryJaxbVm;
import org.apache.isis.testdomain.jdo.JdoTestFixtures;
import org.apache.isis.testdomain.jdo.entities.JdoBook;
import org.apache.isis.testdomain.ldap.LdapConstants;
import org.apache.isis.testdomain.util.dto.BookDto;
Expand All @@ -42,26 +44,20 @@
import org.apache.isis.viewer.restfulobjects.client.log.ClientConversationFilter;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import lombok.extern.log4j.Log4j2;

@Service
@Log4j2
@RequiredArgsConstructor(onConstructor_ = {@Inject})
public class RestEndpointService {

private final Environment environment;
private final RestEasyConfiguration restEasyConfiguration;
private final WebAppContextPath webAppContextPath;

@Inject
public RestEndpointService(
final Environment environment,
final RestEasyConfiguration restEasyConfiguration,
final WebAppContextPath webAppContextPath) {
this.environment = environment;
this.restEasyConfiguration = restEasyConfiguration;
this.webAppContextPath = webAppContextPath;
}
private final JdoTestFixtures jdoTestFixtures;
private final InteractionService interactionService;

public int getPort() {
if(port==null) {
Expand Down Expand Up @@ -113,8 +109,8 @@ public RestfulClient newClient(

// -- NEW REQUEST BUILDER

public Invocation.Builder newInvocationBuilder(final RestfulClient client, final String actionPath) {
return client.request(actionPath, SuppressionType.ALL);
public Invocation.Builder newInvocationBuilder(final RestfulClient client, final String endpointPath) {
return client.request(endpointPath, SuppressionType.ALL);
}

// -- ENDPOINTS
Expand Down Expand Up @@ -210,10 +206,27 @@ public Try<JdoInventoryJaxbVm> getInventoryAsJaxbVm(final RestfulClient client)

val response = request.post(args);
val digest = client.digest(response, JdoInventoryJaxbVm.class);

return digest;
}

public Try<Can<JdoBook>> getBooksFromInventoryAsJaxbVm(final RestfulClient client) {

val objectId = interactionService.callAnonymous(
()->jdoTestFixtures.getJdoInventoryJaxbVmAsBookmark().getIdentifier());

// using domain object alias ...
val request = newInvocationBuilder(client,
"objects/testdomain.jdo.JdoInventoryJaxbVmAlias/"
+ objectId + "/actions/listBooks/invoke");

val args = client.arguments()
.build();

val response = request.post(args);
val digest = client.digestList(response, JdoBook.class, new GenericType<List<JdoBook>>() {});

return digest;
}

public Try<String> getHttpSessionInfo(final RestfulClient client) {

Expand All @@ -228,7 +241,6 @@ public Try<String> getHttpSessionInfo(final RestfulClient client) {
return digest;
}


// -- HELPER

private Integer port;
Expand All @@ -238,5 +250,4 @@ private void init() {
port = Integer.parseInt(environment.getProperty("local.server.port"));
}


}

0 comments on commit 16262c9

Please sign in to comment.