Skip to content

Commit

Permalink
MOSIP-32842 Fix test case failure
Browse files Browse the repository at this point in the history
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
kameshsr committed May 14, 2024
1 parent 4542769 commit fc1932a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 66 deletions.
11 changes: 6 additions & 5 deletions partner/partner-management-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -82,6 +77,12 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.util.ContentCachingRequestWrapper;

Expand Down Expand Up @@ -48,26 +46,26 @@ public class PartnerServiceResponseExceptionHandler extends ResponseEntityExcept
String msg = "mosip.partnermanagement";
String version = "1.0";

// @Override
// protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
// HttpHeaders headers, HttpStatus status, WebRequest request) {
// Map<String, Object> body = new LinkedHashMap<>();
// body.put("id", null);
// body.put("version", null);
// body.put("metadata", null);
// body.put("response", null);
// body.put("responsetime", LocalDateTime.now(ZoneId.of("UTC")));
// List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
// FieldError fieldError = fieldErrors.get(0);
//
// ErrorResponse errorResponse = new ErrorResponse();
// errorResponse.setErrorCode(ErrorCode.MISSING_PARTNER_INPUT_PARAMETER.getErrorCode());
// errorResponse.setMessage("Invalid request parameter - " + fieldError.getDefaultMessage() + " :" + fieldError.getField());
// List<ErrorResponse> errors = new ArrayList<>();
// errors.add(errorResponse);
// body.put("errors", errors);
// return new ResponseEntity<>(body, headers, HttpStatus.OK);
// }
// @ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("id", null);
body.put("version", null);
body.put("metadata", null);
body.put("response", null);
body.put("responsetime", LocalDateTime.now(ZoneId.of("UTC")));

List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
FieldError fieldError = fieldErrors.get(0);

ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(ErrorCode.MISSING_PARTNER_INPUT_PARAMETER.getErrorCode());
errorResponse.setMessage("Invalid request parameter - " + fieldError.getDefaultMessage() + " :" + fieldError.getField());
List<ErrorResponse> errors = new ArrayList<>();
errors.add(errorResponse);
body.put("errors", errors);
return ResponseEntity.badRequest().body(body);
}

@ExceptionHandler(MISPServiceException.class)
public ResponseEntity<ResponseWrapper<ErrorResponse>> getExcepionMassages(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package io.mosip.pms.test.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
Expand All @@ -21,36 +14,41 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class TestSecurityConfig {


@Bean
public HttpFirewall defaultHttpFirewall() {
return new DefaultHttpFirewall();
}

// @Bean
// public WebSecurity configure(WebSecurity webSecurity) throws Exception {
// webSecurity.ignoring().requestMatchers(allowedEndPoints());
// @Override
// public void configure(WebSecurity webSecurity) throws Exception {
// webSecurity.ignoring().antMatchers(allowedEndPoints());
// super.configure(webSecurity);
// webSecurity.httpFirewall(defaultHttpFirewall());
// return webSecurity;
// }

@Bean
protected SecurityFilterChain configure(final HttpSecurity httpSecurity) throws Exception {
protected SecurityFilterChain configureSecurityFilterChain(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf(http -> http.disable());
return httpSecurity.build();
}

private String[] allowedEndPoints() {
return new String[] { "*","/swagger-ui.html" };
return new String[]{"*", "/swagger-ui.html"};
}

// @Bean
// @Override
// protected void configure(final HttpSecurity httpSecurity) throws Exception {
// httpSecurity.csrf(httpEntry -> httpEntry.disable());
// httpSecurity.csrf().disable();
// httpSecurity.httpBasic().and().authorizeRequests().anyRequest().authenticated().and().sessionManagement()
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().exceptionHandling()
// .authenticationEntryPoint(unauthorizedEntryPoint());
Expand All @@ -64,16 +62,16 @@ public AuthenticationEntryPoint unauthorizedEntryPoint() {
@Bean
public UserDetailsService userDetailsService() {
List<UserDetails> users = new ArrayList<>();
users.add(new User("misp-user", "misp",
Arrays.asList(new SimpleGrantedAuthority("ROLE_MISP"))));
users.add(new User("policy", "policy",
Arrays.asList(new SimpleGrantedAuthority("POLICYMANAGER"))));
users.add(new User("partner", "partner",
Arrays.asList(new SimpleGrantedAuthority("PARTNER"))));
users.add(new User("zonal-admin", "admin",
Arrays.asList(new SimpleGrantedAuthority("ZONAL_ADMIN"))));
users.add(new User("partner-admin", "admin",
Arrays.asList(new SimpleGrantedAuthority("ROLE_PARTNER_ADMIN"))));
users.add(User.withDefaultPasswordEncoder().username("misp-user").password("misp")
.authorities(new SimpleGrantedAuthority("ROLE_MISP")).build());
users.add(User.withDefaultPasswordEncoder().username("policy").password("policy")
.authorities(new SimpleGrantedAuthority("POLICYMANAGER")).build());
users.add(User.withDefaultPasswordEncoder().username("partner").password("partner")
.authorities(new SimpleGrantedAuthority("PARTNER")).build());
users.add(User.withDefaultPasswordEncoder().username("zonal-admin").password("admin")
.authorities(new SimpleGrantedAuthority("ZONAL_ADMIN")).build());
users.add(User.withDefaultPasswordEncoder().username("partner-admin").password("admin")
.authorities(new SimpleGrantedAuthority("ROLE_PARTNER_ADMIN")).build());
return new InMemoryUserDetailsManager(users);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@
*/
package io.mosip.pms.policy.test;

import io.mosip.kernel.dataaccess.hibernate.config.HibernateDaoConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Import;

import io.mosip.pms.common.helper.FilterHelper;
import io.mosip.pms.common.helper.SearchHelper;
import io.mosip.pms.common.helper.WebSubPublisher;
import io.mosip.pms.common.util.PageUtils;
import io.mosip.pms.common.util.RestUtil;
import io.mosip.pms.common.validator.FilterColumnValidator;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

/**
* @author Nagarjuna
*
*/
@Import(value = {WebSubPublisher.class,RestUtil.class,SearchHelper.class,FilterHelper.class,PageUtils.class,FilterColumnValidator.class,RestUtil.class})
@SpringBootApplication(scanBasePackages = { "io.mosip.*"},exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class })

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan(value = {"io.mosip.pms.*"}, excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {HibernateDaoConfig.class})})
public class PolicyServiceTest {

/**
Expand Down

0 comments on commit fc1932a

Please sign in to comment.