Skip to content

Commit

Permalink
Fix authorization tests using http configuration bean (eclipse-basyx#338
Browse files Browse the repository at this point in the history
)

* Restrict http test configs to profile 'httptests'

* Add 'httptests' profile to affected tests

* Add 'authorization' profile for auth related tests not using 'httptests'
config file
  • Loading branch information
mateusmolina-iese authored Jul 29, 2024
1 parent 45afd4f commit 700059e
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -55,7 +55,7 @@ public class TestConnectedAasRepository extends AasRepositorySuite {

@BeforeClass
public static void startAASRepo() throws Exception {
appContext = new SpringApplication(DummyAasRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAasRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;


/**
Expand All @@ -43,6 +44,7 @@
*
*/
@Configuration
@Profile("authorization")
public class DummyAuthorizedAasRepositoryConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -92,7 +92,7 @@ public class TestAuthorizedAasRepository {
public static void setUp() throws FileNotFoundException, IOException {
tokenProvider = new AccessTokenProvider(authenticaltionServerTokenEndpoint, clientId);

appContext = new SpringApplication(DummyAuthorizedAasRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAuthorizedAasRepositoryComponent.class).profiles("authorization").run(new String[] {});

aasRepo = appContext.getBean(AasRepository.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -46,7 +46,7 @@ public class TestAasRepositoryHTTP extends AasRepositoryHTTPSuite {

@BeforeClass
public static void startAasRepo() throws Exception {
appContext = new SpringApplication(DummyAasRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAasRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;


/**
Expand All @@ -42,6 +43,7 @@
*
*/
@Configuration
@Profile("httptests")
public class DummyAasRepositoryConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -72,16 +72,12 @@ public class TestAuthorizedConnectedAasService extends AasServiceSuite {

private static final String AAS_REPO_URL = "http://localhost:8081/shells/";
private static ConfigurableApplicationContext appContext;
private static final String PROFILE = "authorization";
public static String authenticaltionServerTokenEndpoint = "http://localhost:9096/realms/BaSyx/protocol/openid-connect/token";
public static String clientId = "basyx-client-api";

@BeforeClass
public static void startAASRepo() throws Exception {
SpringApplication application = new SpringApplication(DummyAuthorizedAasRepositoryComponent.class);
application.setAdditionalProfiles(PROFILE);

appContext = application.run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAuthorizedAasRepositoryComponent.class).profiles("authorization").run(new String[] {});
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -50,7 +50,7 @@ public class TestConnectedAasService extends AasServiceSuite {

@BeforeClass
public static void startAASRepo() throws Exception {
appContext = new SpringApplication(DummyAasRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAasRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
* Configuration for tests
Expand All @@ -42,6 +43,7 @@
*
*/
@Configuration
@Profile("authorization")
public class DummyAuthorizedCDRepositoryConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.eclipse.digitaltwin.basyx.http.serialization.BaSyxHttpTestUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -79,7 +79,7 @@ public class TestAuthorizedCDRepository {
public static void setUp() throws FileNotFoundException, IOException, DeserializationException {
tokenProvider = new AccessTokenProvider(authenticaltionServerTokenEndpoint, clientId);

appContext = new SpringApplication(DummyAuthorizedCDRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAuthorizedCDRepositoryComponent.class).profiles("authorization").run(new String[] {});

cdRepo = appContext.getBean(ConceptDescriptionRepository.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -50,7 +50,7 @@ public class TestConceptDescriptionRepositoryHTTP extends ConceptDescriptionRepo

@BeforeClass
public static void startConceptDescriptionRepo() throws Exception {
appContext = new SpringApplication(DummyConceptDescriptionRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyConceptDescriptionRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
* Configuration for tests
Expand All @@ -39,6 +40,7 @@
*
*/
@Configuration
@Profile("httptests")
public class DummyConceptDescriptionRepositoryConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -37,6 +38,7 @@
import org.eclipse.digitaltwin.basyx.client.internal.authorization.credential.ClientCredential;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.grant.ClientCredentialAccessTokenProvider;
import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelrepository.core.SubmodelRepositorySuite;
Expand Down Expand Up @@ -77,7 +79,10 @@ public void removeSubmodelFromRepo() throws FileNotFoundException, IOException {
TestAuthorizedConnectedSubmodelService.configureSecurityContext(TestAuthorizedConnectedSubmodelService.getTokenProvider());

SubmodelRepository repo = appContext.getBean(SubmodelRepository.class);
repo.getAllSubmodels(PaginationInfo.NO_LIMIT).getResult().stream().map(s -> s.getId()).forEach(repo::deleteSubmodel);
try {
repo.getAllSubmodels(PaginationInfo.NO_LIMIT).getResult().stream().map(s -> s.getId()).forEach(repo::deleteSubmodel);
} catch (Exception e) {
}

SecurityContextHolder.clearContext();
}
Expand Down Expand Up @@ -139,4 +144,15 @@ protected boolean fileExistsInStorage(String fileValue) {

return file.exists();
}

@Override
public void invokeOperation() {
// TODO
}

@Override
public void invokeNonOperation() {
// TODO
throw new NotInvokableException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -55,7 +55,7 @@ public class TestConnectedSubmodelRepository extends SubmodelRepositorySuite {

@BeforeClass
public static void startAASRepo() throws Exception {
appContext = new SpringApplication(DummySubmodelRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummySubmodelRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;


/**
Expand All @@ -43,6 +44,7 @@
*
*/
@Configuration
@Profile("authorization")
public class DummyAuthorizedSubmodelRepositoryConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -97,7 +97,7 @@ public class TestAuthorizedSubmodelRepository {
public static void setUp() throws FileNotFoundException, IOException, DeserializationException {
tokenProvider = new AccessTokenProvider(authenticaltionServerTokenEndpoint, clientId);

appContext = new SpringApplication(DummyAuthorizedSubmodelRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummyAuthorizedSubmodelRepositoryComponent.class).profiles("authorization").run(new String[] {});

submodelRepo = appContext.getBean(SubmodelRepository.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
package org.eclipse.digitaltwin.basyx.submodelrepository.http;

import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;

import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelservice.http.SubmodelServiceSubmodelElementsTestSuiteHTTP;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -49,7 +48,7 @@ public class TestSubmodelRepositorySubmodelElementsHTTP extends SubmodelServiceS

@BeforeClass
public static void startAASRepo() throws Exception {
appContext = new SpringApplication(DummySubmodelRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummySubmodelRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

/**
Expand All @@ -47,7 +47,7 @@ public class TestSubmodelRepositorySubmodelHTTP extends SubmodelRepositorySubmod

@BeforeClass
public static void startAASRepo() throws Exception {
appContext = new SpringApplication(DummySubmodelRepositoryComponent.class).run(new String[] {});
appContext = new SpringApplicationBuilder(DummySubmodelRepositoryComponent.class).profiles("httptests").run(new String[] {});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
* Configuration for tests
Expand All @@ -41,6 +42,7 @@
*
*/
@Configuration
@Profile("httptests")
public class DummySubmodelRepositoryConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.credential.ClientCredential;
import org.eclipse.digitaltwin.basyx.client.internal.authorization.grant.ClientCredentialAccessTokenProvider;
import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException;
import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo;
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier;
import org.eclipse.digitaltwin.basyx.http.serialization.BaSyxHttpTestUtils;
Expand Down Expand Up @@ -86,7 +87,10 @@ public static void startAASRepo() throws Exception {
@After
public void removeAasFromRepo() {
SubmodelRepository repo = appContext.getBean(SubmodelRepository.class);
repo.getAllSubmodels(PaginationInfo.NO_LIMIT).getResult().stream().map(s -> s.getId()).forEach(repo::deleteSubmodel);
try {
repo.getAllSubmodels(PaginationInfo.NO_LIMIT).getResult().stream().map(s -> s.getId()).forEach(repo::deleteSubmodel);
} catch (Exception e) {
}
}

@AfterClass
Expand Down Expand Up @@ -161,4 +165,15 @@ public static AccessTokenProvider getTokenProvider() {
return new AccessTokenProvider(authenticaltionServerTokenEndpoint, clientId);
}

@Override
public void invokeOperation() {
// TODO
}

@Override
public void invokeNonOperation() {
// TODO
throw new NotInvokableException();
}

}
Loading

0 comments on commit 700059e

Please sign in to comment.