Search in sources :

Example 6 with CoffeeNetApp

use of rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp in project coffeenet-starter by coffeenet.

the class CoffeeNetNavigationEndpointTest method invokeWithEmptyUserRoles.

@Test
public void invokeWithEmptyUserRoles() {
    CurrentCoffeeNetUser currentCoffeeNetUser = new CurrentCoffeeNetUser("username", "email");
    CoffeeNetApp coffeeNetApp = new CoffeeNetApp("NoRights", "urlNoRights", emptySet());
    List<CoffeeNetApp> coffeeNetApps = singletonList(coffeeNetApp);
    CoffeeNetNavigationAppInformation coffeeNetNavigationAppInformation = new CoffeeNetNavigationAppInformation("", "", "", "", "", "");
    CoffeeNetNavigationInformation coffeeNetNavigationInformation = new CoffeeNetNavigationInformation(currentCoffeeNetUser, coffeeNetApps, coffeeNetApp, "path", coffeeNetNavigationAppInformation);
    when(coffeeNetNavigationServiceMock.get()).thenReturn(coffeeNetNavigationInformation);
    CoffeeNetNavigationEndpoint sut = new CoffeeNetNavigationEndpoint(coffeeNetNavigationServiceMock);
    CoffeeNetNavigationInformation receivedCoffeeNetNavigationInformation = sut.invoke();
    assertThat(receivedCoffeeNetNavigationInformation.getCoffeeNetApps(), is(coffeeNetApps));
    assertThat(receivedCoffeeNetNavigationInformation.getCurrentCoffeeNetUser(), Is.is(currentCoffeeNetUser));
    assertThat(receivedCoffeeNetNavigationInformation.getLogoutPath(), is("path"));
    assertThat(receivedCoffeeNetNavigationInformation.getProfileApp(), is(coffeeNetApp));
}
Also used : CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp) Test(org.junit.Test)

Example 7 with CoffeeNetApp

use of rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp in project coffeenet-starter by coffeenet.

the class CoffeeNetNavigationDataExtractor method extractApps.

/**
 * Extracts a map of bundled {@link CoffeeNetApp}s by their category. At this time there are only 'apps' and
 * 'profile' as keys
 *
 * @return  map of {@link CoffeeNetApp}s
 */
Optional<Map<String, List<CoffeeNetApp>>> extractApps() {
    Optional<CoffeeNetAppService> coffeeNetAppService = getCoffeeNetAppService();
    if (!coffeeNetAppService.isPresent()) {
        return Optional.empty();
    }
    Map<String, List<CoffeeNetApp>> preparedCoffeeNetApps = new HashMap<>();
    Optional<CoffeeNetCurrentUserService> userService = getCoffeeNetCurrentUserService();
    // create to retrieve CoffeeNet apps
    Builder queryBuilder = AppQuery.builder();
    // add user roles to query if there is a CoffeeNet user
    userService.ifPresent(coffeeNetCurrentUserService -> coffeeNetCurrentUserService.get().ifPresent(userDetails -> queryBuilder.withRoles(userDetails.getAuthoritiesAsString())));
    Map<String, List<CoffeeNetApp>> filteredCoffeeNetApps = coffeeNetAppService.get().getApps(queryBuilder.build());
    // extract profile application
    String profileServiceName = coffeeNetNavigationProperties.getProfileServiceName();
    List<CoffeeNetApp> profileApps = filteredCoffeeNetApps.get(profileServiceName);
    if (profileApps != null) {
        CoffeeNetApp profileApp = profileApps.get(0);
        filteredCoffeeNetApps.remove(profileServiceName);
        preparedCoffeeNetApps.put("profile", singletonList(profileApp));
    }
    // retrieve all CoffeeNetApps
    List<CoffeeNetApp> firstCoffeeNetApps = filteredCoffeeNetApps.entrySet().stream().map(entry -> entry.getValue().get(0)).sorted(Comparator.comparing(CoffeeNetApp::getName, CASE_INSENSITIVE_ORDER)).collect(toList());
    preparedCoffeeNetApps.put("apps", firstCoffeeNetApps);
    return Optional.of(preparedCoffeeNetApps);
}
Also used : AppQuery(rocks.coffeenet.autoconfigure.discovery.service.AppQuery) CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp) EnumMap(java.util.EnumMap) CASE_INSENSITIVE_ORDER(java.lang.String.CASE_INSENSITIVE_ORDER) BuildProperties(org.springframework.boot.info.BuildProperties) HashMap(java.util.HashMap) CoffeeNetCurrentUserService(rocks.coffeenet.autoconfigure.security.service.CoffeeNetCurrentUserService) BUILD_PROPERTIES(rocks.coffeenet.autoconfigure.navigation.CoffeeNetNavigationDataExtractor.CoffeeNetServices.BUILD_PROPERTIES) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) USER_SERVICE(rocks.coffeenet.autoconfigure.navigation.CoffeeNetNavigationDataExtractor.CoffeeNetServices.USER_SERVICE) CoffeeNetAppService(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetAppService) CoffeeNetUserDetails(rocks.coffeenet.autoconfigure.security.service.CoffeeNetUserDetails) Map(java.util.Map) Builder(rocks.coffeenet.autoconfigure.discovery.service.AppQuery.Builder) APP_SERVICE(rocks.coffeenet.autoconfigure.navigation.CoffeeNetNavigationDataExtractor.CoffeeNetServices.APP_SERVICE) Optional(java.util.Optional) Comparator(java.util.Comparator) CoffeeNetCurrentUserService(rocks.coffeenet.autoconfigure.security.service.CoffeeNetCurrentUserService) CoffeeNetAppService(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetAppService) HashMap(java.util.HashMap) Builder(rocks.coffeenet.autoconfigure.discovery.service.AppQuery.Builder) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp)

Example 8 with CoffeeNetApp

use of rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp in project coffeenet-starter by coffeenet.

the class CoffeeNetNavigationServiceImpl method get.

@Override
public CoffeeNetNavigationInformation get() {
    CurrentCoffeeNetUser currentCoffeeNetUser = dataExtractor.extractUser().orElse(null);
    CoffeeNetNavigationAppInformation appInformation = dataExtractor.extractAppInformation().orElse(null);
    Map<String, List<CoffeeNetApp>> apps = dataExtractor.extractApps().orElseGet(Collections::emptyMap);
    String logoutPath = dataExtractor.extractLogoutPath();
    List<CoffeeNetApp> profileApps = apps.get("profile");
    CoffeeNetApp profileApp = null;
    if (profileApps != null && !profileApps.isEmpty()) {
        profileApp = profileApps.get(0);
    }
    List<CoffeeNetApp> coffeeNetApps = apps.get("apps");
    return new CoffeeNetNavigationInformation(currentCoffeeNetUser, coffeeNetApps, profileApp, logoutPath, appInformation);
}
Also used : List(java.util.List) CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp) Collections(java.util.Collections)

Example 9 with CoffeeNetApp

use of rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp in project coffeenet-starter by coffeenet.

the class CoffeeNetNavigationDataExtractorTest method extractNoUserService.

@Test
public void extractNoUserService() {
    Map<String, List<CoffeeNetApp>> apps = new HashMap<>();
    CoffeeNetApp coffeeNetApp = new CoffeeNetApp("Coffee App", "coffeeapp.coffeenet", emptySet());
    apps.put("cna1", singletonList(coffeeNetApp));
    CoffeeNetApp profileApp = new CoffeeNetApp("Profile", "profile.coffeenet", emptySet());
    apps.put("profile", singletonList(profileApp));
    sut.registerService(CoffeeNetNavigationDataExtractor.CoffeeNetServices.APP_SERVICE, coffeeNetAppServiceMock);
    when(coffeeNetAppServiceMock.getApps(any())).thenReturn(apps);
    Optional<Map<String, List<CoffeeNetApp>>> extractedApps = sut.extractApps();
    Map<String, List<CoffeeNetApp>> coffeeNetApps = extractedApps.get();
    assertThat(coffeeNetApps).hasSize(2);
    assertThat(coffeeNetApps.get("apps")).hasSize(1);
    assertThat(coffeeNetApps.get("apps").get(0)).isSameAs(coffeeNetApp);
    assertThat(coffeeNetApps.get("profile")).hasSize(1);
    assertThat(coffeeNetApps.get("profile").get(0)).isSameAs(profileApp);
}
Also used : HashMap(java.util.HashMap) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 10 with CoffeeNetApp

use of rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp in project coffeenet-starter by coffeenet.

the class CoffeeNetNavigationDataExtractorTest method extractAppsNoUser.

@Test
public void extractAppsNoUser() {
    Map<String, List<CoffeeNetApp>> apps = new HashMap<>();
    CoffeeNetApp coffeeNetApp = new CoffeeNetApp("Coffee App", "coffeeapp.coffeenet", emptySet());
    CoffeeNetApp coffeeNetApp2 = new CoffeeNetApp("Coffee App2", "coffeeapp.coffeenet", new HashSet<>(singletonList("ROLE_COFFEENET-USER")));
    apps.put("cna1", singletonList(coffeeNetApp));
    apps.put("cna2", singletonList(coffeeNetApp2));
    CoffeeNetApp profileApp = new CoffeeNetApp("Profile", "profile.coffeenet", emptySet());
    apps.put("profile", singletonList(profileApp));
    sut.registerService(CoffeeNetNavigationDataExtractor.CoffeeNetServices.APP_SERVICE, coffeeNetAppServiceMock);
    when(coffeeNetAppServiceMock.getApps(any())).thenReturn(apps);
    // user
    sut.registerService(CoffeeNetNavigationDataExtractor.CoffeeNetServices.USER_SERVICE, coffeeNetCurrentUserServiceMock);
    when(coffeeNetCurrentUserServiceMock.get()).thenReturn(Optional.empty());
    Optional<Map<String, List<CoffeeNetApp>>> extractedApps = sut.extractApps();
    Map<String, List<CoffeeNetApp>> coffeeNetApps = extractedApps.get();
    assertThat(coffeeNetApps).hasSize(2);
    assertThat(coffeeNetApps.get("apps")).hasSize(2);
    assertThat(coffeeNetApps.get("apps").get(0)).isSameAs(coffeeNetApp);
    assertThat(coffeeNetApps.get("apps").get(1)).isSameAs(coffeeNetApp2);
    assertThat(coffeeNetApps.get("profile")).hasSize(1);
    assertThat(coffeeNetApps.get("profile").get(0)).isSameAs(profileApp);
}
Also used : HashMap(java.util.HashMap) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) CoffeeNetApp(rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

CoffeeNetApp (rocks.coffeenet.autoconfigure.discovery.service.CoffeeNetApp)12 List (java.util.List)11 Collections.singletonList (java.util.Collections.singletonList)10 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 Map (java.util.Map)6 Collections.emptyList (java.util.Collections.emptyList)5 CoffeeNetUserDetails (rocks.coffeenet.autoconfigure.security.service.CoffeeNetUserDetails)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 HumanCoffeeNetUser (rocks.coffeenet.autoconfigure.security.service.HumanCoffeeNetUser)2 CASE_INSENSITIVE_ORDER (java.lang.String.CASE_INSENSITIVE_ORDER)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumMap (java.util.EnumMap)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 BuildProperties (org.springframework.boot.info.BuildProperties)1 AppQuery (rocks.coffeenet.autoconfigure.discovery.service.AppQuery)1 Builder (rocks.coffeenet.autoconfigure.discovery.service.AppQuery.Builder)1